I'm trying to create a program that calculates the price of the selected food while displaying the total price. I can already calculate and display the total price. My problem is whenever the user enters "[3] exit" and he chooses "[1] reset", the program breaks and detects many error. I wonder what better statement should I use with my program. Thanks!
我正在尝试创建一个程序,在显示总价时计算所选食品的价格。我已经可以计算并显示总价。我的问题是每当用户输入“[3]退出”并且他选择“[1]重置”时,程序会中断并检测到许多错误。我想知道我的程序应该使用哪种更好的声明。谢谢!
void PriceCalculator(){
system("cls");
char yn[3];
int choice;
float spinachP=7.18;
float romaineP=12.21;
float mustardP=11.18;
float carrotsP=1.6;
float potatoP=1.0;
float yamP=2.1;
float asparagusP=15.98;
float brocolliP=10.65;
float melonP=8.31;
float strawberryP=10.62;
float spinach=0;
float romaine=0;
float mustard=0;
float carrots=0;
float potato=0;
float yam=0;
float asparagus=0;
float brocolli=0;
float melon=0;
float strawberry=0;
float spinachT=0;
float romaineT=0;
float mustardT=0;
float carrotsT=0;
float potatoT=0;
float yamT=0;
float asparagusT=0;
float brocolliT=0;
float melonT=0;
float strawberryT=0;
float totalSpi=0;
float totalRom=0;
float totalMus=0;
float totalCar=0;
float totalPot=0;
float totalYam=0;
float totalAsp=0;
float totalBro=0;
float totalMel=0;
float totalStr=0;
float totalP=0;
do{
border();
gotoxy(50, 5); p("Budget Calculator");
totalP=totalSpi+totalRom+totalMus+totalCar+totalPot+totalYam+totalAsp+totalBro+totalMel+totalStr;\
gotoxy(50, 7); p("Total Price : P %.2f", totalP);
gotoxy(5, 5); p("[1] Spinach\t=\tP %.2f", totalSpi);
gotoxy(5, 6); p("[2] Romaine\t=\tP %.2f", totalRom);
gotoxy(5, 7); p("[3] Mustard\t=\tP %.2f", totalMus);
gotoxy(5, 8); p("[4] Carrots\t=\tP %.2f", totalCar);
gotoxy(5, 9); p("[5] Potato\t\t=\tP %.2f", totalPot);
gotoxy(5, 10); p("[6] Yam\t\t=\tP %.2f", totalYam);
gotoxy(5, 11); p("[7] Asparagus\t=\tP %.2f", totalAsp);
gotoxy(5, 12); p("[8] Brocolli\t=\tP %.2f", totalBro);
gotoxy(5, 13); p("[9] Melon\t\t=\tP %.2f", totalMel);
gotoxy(5, 14); p("[10] Strawberry\t=\tP %.2f", totalStr);
gotoxy(5, 15); p("[11] Exit");
gotoxy(5, 17); p("Enter Choice: ");
s("%d",&choice);
if(choice == 1){
gotoxy(5, 19); p("How many grams of Spinach? ");
s("%f",&spinach);
if(spinach < 0){
gotoxy(5, 20); p("Error! Invalid input.");
}else{
spinachT=(spinach/20)*spinachP;
totalSpi += spinachT;
gotoxy(5, 20); p("Price: P %.2f",spinachT);
gotoxy(5, 21); p("Enter again (y/n)?");
s("%s", &yn);
if(strcmp(yn, "y")==0){
system("cls");
}else if(strcmp(yn, "n")){
break;
}
}
}
else if(choice == 2){
gotoxy(5, 19); p("How many grams of Romain Lettuce? ");
s("%f",&romaine);
if(romaine < 0){
gotoxy(5, 20); p("Error! Invalid input.");
}else{
romaineT=(romaine/20)*romaineP;
totalRom += romaineT;
gotoxy(5, 20); p("Price: P %.2f",romaineT);
gotoxy(5, 21); p("Enter again (y/n)?");
s("%s", &yn);
if(strcmp(yn, "y")==0){
system("cls");
}else if(strcmp(yn, "n")){
break;
}
}
}
else if(choice == 3){
gotoxy(5, 19); p("How many grams of Mustard Greens? ");
s("%f",&mustard);
if(mustard < 0){
gotoxy(5, 20); p("Error! Invalid input.");
}else{
mustardT=(mustard/20)*mustardP;
totalMus += mustardT;
gotoxy(5, 20); p("Price: P %.2f",mustardT);
}
}
else if(choice == 4){
gotoxy(5, 19); p("How many grams of Carrots? ");
s("%f",&carrots);
if(carrots < 0){
gotoxy(5, 20); p("Error! Invalid input.");
}else{
carrotsT=(carrots/20)*carrotsP;
totalCar += carrotsT;
gotoxy(5, 20); p("Price: P %.2f",carrotsT);
}
}
else if(choice == 5){
gotoxy(5, 19); p("How many grams of Potato? ");
s("%f",&potato);
if(potato < 0){
gotoxy(5, 20); p("Error! Invalid input.");
}else{
potatoT=(potato/20)*potatoP;
totalPot += potatoT;
gotoxy(5, 20); p("Price: P %.2f",potatoT);
}
}
else if(choice == 6){
gotoxy(5, 19); p("How many grams of Yam? ");
s("%f",&yam);
if(yam < 0){
gotoxy(5, 20); p("Error! Invalid input.");
}else{
yamT=(yam/20)*yamP;
totalPot += yamT;
gotoxy(5, 20); p("Price: P %.2f",yamT);
}
}
else if(choice == 7){
gotoxy(5, 19); p("How many grams of Asparagus? ");
s("%f",&asparagus);
if(asparagus < 0){
gotoxy(5, 20); p("Error! Invalid input.");
}else{
asparagusT=(asparagus/20)*asparagusP;
totalAsp += asparagusT;
gotoxy(5, 20); p("Price: P %.2f",asparagusT);
}
}
else if(choice == 8){
gotoxy(5, 19); p("How many grams of Brocolli? ");
s("%f",&brocolli);
if(brocolli < 0){
gotoxy(5, 20); p("Error! Invalid input.");
}else{
brocolliT=(brocolli/20)*brocolliP;
totalBro += brocolliT;
gotoxy(5, 20); p("Price: P %.2f",brocolliT);
}
}
else if(choice == 9){
gotoxy(5, 19); p("How many grams of Melon? ");
s("%f",&melon);
if(melon < 0){
gotoxy(5, 20); p("Error! Invalid input.");
}else{
melonT=(melon/20)*melonP;
totalMel += melonT;
gotoxy(5, 20); p("Price: P %.2f",melonT);
}
}
else if(choice == 10){
gotoxy(5, 19); p("How many grams of Strawberry? ");
s("%f",&strawberry);
if(strawberry < 0){
gotoxy(5, 20); p("Error! Invalid input.");
}else{
strawberryT=(strawberry/20)*strawberryP;
totalStr += strawberryT;
gotoxy(5, 20); p("Price: P %.2f",strawberryT);
}
}
else if(choice == 11){
gotoxy(5, 19); p("[1] Reset [2] Back to Home");
gotoxy(5, 20); p("Enter choice: ");
s("%d", &choice);
if(choice = 1){
totalP=0;
spinachT=0;
romaineT=0;
mustardT=0;
carrotsT=0;
potatoT=0;
yamT=0;
asparagusT=0;
brocolliT=0;
melonT=0;
strawberryT=0;
totalSpi=0;
totalRom=0;
totalMus=0;
totalCar=0;
totalPot=0;
totalYam=0;
totalAsp=0;
totalBro=0;
totalMel=0;
totalStr=0;
system("cls");
}
else if(choice == 2){
break;
}
}
if(choice < 1 || choice > 12){
gotoxy(5, 19); p("Error! Please choose between 1-10.");
}
}while(choice != 0);
}
2 个解决方案
#1
1
Either use if-else
or switch
statement. Otherwise, each and every if
condition will be evaluated and checked as per your snippet.
使用if-else或switch语句。否则,将根据您的代码段评估和检查每个if条件。
#2
1
You have a typo in your code:
您的代码中有拼写错误:
else if(choice == 11){
// some code...
if(choice = 1){ // you miss a "=" here!!!
// more code...
}
else if(choice == 2){
break;
}
}
Results in this:
结果如下:
if(choice = 1)
This will assign choice to 1 and after that it will check if choice is bigger than 0. This will be true all the time. the next branch
这会将选项分配给1,之后它将检查选项是否大于0.这将始终为真。下一个分支
else if(choice == 2)
will never be executed.
将永远不会被执行。
PS: if you ask a question next time, Please include the correct and full description of the problem.
PS:如果你下次问一个问题,请包括问题的正确和完整描述。
the program breaks and detects many error
程序中断并检测到许多错误
Is no description of a problem. Also, If you would have debugged the lines, or included some trace messages, you would have noticed immediately that your program flow is not as expected.
没有问题的描述。此外,如果您已调试行或包含一些跟踪消息,您会立即注意到您的程序流程不符合预期。
#1
1
Either use if-else
or switch
statement. Otherwise, each and every if
condition will be evaluated and checked as per your snippet.
使用if-else或switch语句。否则,将根据您的代码段评估和检查每个if条件。
#2
1
You have a typo in your code:
您的代码中有拼写错误:
else if(choice == 11){
// some code...
if(choice = 1){ // you miss a "=" here!!!
// more code...
}
else if(choice == 2){
break;
}
}
Results in this:
结果如下:
if(choice = 1)
This will assign choice to 1 and after that it will check if choice is bigger than 0. This will be true all the time. the next branch
这会将选项分配给1,之后它将检查选项是否大于0.这将始终为真。下一个分支
else if(choice == 2)
will never be executed.
将永远不会被执行。
PS: if you ask a question next time, Please include the correct and full description of the problem.
PS:如果你下次问一个问题,请包括问题的正确和完整描述。
the program breaks and detects many error
程序中断并检测到许多错误
Is no description of a problem. Also, If you would have debugged the lines, or included some trace messages, you would have noticed immediately that your program flow is not as expected.
没有问题的描述。此外,如果您已调试行或包含一些跟踪消息,您会立即注意到您的程序流程不符合预期。