C++ 期末考试有困难。代码不断循环回到我的订购菜单,不会显示我的总价
我对编程非常陌生。在期末考试中,我被告知制作一个包含if-else、whiledo和数组的程序。我真的很困惑,为什么在我的程序中,当我必须计算价格时,它没有显示出来,只是循环回我的订购代码。有什么想法吗C++ 期末考试有困难。代码不断循环回到我的订购菜单,不会显示我的总价,c++,C++,我对编程非常陌生。在期末考试中,我被告知制作一个包含if-else、whiledo和数组的程序。我真的很困惑,为什么在我的程序中,当我必须计算价格时,它没有显示出来,只是循环回我的订购代码。有什么想法吗 cout <<"COKE \t\t\t-Php 30 \t" ; cout << setw (50) <<" Carbonated softdrink by Coca-Cola Company i
cout <<"COKE \t\t\t-Php 30 \t" ;
cout << setw (50) <<" Carbonated softdrink by Coca-Cola Company in 2.5L."<<endl;
cout <<"SPRITE \t\t-Php 30 \t";
cout<< setw(50) <<" A colorless, lemon and lime-flavored soft drink in 2.5L."<<endl;
cout <<"ICED TEA\t\t-Php 30 \t ";
cout <<setw (20) <<"Houseblend iced tea in 1L pitcher."<<endl;
cout <<"COFFEE \t\t\t-Php 25 \t";
cout << setw (55) << " A brewed drink prepared from roasted coffee beans in a cup."<<endl;
cout <<"BOTTLED WATER \t\t-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) << "DRINKS\n" <<endl;
cout <<"Hotsilog \t-Php 45 \t" <<setw(15) <<"Coke \t\t\t-Php 30" <<endl;
cout <<"Tocilog \t-Php 45 \t" << setw(15) <<"Sprite \t\t-Php 30" <<endl;
cout <<"Tapsilog \t-Php 45 \t" << setw(15) <<"Iced Tea \t\t-Php 30" << endl;
cout <<"Porksilog \t-Php 50 \t" << setw(15) <<"Coffee \t\t\t-Php 25" <<endl;
cout <<"Chickensilog \t-Php 55 \t" <<setw (15) <<"Bottled Water \t\t-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.\n\n";
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;
}
cout问题如下:
while(ow!=0){
系统(“cls”);
在while
循环的最后,您计算并打印totalprice
。然后它循环,因为您没有更改while
循环中ow
的值,它仍然不等于0
,因此,您进入了一个无限循环。您没有看到totalprice
打印的原因是什么因为当你循环时,你会立即调用系统(“cls”)
从屏幕上删除totalprice
。函数是您最好的朋友,在提高文本可读性方面,变量名也非常重要。我对您的代码进行了一些修改,以便您可以观察到它有多大的不同:
#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.\n\n";
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 " << "DRINKS\n" <<endl;
cout <<"Hotsilog \t-Php 45 \t" <<"Coke \t\t\t-Php 30" <<endl;
cout <<"Tocilog \t-Php 45 \t" <<"Sprite \t\t-Php 30" <<endl;
cout <<"Tapsilog \t-Php 45 \t" <<"Iced Tea \t\t-Php 30" << endl;
cout <<"Porksilog \t-Php 50 \t" <<"Coffee \t\t\t-Php 25" <<endl;
cout <<"Chickensilog \t-Php 55 \t" <<"Bottled Water \t\t-Php 20" << endl;
}
void display_main_menu() {
cout <<"COKE \t\t\t-Php 30 \t" ;
cout <<" Carbonated softdrink by Coca-Cola Company in 2.5L."<<endl;
cout <<"SPRITE \t\t-Php 30 \t";
cout <<" A colorless, lemon and lime-flavored soft drink in 2.5L."<<endl;
cout <<"ICED TEA\t\t-Php 30 \t ";
cout <<"Houseblend iced tea in 1L pitcher."<<endl;
cout <<"COFFEE \t\t\t-Php 25 \t";
cout << " A brewed drink prepared from roasted coffee beans in a cup."<<endl;
cout <<"BOTTLED WATER \t\t-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: ";
}
#包括
使用名称空间std;
无效显示主菜单();
无效显示标题();
int main(){
int选择的_选项;
显示主菜单();
cin>>选择的_选项;
如果(所选选项==1){
while(选中的_选项!=0){
弦积[50];
弦乐[50];
整数数量[10];
国际价格[10];
INTE;
int totalprice=0;
库特;
下一步:学习,如何调试程序;-,欢迎来到堆栈溢出。请阅读,采取,阅读,以及。最后,请学习如何创建一个,重点放在最小的部分。我与一些。但不要过分强调“最小”,我明确希望它也是可复制的。