简体   繁体   English

使用scanf读取字符串,然后读取整数,仅读取字符串

[英]Using scanf to read a string then a integer, reads string only

I'm trying to use two scanf's to read a string and then an integer. 我正在尝试使用两个scanf读取一个字符串,然后读取一个整数。 The program waits for me to enter the string, i press enter and then doesn't wait for me to insert the integer. 该程序等我输入字符串,然后按Enter键,然后不等我插入整数。 The code i'm using: 我正在使用的代码:

printf("Insert the name of the author to search: ");
scanf("%300s", author);

printf("Insert the year: ");
scanf("%d", &year);

Any suggestions? 有什么建议么?

The conversion specifier "%s" breaks on whitespace. 转换说明符"%s"在空格处中断。

If you enter, for instance, "John Smith", the variable author will have "John" and the rest of the input will be used for year . 例如,如果输入“ John Smith”,则变量author将具有"John" ,其余的输入将用于year

Always validate the return value of (most) library functions. 始终验证(大多数)库函数的返回值。

printf("Insert the name of the author to search: ");
if (scanf("%300s", author) != 1) /* error */;

printf("Insert the year: ");
if (scanf("%d", &year) != 1) /* error */;

The best way to get user input is to use fgets() , then, if needed, parse the input and assign to variables. 获取用户输入的最佳方法是使用fgets() ,然后根据需要解析输入并分配给变量。

char tmp[1000];

printf("Insert the name of the author to search: ");
if (!fgets(tmp, sizeof tmp, stdin)) /* error */;
strcpy(author, tmp);

printf("Insert the year: ");
if (!fgets(tmp, sizeof tmp, stdin)) /* error */;
if (sscanf(tmp, "%d", &year) != 1) /* error */;

you are trying to read input into author array where input length can be upto 300 characters,So i hope that you have declared your array size as char author[301] , 300+1 (one extra for accomadating '\\0'). 您正在尝试将输入读取到author数组中,其中输入长度最多可以为300个字符,因此,我希望您已将数组大小声明为char author[301] ,300 + 1(一个额外的用于容纳'\\ 0')。

You can use function like this scanf(" %300[^\\n],author") this means keep reading characters from keyboard(or you can say keyboard buffer) until '\\n' is found, and '\\n' is generated when you hit Enter key,Hence we are forcing scanf() to read characters until we hit enter. 您可以使用函数像这样scanf(" %300[^\\n],author")这意味着继续阅读从键盘字符(或者你可以说键盘缓冲区),直到'\\n'被发现,和'\\n'产生当您按下Enter键时,因此我们将强制scanf()读取字符,直到我们按下Enter键为止。

We can use fgets() but one of the problem of it is that it will even read '\\n' into array,which will always print the output of next printf() operation on one extra new line.This can cause problem when you want your output to be in tabular form. 我们可以使用fgets()但是它的问题之一是它甚至会将'\\n'读入数组,这将总是在下一个新行上打印下一个printf() operation的输出。这可能会导致问题希望您的输出采用表格形式。

int main()
{
   char author[301]="\0";//initialize char array to '\0' NULL's
   int year;
   printf("Insert the name of the author to search: ");

   if( scanf(" %300[^\n]", author)!=1 )
   {
       printf("End of Input or Error Occurred reading Name.\n");
       return 1;// return (NON-ZERO) means unsuccessful termination
   }

   printf("Insert the year: ");
   if( scanf("%d", &year)!=1 )
   {
       printf("End of Input or Error Occurred reading Year.\n");
       return 1;// return (NON-ZERO) means unsuccessful termination
   }    
return 0;
}

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

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