我的 while 循环中的 if 语句在不应该重复的情况下一直重复。它应该在 if stateme 之后回到 while 循环

My if statements inside my while loop keep repeating forever when they are not suppose to be. It should go back to the while loop after the if stateme

所以我正在为学校做一个项目,但我无法让它工作。其他一切似乎 运行 但是当我输入用户对 q、s、z10、z25、z5、z1 的选择时,它无限循环。我不明白为什么它会一遍又一遍地 运行ning 这个函数。我不太确定我是不是打错了什么,但这是我目前唯一真正坚持的事情。这是代码

#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 永远不会更新。