当换行符在格式字符串中时scanf的行为

下面是我的代码的副本。 基本上,我需要创建一个程序,根据“工资代码”计算工资,例如工人的职位。 我已经创建了我的switch语句,除了刚开始输入第一个付费代码时,一切正常。 我输入第一个付款代码,它转到下一行,将其留空。 我输入了另一个数字,它按照预期的方式运行该数字和前一个数字。 然后,在那之后一切正常。 我确信这是一个简单的解决方案,但对我来说有点棘手。 另外,我不确定我应该如何在这里格式化我的代码以使它看起来像在服务器上,所以我为它看起来令人困惑的方式道歉。

#include  //precompiled header int main(void) { //declare variables unsigned int counter = 0; //counters to calculate amount of workers paid unsigned int counter2 = 0; unsigned int counter3 = 0; unsigned int counter4 = 0; int paycode; float overtime; //amount of overtime hours float salary; //weekly salary float hoursWorked; float hourlyRate; float grossWeeklySales; //weekly sales for commissioned workers int itemsProduced; float fixedAmount; //money given per item produced //prompt for input printf("Please enter the employee's paycode.\n"); printf("1: Manager\n"); printf("2: Hourly Worker\n"); printf("3: Commission Worker\n"); printf("4: Pieceworker\n"); printf("-1 to end\n"); printf("%s","Paycode: "); scanf("%d\n", &paycode); while (paycode != -1)//begin while loop { switch(paycode) { case 1: //calculate manager's pay printf("Manager selected.\n"); printf("Enter weekly salary: $ "); scanf("%f", &salary); counter = counter + 1; printf("Weekly salary is %.2f\n\n", salary); break; case 2: printf("Hourly worker selected.\n"); printf("Enter hourly rate: $"); scanf("%f", &hourlyRate); printf("Enter hours worked: "); scanf("%f", &hoursWorked); if(hoursWorked<=40) //if statement to calculate overtime { salary=hourlyRate*hoursWorked; printf("No overtime worked."); } else { salary=40.0*hourlyRate+(hoursWorked-40)*1.5*hourlyRate; overtime = hoursWorked - 40; printf("Total amount of overtime worked: %.2f\n", overtime); } counter2 = counter2 +1; printf("Weekly salary is: $%.2f\n\n", salary); break; case 3: printf("Commissioned worker selected.\n"); printf("Enter gross weekly sales: $"); scanf("%f", &grossWeeklySales); salary=.057*grossWeeklySales+250; counter3 = counter3 +1; printf("Weekly salary is: $%.2f\n\n", salary); break; case 4: printf("Pieceworker Selected.\n"); printf("Enter amount of items produced: "); scanf("%d", &itemsProduced); printf("Enter the fixed pay per item produced: $ "); scanf("%f", &fixedAmount); salary=itemsProduced*fixedAmount; counter4 = counter4 + 1; printf("Weekly salary is: $%.2f\n\n", salary); } //get next input printf("Please enter paycode, -1 to end.\n"); printf("%s","Paycode: "); scanf("%d", &paycode); } printf("Number of managers paid: %d\n", counter); //display amount of workers paid printf("Number of hourly workers paid is: %d\n", counter2); printf("Number of commisioned workers is: %d\n", counter3); printf("Number of piece workers paid is: %d\n\n", counter4); } 

scanf("%d\n", &paycode)格式字符串中的'\n'字符scanf("%d\n", &paycode)匹配任意数量的空白字符(空格,制表符,换行符等) – 在ctype.h声明的isspace函数产生的字符为true )在给出的输入中。 因此, scanf调用将读取并丢弃任意数量的空白字符,直到遇到非空白字符,此时它将返回。 对于scanf格式字符串中的任何空格字符都是如此,而不仅仅是换行符。 例如,以下内容将表现出相同的行为:

 scanf("%d ", &paycode) ^ a space 

您应该将scanf调用更改为

 scanf("%d", &paycode); 

另外,不是printf("%s", "Paycode: "); 你可以简单地写printf("Paycode: "); 您已经评论过stdio.h是一个预编译的头文件。 不是。 它是一个头文件,包含宏定义和函数声明或原型。 它不是预编译意义上的目标文件。

函数scanf接收您期望的格式作为参数。 通过scanf("%d\n", &paycode); 你说的是“嘿,计算机,读取数字和换行符,并将其保存在paycode变量中”。 此外, scanf将读取并忽略空格字符。

尝试将其更改为scanf("%d", &paycode); ,因此您只能读取并保存数字。

更改

 scanf("%d\n", &paycode); 

 scanf("%d", &paycode); 
 scanf("%d\n", &paycode); ^^ Scanf is waiting for this newline to come. 

将其更改为:

 scanf("%d", &paycode); 

换行常数令人困惑。 你需要改变第一个

scanf(“%d \ n”,&paycode);

scanf(“%d”,&paycode);