我的期末考试遇到了麻烦.代码不断循环回到我的订购菜单,不会显示我的总价



我对编程非常陌生。在我的期末考试中,我被告知要制作一个由if-else-with-do和数组组成的程序。我真的很困惑,为什么在我的程序中,当我必须计算价格时,它没有显示,而是循环回我的订购代码。有什么想法吗?

cout <<"COKE ttt-Php 30 t" ;
cout << setw (50) <<" Carbonated softdrink by Coca-Cola Company in 2.5L."<<endl;
cout <<"SPRITE  tt-Php 30 t";
cout<< setw(50) <<" A colorless, lemon and lime-flavored soft drink in 2.5L."<<endl;
cout <<"ICED TEAtt-Php 30 t ";
cout <<setw (20) <<"Houseblend iced tea in 1L pitcher."<<endl;
cout <<"COFFEE ttt-Php 25 t";
cout << setw (55) << " A brewed drink prepared from roasted coffee beans in a cup."<<endl;
cout <<"BOTTLED WATER tt-Php 20 t";
cout <<setw (25) <<" A purified drinking water in 500mL." <<endl;
cout <<"-----------------------------------------------------------------------------------------------------------------------" <<endl <<endl;
cout <<"1 t Order Now" <<endl;
cout <<"0 t Back" <<endl <<endl;
cout <<"Enter the number of your choice: ";
cin >> ow;
if (ow==1){
history[length] = "Order";
length += 1;
while(ow!=0){
system ("cls");
cout <<"nRICE MEAL " <<setw (30) << "DRINKSn" <<endl;
cout <<"Hotsilog t-Php 45 t" <<setw(15) <<"Coke ttt-Php 30" <<endl;
cout <<"Tocilog t-Php 45 t" << setw(15) <<"Sprite  tt-Php 30" <<endl;
cout <<"Tapsilog t-Php 45 t" << setw(15) <<"Iced Tea tt-Php 30" << endl;
cout <<"Porksilog t-Php 50 t" << setw(15) <<"Coffee ttt-Php 25" <<endl;
cout <<"Chickensilog t-Php 55 t" <<setw (15) <<"Bottled Water tt-Php 20" << endl;
string product [50];
string note [50];
int qty [10];
int price [10];
int e;
int totalprice = 0;
cout <<"nNumber of Product Name you will need: ";
cin>> e;
cin.ignore();
cout <<"nFill in the order form based on above.n";
cout <<"Orders not mention above will be disregarded.nn";
for (int i=0; i<e; i++){
cout <<"Enter the 'Name' of the product you will buy: ";
getline (cin, product[i]);
cout <<"Additional Note (Press Enter if no additional note): ";
getline (cin, note [i]);
cout <<"Quantity: ";
cin >> qty[i] ;
cout <<"Price: ";
cin >> price[i];
cin.ignore();
system ("cls");
}
// system ("cls");
cout <<"Your Product's List " <<endl << endl;
for (int j=0; j<e; j++){
cout <<"Product Name: " << product [j] <<endl;
cout <<"Quantity:" << qty[j]<< endl;
cout <<"Price: "<< price [j] << endl <<endl;
totalprice = totalprice + (qty [j]*price[j]);  
cout << "Total Price: " <<totalprice;
} \This is the line that I am having trouble with, it won't show up when I enter the number of food I input.

}   
}
}
}
}
return 0;
}

任何帮助都将不胜感激!

函数是您最好的朋友,在提高文本可读性方面,变量名也非常重要。我稍微修改了你的代码,这样你就可以观察到它的巨大区别:

#include <iostream>
using namespace std;
void display_main_menu();
void display_header();
int main() {
int selected_option;
display_main_menu();
cin >> selected_option;
if (selected_option == 1) {
while(selected_option != 0) {
string product [50];
string note [50];
int qty [10];
int price [10];
int e;
int totalprice = 0;
cout <<"nNumber of Product Name you will need: ";
cin >> e;
cout <<"nFill in the order form based on above.n";
cout <<"Orders not mention above will be disregarded.nn";
for (int counter = 0; counter < e; counter++){
cout <<"Enter the 'Name' of the product you will buy: ";
getline (cin, product[counter]);
cout <<"Additional Note (Press Enter if no additional note): ";
getline (cin, note[counter]);
cout <<"Quantity: ";
cin >> qty[counter] ;
cout <<"Price: ";
cin >> price[counter];
system ("cls");
}
cout <<"Your Product's List " << endl << endl;
for (int counter = 0; counter < e; counter++){
cout <<"Product Name: " << product[counter] <<endl;
cout <<"Quantity:" << qty[counter]<< endl;
cout <<"Price: "<< price[counter] << endl <<endl;
totalprice += (qty[counter] * price[counter]);  
cout << "Total Price: " << totalprice;
}
cin >> selected_option;
system ("cls");
}   
}
return 0;
}
void display_header() {
system ("cls");
cout <<"nRICE MEAL " << "DRINKSn" <<endl;
cout <<"Hotsilog t-Php 45 t" <<"Coke ttt-Php 30" <<endl;
cout <<"Tocilog t-Php 45 t" <<"Sprite  tt-Php 30" <<endl;
cout <<"Tapsilog t-Php 45 t" <<"Iced Tea tt-Php 30" << endl;
cout <<"Porksilog t-Php 50 t" <<"Coffee ttt-Php 25" <<endl;
cout <<"Chickensilog t-Php 55 t" <<"Bottled Water tt-Php 20" << endl;
}
void display_main_menu() {
cout <<"COKE ttt-Php 30 t" ;
cout <<" Carbonated softdrink by Coca-Cola Company in 2.5L."<<endl;
cout <<"SPRITE  tt-Php 30 t";
cout <<" A colorless, lemon and lime-flavored soft drink in 2.5L."<<endl;
cout <<"ICED TEAtt-Php 30 t ";
cout <<"Houseblend iced tea in 1L pitcher."<<endl;
cout <<"COFFEE ttt-Php 25 t";
cout << " A brewed drink prepared from roasted coffee beans in a cup."<<endl;
cout <<"BOTTLED WATER tt-Php 20 t";
cout <<" A purified drinking water in 500mL." <<endl;
cout <<"-----------------------------------------------------------------------------------------------------------------------" <<endl <<endl;
cout <<"1 t Order Now" <<endl;
cout <<"0 t Back" <<endl <<endl;
cout <<"Enter the number of your choice: ";
}

你可以将其进一步划分为单独的函数,我这样做不是为了不让指针吓到你。

问题如下:

while (ow != 0) {
system ("cls");

while循环结束时,计算并打印totalprice。之后它循环,因为你没有改变while循环中ow的值,它仍然不等于0,因此,你进入了一个无限循环。你看不到totalprice打印出来的原因是,当你循环时,你会立即调用system ("cls"),从屏幕上删除totalprice

最新更新