简体   繁体   English

Scanf函数不接受输入

[英]Scanf function not accepting input

I am working on a simple C program using a struct named 'student'. 我正在使用名为'student'的结构编写一个简单的C程序。 Here is my code 这是我的代码

#include<stdio.h>
#include<stdlib.h>
  struct  student {
    char name[50];
    int id;
    float marks_1;
    float marks_2;

};


void main(){

    int num,a,i;
    printf("Enter number of students\n");
    scanf("%d",&num);
    struct student s[num];
    for(i=0;i<num;i++)
    {
        a=i+1;
        printf("Enter name of student number %d\n",a);
        scanf("%[^\n]%*c",s[i].name);

    }

  }

When I run the program I am able to enter the number of students correctly, but after that I am not able to enter the name corresponding to each student. 当我运行该程序时,我能够正确输入学生人数,但之后我无法输入与每个学生相对应的名称。 This is the output that I get. 这是我得到的输出。

Enter number of students
2
Enter name of student number 1
Enter name of student number 2

RUN FINISHED; exit value 2; real time: 1s; user: 0ms; system: 0ms

What might be the problem? 可能是什么问题? Any help appreciated 任何帮助赞赏

scanf("%d",&num);

leaves the newline you typed to send the input to the programme in the input buffer. 离开您键入的换行符,将输入发送到输入缓冲区中的程序。 Thus the first iteration of 因此,第一次迭代

for(i=0;i<num;i++)
{
    a=i+1;
    printf("Enter name of student number %d\n",a);
    scanf("%[^\n]%*c",s[i].name);

}

immediately finds that newline and scans in an empty string. 立即找到该换行符并扫描一个空字符串。

Consume the newline before scanning, either by changing the first format to "%d%*c" or by adding a space to the start of the name-scanning format to skip initial white space. 扫描前可以使用换行符,方法是将第一种格式更改为"%d%*c"或者在名称扫描格式的开头添加一个空格以跳过初始空白。

change scanf("%[^\\n]%*c",s[i].name); 更改scanf("%[^\\n]%*c",s[i].name); to scanf(" %[^\\n]%*c",s[i].name); to scanf(" %[^\\n]%*c",s[i].name); . Notice the space given before specifier to consume last input char left in stdin. 注意在说明符之前给出的空间消耗stdin中剩下的最后一个输入字符。

Scanf function not accepting input Scanf函数不接受输入

If scanf() doesn't work, then use fgets() : 如果scanf()不起作用,请使用fgets()

fgets(s[i].name, sizeof(s[i].name), stdin);

And stay far away from scanf() if/while you don't have a full, proper understanding of how it works, because it's not intuitive to use, so to say. 如果/虽然您对它的工作方式没有一个完整,正确的了解,请远离scanf() ,因为这样使用起来不直观。 (It's also unsafe if you are not careful enough, and in this case, you weren't. The code is prone to buffer overflows.) (如果您不够小心,也是不安全的,在这种情况下,您不是很安全。该代码易于发生缓冲区溢出。)

I'm not sure what you're trying to do here: 我不确定你在这里要做什么:

scanf("%[^\n]%*c",s[i].name);

but try replacing it with: 但尝试将其替换为:

scanf("%50[^\n]s",s[i].name);

to read a line in c that include white spaces use fgets (name, 100, stdin); 要读取c中包含空格的行,请使用fgets (name, 100, stdin);

here is the full code: 这是完整的代码:

#include<stdio.h>
#include<stdlib.h>
  struct  student {
    char name[50];
    int id;
    float marks_1;
    float marks_2;

};


void main(){

    int num,a,i;
    printf("Enter number of students\n");
    scanf("%d",&num);
    struct student s[num];
    for(i=0;i<num;i++)
    {
        a=i+1;
        printf("Enter name of student number %d\n",a);
        fgets (s[i].name, 50, stdin);

    }

  }

scanf will read until the first whitespace, but not fgets , however, if you'll press enter when using fgets it'll will be stored in the string as well. scanf将读取直到第一个空格,但不是fgets ,但是,如果你在使用fgets时按Enter键,它也将被存储在字符串中。 so you need to remove it afterwards 因此您需要在以后将其删除

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM