这里的第一次海报。我现在正在学校学习C编程课程,过去只学过编程必需品。我一直在努力,但不知何故设法完成了第5周的所有事情。
基本上,这是我到目前为止所拥有的。我想要做的是让你的程序在你运行一次之后回收到菜单选择屏幕。我相信我需要把所有东西都放在while循环中,但是我已经尝试添加它,但所有发生的事情是我最终得到程序要求我输入我已经选择的选择的小时数。我正在使用Microsoft Visual Studio Express 2015,如果这有用的话。
感谢您提供任何帮助/解释。
#include <stdio.h>
#include <iostream>
#define BASEPAY1 8.75
#define BASEPAY2 9.33
#define BASEPAY3 10
#define BASEPAY4 11.20
#define BASEHRS 40
#define OVERTIME 1.5
#define AMT1 300
#define AMT2 150
#define RATE1 0.15
#define RATE2 0.20
#define RATE3 0.25
int main(void)
{
int hours;
float gross;
float net;
float taxes;
int choice;
printf("\n\nMenu\n----------\n\n");
printf("1) $8.75/hr\n");
printf("2) $9.33/hr\n");
printf("3) $10.00/hr\n");
printf("4) $11.20/hr\n");
printf("5) quit\n\n");
printf("Enter the number that corresponds to your rate or press 5 to quit.\n");
scanf_s("%d", &choice);
switch (choice) {
case 1:
printf("Enter the number of hours worked this week: \n");
scanf_s("%d", &hours);
if (hours <= BASEHRS)
gross = hours * BASEPAY1;
else
gross = BASEHRS * BASEPAY1 + (hours - BASEHRS) * BASEPAY1 * OVERTIME;
if (gross <= AMT1)
taxes = gross * RATE1;
else if (gross <= AMT1 + AMT2)
taxes = AMT1 * RATE1 + (gross - AMT1) * RATE2;
else
taxes = AMT1 * RATE1 + AMT2 * RATE2 + (gross - AMT1 - AMT2) * RATE3;
net = gross - taxes;
printf("gross: $%.2f \n", gross);
printf("taxes: $%.2f \n", taxes);
printf("net: $%.2f \n", net);
break;
case 2:
printf("Enter the number of hours worked this week: ");
scanf_s("%d", &hours);
if (hours <= BASEHRS)
gross = hours * BASEPAY2;
else
gross = BASEHRS * BASEPAY2 + (hours - BASEHRS) * BASEPAY2 * OVERTIME;
if (gross <= AMT1)
taxes = gross * RATE1;
else if (gross <= AMT1 + AMT2)
taxes = AMT1 * RATE1 + (gross - AMT1) * RATE2;
else
taxes = AMT1 * RATE1 + AMT2 * RATE2 + (gross - AMT1 - AMT2) * RATE3;
net = gross - taxes;
printf("gross: $%.2f \n", gross);
printf("taxes: $%.2f \n", taxes);
printf("net: $%.2f \n", net);
break;
case 3:
printf("Enter the number of hours worked this week: ");
scanf_s("%d", &hours);
if (hours <= BASEHRS)
gross = hours * BASEPAY3;
else
gross = BASEHRS * BASEPAY3 + (hours - BASEHRS) * BASEPAY3 * OVERTIME;
if (gross <= AMT1)
taxes = gross * RATE1;
else if (gross <= AMT1 + AMT2)
taxes = AMT1 * RATE1 + (gross - AMT1) * RATE2;
else
taxes = AMT1 * RATE1 + AMT2 * RATE2 + (gross - AMT1 - AMT2) * RATE3;
net = gross - taxes;
printf("gross: $%.2f \n", gross);
printf("taxes: $%.2f \n", taxes);
printf("net: $%.2f \n", net);
break;
case 4:
printf("Enter the number of hours worked this week: ");
scanf_s("%d", &hours);
if (hours <= BASEHRS)
gross = hours * BASEPAY4;
else
gross = BASEHRS * BASEPAY4 + (hours - BASEHRS) * BASEPAY4 * OVERTIME;
if (gross <= AMT1)
taxes = gross * RATE1;
else if (gross <= AMT1 + AMT2)
taxes = AMT1 * RATE1 + (gross - AMT1) * RATE2;
else
taxes = AMT1 * RATE1 + AMT2 * RATE2 + (gross - AMT1 - AMT2) * RATE3;
net = gross - taxes;
printf("gross: $%.2f \n", gross);
printf("taxes: $%.2f \n", taxes);
printf("net: $%.2f \n", net);
break;
case 5:
return 0;
default:
printf("Invalid choice, please enter the number that corresponds ");
printf("to your rate or press 5 to quit.\n\n");
}
system("PAUSE");
return 0;
}
答案 0 :(得分:2)
您需要在每个步骤中更新choice
的值。所以,我会编写类似的代码:
int main(void)
{
// some code
int choice;
bool loop = true;
while(loop)
{
printf("\n\nMenu\n----------\n\n");
// print the menu
printf("Enter the number that corresponds to your rate or press 5 to quit.\n");
scanf_s("%d", &choice);
switch (choice) {
// cases 1-4
case 5:
loop = false;
break;
default:
printf("Invalid choice.\n\n");
} // exit from switch
} // exit from while-loop
return 0;
}
答案 1 :(得分:0)
你的代码的问题是while循环是在提示工资率之后。所以它只执行一次提示。
只需移动线
即可while ( choice < 5) {
到行前
printf("\n\nMenu\n----------\n\n");
现在它将根据需要循环整个程序。
答案 2 :(得分:0)
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#define BASEPAY1 8.75
#define BASEPAY2 9.33
#define BASEPAY3 10
#define BASEPAY4 11.20
#define BASEHRS 40
#define OVERTIME 1.5
#define AMT1 300
#define AMT2 150
#define RATE1 0.15
#define RATE2 0.20
#define RATE3 0.25
char *szMainMenu=
"\n\nMENU:\n"
"----------\n"
"\n"
" 1) $8.75/hr\n"
" 2) $9.33/hr\n"
" 3) $10.00/hr\n"
" 4) $11.20/hr\n"
" 5) quit\n"
"\n"
"Enter the number that corresponds to your rate or press 5 to quit.\nchoice: ";
/**____________________________________________________________________________
*/
int intPrompt(const char *prompt) {
int n;
printf("%s",prompt);
scanf(" %d",&n);
return n;
}
/**____________________________________________________________________________
*/
int main(void) {
int hours;
float gross;
float net;
float taxes;
int choice;
while((choice=intPrompt(szMainMenu))!=5) {
if(choice>=1 && choice<=4)
hours=intPrompt("Enter the number of hours worked this week: ");
switch (choice) {
case 1:
if (hours <= BASEHRS)
gross = hours * BASEPAY1;
else
gross = BASEHRS * BASEPAY1 + (hours - BASEHRS) * BASEPAY1 * OVERTIME;
if (gross <= AMT1)
taxes = gross * RATE1;
else if (gross <= AMT1 + AMT2)
taxes = AMT1 * RATE1 + (gross - AMT1) * RATE2;
else
taxes = AMT1 * RATE1 + AMT2 * RATE2 + (gross - AMT1 - AMT2) * RATE3;
net = gross - taxes;
printf("gross: $%.2f \n", gross);
printf("taxes: $%.2f \n", taxes);
printf("net: $%.2f \n", net);
break;
case 2:
if (hours <= BASEHRS)
gross = hours * BASEPAY2;
else
gross = BASEHRS * BASEPAY2 + (hours - BASEHRS) * BASEPAY2 * OVERTIME;
if (gross <= AMT1)
taxes = gross * RATE1;
else if (gross <= AMT1 + AMT2)
taxes = AMT1 * RATE1 + (gross - AMT1) * RATE2;
else
taxes = AMT1 * RATE1 + AMT2 * RATE2 + (gross - AMT1 - AMT2) * RATE3;
net = gross - taxes;
printf("gross: $%.2f \n", gross);
printf("taxes: $%.2f \n", taxes);
printf("net: $%.2f \n", net);
break;
case 3:
if (hours <= BASEHRS)
gross = hours * BASEPAY3;
else
gross = BASEHRS * BASEPAY3 + (hours - BASEHRS) * BASEPAY3 * OVERTIME;
if (gross <= AMT1)
taxes = gross * RATE1;
else if (gross <= AMT1 + AMT2)
taxes = AMT1 * RATE1 + (gross - AMT1) * RATE2;
else
taxes = AMT1 * RATE1 + AMT2 * RATE2 + (gross - AMT1 - AMT2) * RATE3;
net = gross - taxes;
printf("gross: $%.2f \n", gross);
printf("taxes: $%.2f \n", taxes);
printf("net: $%.2f \n", net);
break;
case 4:
if (hours <= BASEHRS)
gross = hours * BASEPAY4;
else
gross = BASEHRS * BASEPAY4 + (hours - BASEHRS) * BASEPAY4 * OVERTIME;
if (gross <= AMT1)
taxes = gross * RATE1;
else if (gross <= AMT1 + AMT2)
taxes = AMT1 * RATE1 + (gross - AMT1) * RATE2;
else
taxes = AMT1 * RATE1 + AMT2 * RATE2 + (gross - AMT1 - AMT2) * RATE3;
net = gross - taxes;
printf("gross: $%.2f \n", gross);
printf("taxes: $%.2f \n", taxes);
printf("net: $%.2f \n", net);
break;
default:
_clrscr();
printf(
"Invalid choice, please enter the number that corresponds\n"
"to your rate or press 5 to quit.\n\n"
"press a key to continue");
_getch();
}
_clrscr();
}
system("PAUSE");
return 0;
}