我正在尝试获取变量:float managerTotal,hourlyTotal,commissionTotal和pieceworkerTotal来自各自的函数,以在switch语句中打印:choiceInput函数中的'q'。昨天我有很多很多错误,现在我让它们通过,但当我输入变量的输入时,它没有通过值,它们只返回0.请有人帮助解释我做错了什么。谢谢。
以下是代码:
#include<stdio.h>
#include<conio.h>
void welcome() {
printf("-----------------------------------------------\n");
printf("Welcome to the Factory A Payroll\n");
}
void choosePayroll() {
printf("-----------------------------------------------\n");
printf("Press '1' to access the Manager's payroll\n");
printf("Press '2' to access the Hourly worker's payroll\n");
printf("Press '3' to access Commission worker's payroll\n");
printf("Press '4' to access Pieceworkers's payroll\n");
printf("Press 'Q' to access Manager's payroll\n");
printf("-----------------------------------------------\n");
choiceInput();
}
float managerIntro() {
float managerTotal;
char charReturn = 0;
printf("-----------------------------------------------\n");
printf("You have selected 'Manager's payroll'\n");
printf("-----------------------------------------------\n");
printf("Please enter your fixed weekly salary: $");
scanf_s("%f", &managerTotal);
printf("-----------------------------------------------\n");
printf("You have entered $%.2f as the weekly fixed salary\n", managerTotal);
printf("-----------------------------------------------\n");
printf("Press 'R' to return back to the main menu\n\n");
scanf_s(" %c", &charReturn); // space before %c to tell scanf to skip the leading whitespace
switch (charReturn)
{
case 'r':
choosePayroll();
default:
printf("You have entered an invalid key, press any key to try again\n");
getchar();
getchar();
hourlyIntro();
}
return managerTotal;
}
float hourlyIntro() {
char charReturn = 0;
float hourlyWage = 0;
float hoursWorked = 0;
float overTimeHours = 0;
float normalPay = 0;
float overTimePay = 0;
float hourlyTotal;
printf("-----------------------------------------------\n");
printf("You have selected 'Hourly worker's payroll'\n");
printf("-----------------------------------------------\n");
printf("Please enter the hourly wage for the worker: $");
scanf_s("%f", &hourlyWage);
printf("-----------------------------------------------\n");
printf("You have entered $%.2f as the hourly wage.", hourlyWage);
printf("\n-----------------------------------------------");
printf("\nPlease enter how many hours the worker has worked this week: ");
scanf_s("%f", &hoursWorked);
printf("\n-----------------------------------------------");
if (hoursWorked > 40) {
overTimeHours = hoursWorked - 40;
hoursWorked = 40;
}
normalPay = hourlyWage * hoursWorked;
overTimePay = (hourlyWage * 1.5) * overTimeHours;
hourlyTotal = normalPay + overTimeHours;
printf("\nNormal weekly pay : $%.2f\nOvertime weekly pay: $%.2f\n\nTotal weekly pay: $%.2f\n\n", normalPay, overTimePay, hourlyTotal);
printf("Press 'R' to return back to the main menu\n");
scanf_s(" %c", &charReturn); // space before %c to tell scanf to skip the leading whitespace
switch (charReturn)
{
case 'r':
choosePayroll();
default:
printf("You have entered an invalid key, press any key to try again\n");
getchar();
getchar();
hourlyIntro();
}
return hourlyTotal;
}
float commissionIntro() {
char charReturn = 0;
float basicSalary = 250;
float itemAPrice = 0;
float itemBPrice = 0;
float itemCPrice = 0;
float itemAQuantity = 0;
float itemBQuantity = 0;
float itemCQuantity = 0;
float itemACommission = 0;
float itemBCommission = 0;
float itemCCommission = 0;
float totalCommission = 0;
float commissionTotal = 0;
printf("-----------------------------------------------\n");
printf("You have selected 'Commission worker's payroll'\n");
printf("-----------------------------------------------\n");
printf("Please enter the price of Item A: $");
scanf_s("%f", &itemAPrice);
printf("-----------------------------------------------\n");
printf("Please enter the quantity of Item A sold this week: ");
scanf_s("%f", &itemAQuantity);
printf("-----------------------------------------------\n");
printf("Please enter the price of Item B: $");
scanf_s("%f", &itemBPrice);
printf("-----------------------------------------------\n");
printf("Please enter the quantity of Item B sold this week: ");
scanf_s("%f", &itemBQuantity);
printf("-----------------------------------------------\n");
printf("Please enter the price of Item C: $");
scanf_s("%f", &itemCPrice);
printf("-----------------------------------------------\n");
printf("Please enter the quantity of Item C sold this week: ");
scanf_s("%f", &itemCQuantity);
printf("\n-----------------------------------------------\n");
itemACommission = itemAQuantity * (itemAPrice * 0.057);
itemBCommission = itemBQuantity * (itemBPrice * 0.064);
itemCCommission = itemCQuantity * (itemCPrice * 0.072);
totalCommission = itemACommission + itemBCommission + itemCCommission;
commissionTotal = totalCommission + basicSalary;
printf("Commission for item A: $%.2f\n", itemACommission);
printf("Commission for item B: $%.2f\n", itemBCommission);
printf("Commission for item C: $%.2f\n", itemCCommission);
printf("Total commission: $%.2f\n", totalCommission);
printf("Total pay: $%.2f\n", commissionTotal);
printf("-----------------------------------------------\n");
printf("Press 'R' to return back to the main menu\n\n");;
scanf_s(" %c", &charReturn); // space before %c to tell scanf to skip the leading whitespace
switch (charReturn)
{
case 'r':
choosePayroll();
default:
printf("You have entered an invalid key, press any key to try again\n");
getchar();
getchar();
hourlyIntro();
}
return commissionTotal;
}
float pieceworkerIntro() {
char charReturn;
float quantityItem1;
float quantityItem2;
float quantityItem3;
float priceItem1 = 22.50;
float priceItem2 = 24.50;
float priceItem3 = 26;
float payItem1;
float payItem2;
float payItem3;
float pieceworkerTotal;
printf("-----------------------------------------------\n");
printf("You have selected 'Pieceworker's payroll'\n");
printf("-----------------------------------------------\n");
printf("Please enter the quantity of Item 1 produced this week: ");
scanf_s("%f", &quantityItem1);
printf("-----------------------------------------------\n");
printf("Please enter the quantity of Item 2 produced this week: ");
scanf_s("%f", &quantityItem2);
printf("-----------------------------------------------\n");
printf("Please enter the quantity of Item 3 produced this week: ");
scanf_s("%f", &quantityItem3);
printf("-----------------------------------------------\n");
payItem1 = quantityItem1 * priceItem1;
payItem2 = quantityItem2 * priceItem2;
payItem3 = quantityItem3 * priceItem3;
pieceworkerTotal = payItem1 + payItem2 + payItem3;
printf("\nItem 1 pay: $%.2f\nItem 2 pay: $%.2f\nItem 3 pay: $%.2f\n\nTotal pay: $%.2f", payItem1, payItem2, payItem3);
printf("Press 'R' to return back to the main menu\n\n");
scanf_s(" %c", &charReturn); // space before %c to tell scanf to skip the leading whitespace
switch (charReturn)
{
case 'r':
choosePayroll();
default:
printf("You have entered an invalid key, press any key to try again\n");
getchar();
getchar();
hourlyIntro();
}
}
void returnMenu() {
}
float choiceInput(float *managerTotal, float *hourlyTotal, float
*commissionTotal, float *pieceworkerTotal) {
char userChoice = 0;
scanf_s(" %c", &userChoice);
do {
switch (userChoice) {
case '1':
managerIntro();
break;
case '2':
hourlyIntro();
break;
case '3':
commissionIntro();
break;
case '4':
pieceworkerIntro();
break;
case 'q':
printf("Manager total is: $%.2f", &managerTotal);
printf("Hourly total is: $%.2f", &hourlyTotal);
printf("Commission total is: $%.2f", &commissionTotal);
printf("Pieceworker total is: $%.2f", &pieceworkerTotal);
exit(0);
default:
printf("You have entered an invalid key, please try again\n");
getchar();
getchar();
choosePayroll();
break;
}
} while (userChoice != 'Q');
}
int main(void) {
welcome();
choosePayroll();
return 0;
}
注意:我向它道歉很长的代码,但我保持这种方式的原因是因为我最后一次缩短它并没有最终为我的实际代码工作,只有我发布的短代码示例
答案 0 :(得分:0)
我在您的代码中进行了更改。检查一下,如果这个改变不符合你的要求,请告诉我。
#include <stdio.h>
float choiceInput();
void welcome();
void choosePayroll();
float managerIntro();
float hourlyIntro();
float commissionIntro();
float pieceworkerIntro();
float choiceInput();
int main()
{
welcome();
choosePayroll();
return 0;
}
void welcome()
{
printf("-----------------------------------------------\n");
printf("Welcome to the Factory A Payroll\n");
}
void choosePayroll()
{
printf("-----------------------------------------------\n");
printf("Press '1' to access the Manager's payroll\n");
printf("Press '2' to access the Hourly worker's payroll\n");
printf("Press '3' to access Commission worker's payroll\n");
printf("Press '4' to access Pieceworkers's payroll\n");
printf("Press 'Q' to access Manager's payroll\n");
printf("-----------------------------------------------\n");
choiceInput();
}
float managerIntro()
{
float managerTotal;
char charReturn = 0;
printf("-----------------------------------------------\n");
printf("You have selected 'Manager's payroll'\n");
printf("-----------------------------------------------\n");
printf("Please enter your fixed weekly salary: $");
scanf("%f", &managerTotal);
printf("-----------------------------------------------\n");
printf("You have entered $%.2f as the weekly fixed salary\n", managerTotal);
printf("-----------------------------------------------\n");
printf("Press 'R' to return back to the main menu\n\n");
scanf(" %c", &charReturn); // space before %c to tell scanf to skip the leading whitespace
switch (charReturn)
{
case 'r':
choosePayroll();
default:
printf("You have entered an invalid key, press any key to try again\n");
getchar();
getchar();
hourlyIntro();
}
return managerTotal;
}
float hourlyIntro()
{
char charReturn = 0;
float hourlyWage = 0;
float hoursWorked = 0;
float overTimeHours = 0;
float normalPay = 0;
float overTimePay = 0;
float hourlyTotal;
printf("-----------------------------------------------\n");
printf("You have selected 'Hourly worker's payroll'\n");
printf("-----------------------------------------------\n");
printf("Please enter the hourly wage for the worker: $");
scanf("%f", &hourlyWage);
printf("-----------------------------------------------\n");
printf("You have entered $%.2f as the hourly wage.", hourlyWage);
printf("\n-----------------------------------------------");
printf("\nPlease enter how many hours the worker has worked this week: ");
scanf("%f", &hoursWorked);
printf("\n-----------------------------------------------");
if (hoursWorked > 40) {
overTimeHours = hoursWorked - 40;
hoursWorked = 40;
}
normalPay = hourlyWage * hoursWorked;
overTimePay = (hourlyWage * 1.5) * overTimeHours;
hourlyTotal = normalPay + overTimeHours;
printf("\nNormal weekly pay : $%.2f\nOvertime weekly pay: $%.2f\n\nTotal weekly pay: $%.2f\n\n", normalPay, overTimePay, hourlyTotal);
printf("Press 'R' to return back to the main menu\n");
scanf(" %c", &charReturn); // space before %c to tell scanf to skip the leading whitespace
switch (charReturn)
{
case 'r':
choosePayroll();
default:
printf("You have entered an invalid key, press any key to try again\n");
getchar();
getchar();
hourlyIntro();
}
return hourlyTotal;
}
float commissionIntro() {
char charReturn = 0;
float basicSalary = 250;
float itemAPrice = 0;
float itemBPrice = 0;
float itemCPrice = 0;
float itemAQuantity = 0;
float itemBQuantity = 0;
float itemCQuantity = 0;
float itemACommission = 0;
float itemBCommission = 0;
float itemCCommission = 0;
float totalCommission = 0;
float commissionTotal = 0;
printf("-----------------------------------------------\n");
printf("You have selected 'Commission worker's payroll'\n");
printf("-----------------------------------------------\n");
printf("Please enter the price of Item A: $");
scanf("%f", &itemAPrice);
printf("-----------------------------------------------\n");
printf("Please enter the quantity of Item A sold this week: ");
scanf("%f", &itemAQuantity);
printf("-----------------------------------------------\n");
printf("Please enter the price of Item B: $");
scanf("%f", &itemBPrice);
printf("-----------------------------------------------\n");
printf("Please enter the quantity of Item B sold this week: ");
scanf("%f", &itemBQuantity);
printf("-----------------------------------------------\n");
printf("Please enter the price of Item C: $");
scanf("%f", &itemCPrice);
printf("-----------------------------------------------\n");
printf("Please enter the quantity of Item C sold this week: ");
scanf("%f", &itemCQuantity);
printf("\n-----------------------------------------------\n");
itemACommission = itemAQuantity * (itemAPrice * 0.057);
itemBCommission = itemBQuantity * (itemBPrice * 0.064);
itemCCommission = itemCQuantity * (itemCPrice * 0.072);
totalCommission = itemACommission + itemBCommission + itemCCommission;
commissionTotal = totalCommission + basicSalary;
printf("Commission for item A: $%.2f\n", itemACommission);
printf("Commission for item B: $%.2f\n", itemBCommission);
printf("Commission for item C: $%.2f\n", itemCCommission);
printf("Total commission: $%.2f\n", totalCommission);
printf("Total pay: $%.2f\n", commissionTotal);
printf("-----------------------------------------------\n");
printf("Press 'R' to return back to the main menu\n\n");;
scanf(" %c", &charReturn); // space before %c to tell scanf to skip the leading whitespace
switch (charReturn)
{
case 'r':
choosePayroll();
default:
printf("You have entered an invalid key, press any key to try again\n");
getchar();
getchar();
hourlyIntro();
}
return commissionTotal;
}
float pieceworkerIntro() {
char charReturn;
float quantityItem1;
float quantityItem2;
float quantityItem3;
float priceItem1 = 22.50;
float priceItem2 = 24.50;
float priceItem3 = 26;
float payItem1;
float payItem2;
float payItem3;
float pieceworkerTotal;
printf("-----------------------------------------------\n");
printf("You have selected 'Pieceworker's payroll'\n");
printf("-----------------------------------------------\n");
printf("Please enter the quantity of Item 1 produced this week: ");
scanf("%f", &quantityItem1);
printf("-----------------------------------------------\n");
printf("Please enter the quantity of Item 2 produced this week: ");
scanf("%f", &quantityItem2);
printf("-----------------------------------------------\n");
printf("Please enter the quantity of Item 3 produced this week: ");
scanf("%f", &quantityItem3);
printf("-----------------------------------------------\n");
payItem1 = quantityItem1 * priceItem1;
payItem2 = quantityItem2 * priceItem2;
payItem3 = quantityItem3 * priceItem3;
pieceworkerTotal = payItem1 + payItem2 + payItem3;
printf("\nItem 1 pay: $%.2f\nItem 2 pay: $%.2f\nItem 3 pay: $%.2f\n\nTotal pay: $%.2f", payItem1, payItem2, payItem3);
printf("Press 'R' to return back to the main menu\n\n");
scanf(" %c", &charReturn); // space before %c to tell scanf to skip the leading whitespace
switch (charReturn)
{
case 'r':
choosePayroll();
default:
printf("You have entered an invalid key, press any key to try again\n");
getchar();
getchar();
hourlyIntro();
}
}
float choiceInput()
{
char userChoice;
float managerTotal;
float hourlyTotal;
float commissionTotal;
float pieceworkerTotal;
scanf(" %c", &userChoice);
do {
switch (userChoice) {
case '1':
managerTotal = managerIntro();
break;
case '2':
hourlyTotal = hourlyIntro();
break;
case '3':
commissionTotal = commissionIntro();
break;
case '4':
pieceworkerTotal = pieceworkerIntro();
break;
case 'q':
printf("Manager total is: $%.2f", managerTotal);
printf("Hourly total is: $%.2f", hourlyTotal);
printf("Commission total is: $%.2f", commissionTotal);
printf("Pieceworker total is: $%.2f", pieceworkerTotal);
return 0;
default:
printf("You have entered an invalid key, please try again\n");
getchar();
getchar();
choosePayroll();
break;
}
} while (userChoice != 'Q');
}