我的 while 循环中的 if 语句在不应该出现的情况下不断重复。它应该在 if 状态之后返回到 while 循环

黑暗赛弗

所以我正在为学校做一个项目,但我无法让它发挥作用。其他一切似乎都在运行,但是当我输入用户对 q、s、z10、z25、z5、z1 的选择时,它会无限循环。我无法弄清楚为什么它会一遍又一遍地运行该功能。我不太确定我只是输入错误还是什么,但这是我目前唯一真正坚持的事情。这是代码

#include<iostream>
#include<string>
using namespace std;

struct Inventory
{
int Pack_25oz;
int Pack_10oz;
int Pack_5oz;
int Pack_1oz;
};
void show_Menu();
void initialize (Inventory& I);
void add_25OZ (Inventory& I, int P25);
void add_10OZ (Inventory& I, int P10);
void add_5OZ (Inventory& I, int P5);
void add_1OZ (Inventory& I, int P1);
void Place_Order(Inventory& I, int amount);
void show (Inventory& I);

int main(){
Inventory I;
string choice;
int nop= 0;
 initialize(I);
 show_Menu();
cout << "Dear User! please make a choice from above menu : " << endl;
getline(cin,choice);
while(choice!="q" or choice != "Q"){

if(choice ==  "s"|| choice == "S"){
show(I);

}

else if(choice == "z25" || choice == "Z25"){
    cout << "Enter the number of packages : \n";
    cin  >> nop;
     add_25OZ(I, nop);
}


else if(choice == "z10" || choice == "Z10"){
    cout << "Enter the number of packages : \n";
    cin  >> nop;
    add_10OZ(I, nop);
}

else if(choice == "z5"||choice == "Z5"){
    cout << "Enter the number of packages : \n";
    cin  >> nop;
    add_5OZ(I, nop);
}

else if(choice == "z1"|| choice == "Z1"){ // choice equal to "z1" or "Z1"
    cout << "Enter the number of packages : \n" ; // prompt for number of packages
    cin  >> nop ; // accept user input
    add_1OZ( I, nop);// call add_1OZ function
}
else if(choice == "o"||choice == "O")  {

cout << "Enter the number of ounces : ";// prompt for number of ounces
int noun; // noun stands for number of ounces
cin >> noun;// accept user input
Place_Order( I, noun);
 }

else if (choice == "q" || choice == "Q"){ // Output final message that the program has ended
   cout << "The program has ended.";
   break;
}
else
cout << "invalid comand" << endl;
 


};
return 0;
}








void show_Menu(){
cout <<  "S   - Show Inventory" << endl;
cout << "Z25 - Add 25 oz packages"<< endl;
cout << "Z10 - Add 10 oz packages"<< endl;
cout << "Z5  - Add 5 oz packages"<< endl;
cout << "Z1  - Add 1 oz packages "<< endl;
cout <<  "O   - Place order"<< endl;
cout <<     "Q   - End"<< endl;
}
void show (Inventory& I)
{
cout << "The Inventory comprises of the following:\n 1. Pack_25oz : " << I.Pack_25oz << "\n 2. 
Pack_10oz : " << I.Pack_10oz << "\n 3. Pack_5oz : " << I.Pack_5oz << "\n 4. Pack_1oz : " << 
I.Pack_1oz <<endl ;
}

void initialize (Inventory& I) {
int var = 0 ;
I.Pack_25oz = var;
I.Pack_10oz = var;
I.Pack_5oz = var;
I.Pack_1oz = var;
}
void add_25OZ (Inventory& I, int P25) {
I.Pack_25oz += P25;
}

void add_10OZ (Inventory& I, int P10) {
I.Pack_10oz += P10;
}

void add_5OZ (Inventory& I, int P5) {
I.Pack_5oz += P5;
}
void add_1OZ (Inventory& I, int P1) {
I.Pack_1oz += P1;
}
void Place_Order(Inventory& I, int amount) {
int subtract;
subtract = amount / 25;
   if (I.Pack_25oz <= 0) {
    cout << "Not enough packs" << endl;

} else {  I.Pack_25oz -= subtract; }
amount = amount % 25;
subtract = amount / 10;
   if (I.Pack_10oz <= 0) {
    cout << "Not enough packs" << endl;

} else {  I.Pack_10oz -= subtract; }
amount = amount % 10;
subtract = amount / 5;
   if (I.Pack_5oz <= 0) {
    cout << "Not enough packs" << endl;

} else {  I.Pack_5oz -= subtract; }
   amount = amount % 5;
 subtract = amount / 1;
   if (I.Pack_1oz <= 0) {
    cout << "Not enough packs" << endl;

} else {  I.Pack_1oz -= subtract;
 cout << "Order Fufilled" << endl;
}
}
克里斯蒂安·哈拉佐维奇

看来您getline在循环之外调用。所以choice永远不会更新。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章