C - 打印阵列无法正常工作

时间:2021-12-14 21:28:49

I am writing a code about inserting a grade / student number (KAS) / Name of the student into arrays

我正在编写一个关于将学生/学生编号(KAS)/学生姓名插入数组的代码

And the after unknown number of inputs print them in the end

最后,未知数量的输入打印出来

ISSUE: The issue of my project is that on printing results there are some faulty elements on printing .

问题:我的项目问题是,在打印结果上,打印时存在一些错误的元素。

I tried to check every element that I inserted the exact time after I made an input ( with a printf(...) ) and everything seemed fine.

我尝试检查我输入的每个元素(输入printf(...)后的确切时间),一切似乎都很好。

But still in the output some of them were wrong .

但仍在输出中,其中一些是错误的。

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int grade[100] , KAS[100] ,x,spots = 0; // KAS = student number
    char name[spots][14], answer;

    printf("Please insert a grade : ");
    scanf("%d", &grade[spots]);    
    getchar();

    printf("Please add a KAS : ");
    scanf("%d",&KAS[spots]);
    getchar();

    printf("Please enter a name : ");
    scanf("%s",&name[spots]);
    getchar();

    printf("\nDo you want to add another value? y/n : ");
    scanf("%c",&answer);
    puts("");
    getchar();

    while(answer == 'y')
    {
        spots++;

            printf("Please insert another grade : ");
            scanf("%d", &grade[spots]);
            getchar();

            printf("Please add another KAS : ");
            scanf("%d",&KAS[spots]);
            getchar();

            printf("Please enter another name : ");
            scanf("%s",&name[spots]);
            getchar();


            printf("\nDo you want to add another value? y/n : ");
            scanf("%c\n",&answer);
            puts("");
            getchar();

            if(answer == 'n') 
            {
                break;
            }
    }
    puts("*****************************");
    for(x = 0; x < spots; x++)
    {
        puts("");
        printf("%d. Student's great : %d\n",x,grade[x]);
        printf("%d. Student's KAS : %d\n",x,KAS[x]);
        printf("%d. Student's name : %s\n",x,name[x]);
    }
    puts("\n*****************************\n");
}

2 个解决方案

#1


2  

char name[spots][14];         //as spots is 0 , it would be name[0][14]
 /* you would end up access invalid memory and cause UB */

You need to change this to -

您需要将此更改为 -

char name[100][14];

And also while taking input in both statements inside and before loop-

同时在循环内部和循环之前的两个语句中输入 -

scanf("%s",&name[spots]);
           ^ you don't need to use & 

And also in the for loop use x <= spots as loop condition.

并且在for循环中使用x <= spot作为循环条件。

#2


0  

Your Code is alright. There are just a few mistakes there. 1. initialize array by writing static int grade[ ]... 2.Remove getchar before if(answer == 'n') statement. 3.The condition in for loop should be x <= spots. This is the reason that if you enter 2 student names, it prints name/record of only one student. Hope it helped :)

你的守则没问题。那里只有一些错误。 1.通过编写static int grade []来初始化数组... 2.在if(answer =='n')语句之前删除getchar。 3. for循环中的条件应为x <=斑点。这就是为什么如果您输入2个学生姓名,它只打印一个学生的姓名/记录。希望它有帮助:)

#1


2  

char name[spots][14];         //as spots is 0 , it would be name[0][14]
 /* you would end up access invalid memory and cause UB */

You need to change this to -

您需要将此更改为 -

char name[100][14];

And also while taking input in both statements inside and before loop-

同时在循环内部和循环之前的两个语句中输入 -

scanf("%s",&name[spots]);
           ^ you don't need to use & 

And also in the for loop use x <= spots as loop condition.

并且在for循环中使用x <= spot作为循环条件。

#2


0  

Your Code is alright. There are just a few mistakes there. 1. initialize array by writing static int grade[ ]... 2.Remove getchar before if(answer == 'n') statement. 3.The condition in for loop should be x <= spots. This is the reason that if you enter 2 student names, it prints name/record of only one student. Hope it helped :)

你的守则没问题。那里只有一些错误。 1.通过编写static int grade []来初始化数组... 2.在if(answer =='n')语句之前删除getchar。 3. for循环中的条件应为x <=斑点。这就是为什么如果您输入2个学生姓名,它只打印一个学生的姓名/记录。希望它有帮助:)