[英]some fgets cannot work in C programming
#include <stdio.h>
#include <string.h>
int main()
{
int sel_1, sel_2, telno[12], price = 0, sum = 0, i;
char pkg[2], name[10], addrs[30], name_1[50][50], addrs_1[50][50];
printf(" Lunch4You Enterprise\n Delivery Service Subscription System\n");
printf("==========================================================================");
printf("\nPackage that we have:\n 1. Package A: 20days/month(5days/week) - RM60/month");
printf("\n 2. Package B: 12days/month(3days/week) - RM45/month");
printf("\n 3. Package C: 8days/month(2days/week) - RM35/month");
do
{
printf("\nPlease select the package: ");
scanf("%d", &sel_1);
}
while (sel_1 != 1 && sel_1 != 2 && sel_1 != 3);
switch (sel_1)
{
case 1: strcpy(pkg, "A"); price = 60;
break;
case 2: strcpy(pkg, "B"); price = 45;
break;
case 3: strcpy(pkg, "C"); price = 35;
break;
}
printf("\n\nYou have selected Package %s.", pkg);
do
{
printf("\nHow many receiver of lunchbox?: ");
scanf("%d", &sel_2);
}
while (sel_2 > 5 || sel_2 < 1);
sum = sel_2 * price;
printf("\n\nPlease enter the information below:\nName of subscriber: ");
scanf("%s", &name);
printf("\nAddress (Pick-up): ");
fgets(addrs,30,stdin);
for (i = 0; i < sel_2; ++i)
{
printf("Name of receiver (%d): ", i + 1);
fgets(name_1[i], 50, stdin);
printf("Address(Receiver)(%d): ", i + 1);
fgets(addrs_1[i], 50, stdin);
printf("Tel No (%d)", i + 1);
scanf("%d", &telno[i]);
}
printf("\n\nYour information for lunchbox delivery are:");
printf("\nPackage : %s", pkg);
printf("\nPick-up Address: %s", addrs);
for (i = 0; i < sel_2; ++i)
{
printf("\nReceiver %d: %s, %s\n\t\t%d", i + 1, name_1[i], addrs_1[i], telno[i]);
}
printf("\n\nTotal fee: RM%d\n", sum);
return 0;
}
$some fgets cannot work, it just skip to the next fgets then wait for users to enter.(in this case, the fgets(addrs) and fgets(name_1) in the 'for loop' cannot work) can I know why and how to solve this problem? $ some fgets无法工作,它只是跳到下一个fgets,然后等待用户输入。(在这种情况下,“ for循环”中的fgets(addrs)和fgets(name_1)无法工作)我可以知道原因和方式解决这个问题? thks.
THKS。
This sequence is a problem: 这个序列是一个问题:
scanf("%s", &name);
printf("\nAddress (Pick-up): ");
fgets(addrs,30,stdin);
The scanf()
stopped at the first white space character, quite probably a newline; scanf()
停在第一个空格字符,很可能是换行符; the fgets()
then reads up to and including the newline, so it doesn't need to wait for any input because there's a newline already in the input buffer, left over from the scanf()
call. 然后,
fgets()
读取并包括换行符,因此它不需要等待任何输入,因为在输入缓冲区中已经有一个换行符,它是scanf()
调用留下的。
You need to read up to the newline after the scanf()
. 您需要在
scanf()
之后阅读换行符。 And you should be checking for problems after each input operation. 并且您应该在每次输入操作之后检查问题。
if (scanf("%s", name) != 1)
…report error…
while ((c = getchar()) != EOF && c != '\n')
;
if (fgets(addrs, sizeof(addrs), stdin) == 0)
…report error…
You need to end (not start) your printf(3) format string with \\n
or to call fflush(3) since stdio(3) is usually buffered (see setvbuf(3) ...); 您需要使用
\\n
结束(而不是开始) printf(3)格式字符串或调用fflush(3),因为stdio(3)通常是缓冲的(请参阅setvbuf(3) ...); so add a fflush(NULL);
所以添加一个
fflush(NULL);
before every call to scanf
. 在每次调用
scanf
之前。
You should also test the return (scanned items) count of scanf(3) . 您还应该测试scanf(3)的返回(已扫描项目)计数。
Read also documentation of fgets(3) . 另请参阅fgets(3)的文档。 You may prefer to use getline(3) or even (on systems having it, like Linux) readline(3)
您可能更喜欢使用getline(3)甚至(在具有它的系统上,例如Linux)使用readline(3)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.