gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 08:35:40 28 4
gpt4 key购买 nike

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

#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);
}

最佳答案

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

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

你应该改变你的 scanf拨电至
scanf("%d", &paycode);

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

关于c - 换行符在格式字符串中时 scanf 的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21859277/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com