我試圖創建一個程序來計算所選食物的價格,同時顯示總價格。我已經可以計算並顯示總價格。我的問題是每當用戶輸入「[3] exit」並且他選擇「[1]復位」時,程序就會中斷並檢測到很多錯誤。我想知道應該在我的程序中使用更好的聲明。謝謝!製作計算器時比if語句有什麼更好
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);
}
正如haccks已經提到的那樣,switch case或if-else更適合您的應用程序。但是,「程序是否打破並檢測錯誤」是什麼意思?我得到的印象是,您有另一個與用戶選擇評估無關的問題。 – jwsc
以及即時通訊問題,如果語句中的if語句突破並返回到我的主頁。 @ jwsc – alpha
發佈您遇到問題的實際代碼,否則我們只是在猜測。 – Samidamaru