scanf函数的行为时,换行符是格式字符串 [英] Behaviour of scanf when newline is in the format string
问题描述
下面是我的code的副本。基本上,我需要创建计算支付基于支付codeS,例如工人的地位的程序。我已经建立了我的switch语句和一切正常,除了当我进入先交code之初。我进入先交code,这是不言而喻到下一行,留下空白。我把另外一个号码,它运行数量和previous数是应该的方式。然后,在那之后一切正常。我敢肯定,这是一个简单的解决方案,但它正在对我来说有点棘手。另外,我不知道我究竟应该如何设置格式我这里code,使它看起来像它在服务器上,所以我为它看起来混乱的做法道歉。
的#include<&stdio.h中GT; // precompiled头
INT主要(无效)
{
//声明变量
无符号整型计数器= 0; //计数器来计算工资的工人数量
无符号整型计数器2 = 0;
无符号整型计数器3 = 0;
unsigned int类型counter4 = 0; INT付code;
浮加班; //加班时间量
浮动工资; //周薪
浮hoursWorked;
浮hourlyRate;
浮grossWeeklySales; //每周销售为委托工人
INT itemsProduced;
浮fixedAmount;每生产项目//给的钱 //提示输入
的printf(请输入员工的工资code \\ n);
的printf(1:经理\\ n);
的printf(2:小时工\\ n);
的printf(3:工人委员会\\ n);
的printf(4:Pieceworker \\ n);
的printf( - 1结束\\ n);
的printf(%S,支付code:);
scanf函数(%d个\\ n,&安培;支付code); 而(收费code!= -1)//开始while循环
{
开关(支付code)
{
案例1://计算经理人的薪酬
的printf(管理器中选择\\ n);
的printf(请输入周薪:$);
scanf函数(%F,&安培;工资);
计数器=计数器+ 1;
的printf(每周薪水%.2f \\ n \\ n,工资);
打破;
案例2:
的printf(小时工选择\\ n);
的printf(请输入时薪:$);
scanf函数(%F,&安培; hourlyRate);
的printf(请输入工作时间:);
scanf函数(%F,&安培; hoursWorked);
如果(hoursWorked< = 40)// if语句来计算加班
{
工资= hourlyRate * hoursWorked;
的printf(不加班的工作。);
}
其他
{
工资= 40.0 * hourlyRate +(hoursWorked-40)* 1.5 * hourlyRate;
加班费= hoursWorked - 40;
的printf(加班总额工作:%.2f \\ n,加班费);
}
计数器2 =计数器2 + 1;
的printf(每周工资是:$%2F \\ n \\ n,工资);
打破;
案例3:
的printf(委托工人选择\\ n);
的printf(请输入每周销售总额:$);
scanf函数(%F,&安培; grossWeeklySales);
工资= 0.057 * grossWeeklySales + 250;
计数器3 =计数器3 + 1;
的printf(每周工资是:$%2F \\ n \\ n,工资);
打破;
情况4:
的printf(Pieceworker选\\ n);
的printf(请输入生产的项目数量:);
scanf函数(%d个,&安培; itemsProduced);
的printf(请输入每生产项目的固定工资:$);
scanf函数(%F,&安培; fixedAmount);
工资= itemsProduced * fixedAmount;
counter4 = counter4 + 1;
的printf(每周工资是:$%2F \\ n \\ n,工资);
}
//获取下一个输入
的printf(请输入支付code,-1结束\\ n);
的printf(%S,支付code:);
scanf函数(%d个,&安培;支付code);
}
的printf(经理数支付数:%d \\ n,计数器); //工作者显示支付金额
的printf(支付小时工的数量为:%d个\\ N,计数器2);
的printf(commisioned工人人数为:%d个\\ N,计数器3);
的printf(支付计件工人数为:%d个\\ n \\ n,counter4);
}
功能 scanf函数
作为参数接收它期待你的格式。通过传递 scanf函数(%d个\\ n,&安培;支付code);
你说嘿,电脑,阅读数和换行,它保存在薪code
变量。此外, scanf函数
将读取并忽略空格字符。
试着将其更改为 scanf函数(%d个,&安培;支付code);
,让您的电脑只读取和保存数。 p>
Below is the copy of my code. Basically, I need to create a program that calculates pay based on "paycodes" eg the worker's position. I've created my switch statement and everything works except for the very beginning when I'm entering the first paycode. I enter the first paycode, it goes to the next line, leaving it blank. I put in another number, and it runs that number and the previous number the way it is supposed to. Then, after that everything works fine. I'm sure it's a simple solution but it is being a bit tricky for me. Also, I'm not sure exactly how I'm supposed to format my code on here to make it look like it does on the server, so I apologize for the confusing way it looks.
#include <stdio.h> //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);
}
The function scanf
receives as a parameter the format it expects from you. By passing scanf("%d\n", &paycode);
you're saying "Hey, computer, read a number AND a newline, and save it in the paycode
variable". Additionally, scanf
will read and ignore white space characters.
Try changing it to scanf("%d", &paycode);
, so you the computer only reads and saves the number.
这篇关于scanf函数的行为时,换行符是格式字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!