垃圾值随机发生

时间:2016-01-19 19:16:31

标签: c

这是该计划。这基本上需要输入和显示输出(我还在学习)。问题是,输出不正确,并为“1”和“1”提供了垃圾值。索引号。

以下是代码:

typedef struct {
    char name[256];
    int id;
    int classes;
} students;

int main(void) {
    students a[3];
    for (int i = 0; i < 3; i++) {
        //fgets(word, sizeof(word), stdin);
        fgets(a[i].name, 100, stdin);
        scanf("%d", &a[i].id);
        scanf("%d", &a[i].classes);
    }
    for (int i = 0; i < 3; i++) {
        printf("Your name is: %s", a[i].name);
    //  printf("\n");
        printf("Your id is: %d", a[i].id + 1);
        printf("\n");
        printf("Your class is: %d", a[i].classes + 1);
        printf("\n");
        printf("\n");
    }
}

我提供的随机输入是:

as
2
33
asd
3
44
asdasd
4
55

但是,输出是(意外):

Your name is: as
Your id is: 3
Your class is: 34

Your name is: 
Your id is: -163754449
Your class is: 129100402

Your name is: asd
Your id is: 4
Your class is: 45

请让我知道为什么会这样。

0 个答案:

没有答案