So im working on a project for school and i just cant get this to work. everything else seems to run but when i input the users choice for q, s, z10, z25,z5,z1 it loops indefinetly. i cant figure out why it keeps running the function over and over. im not quite sure if im just typing something wrong or what but this is the only thing im really stuck on currently. Here is the code
#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;
}
}
CodePudding user response:
It seems you call getline
outside of the loop. so choice
is never updated.