Do While Loop menu in C++ -
i'm having trouble do-while loop menu program i'm working on school. i've checked, , far i'm concerned have written code correctly. however, when testing, if type 'y' or 'n' result same: menu streaming down 100's of times non stop until exit program. idea on i'm doing wrong , how can display menu every time? in advance.
#include <iostream> #include <iomanip> #include <string> #include "cashregister.h" #include "inventoryitem.h" using namespace std; int main() { // variables int selection, numunits, cont; double price; // use first constructor first item inventoryitem item1; item1.setcost(5.0); item1.setdescription("adjustable wrench"); item1.setunits(10); // use second constructor second item inventoryitem item2("screwdriver"); item2.setcost(3.0); item2.setunits(20); // use third constructor remaining items inventoryitem item3("pliers", 7.0, 35); inventoryitem item4("ratchet", 10.0, 10); inventoryitem item5("socket wrench", 15.0, 7); { cout << "#\t" << "item\t\t\t" << "qty on hand" << endl; cout << "------------------------------------------------------------------" << endl; cout << "1\t" << item1.getdescription() << "\t" << setw(3) << item1.getunits() << endl; cout << "2\t" << item2.getdescription() << "\t\t" << setw(3) << item2.getunits() << endl; cout << "3\t" << item3.getdescription() << "\t\t\t" << setw(3) << item3.getunits() << endl; cout << "4\t" << item4.getdescription() << "\t\t\t" << setw(3) << item4.getunits() << endl; cout << "5\t" << item5.getdescription() << "\t\t" << setw(3) << item5.getunits() << endl; cout << "which item above being purchased? "; cin >> selection; // validate selection while (selection < 1 || selection > 5) { cout << "error, please make valid item selection: "; cin >> selection; } cout << "how many units? "; cin >> numunits; // validate quantity of units make sure isn't negative value while (numunits < 0) { cout << "error, please enter valid quantity: "; cin >> numunits; } // use switch statement figure out cost pull switch (selection) { case 1: {price = item1.getcost(); item1.changeunits(numunits); } break; case 2: {price = item2.getcost(); item2.changeunits(numunits); } break; case 3: {price = item3.getcost(); item3.changeunits(numunits); } break; case 4: {price = item4.getcost(); item4.changeunits(numunits); } break; case 5: {price = item5.getcost(); item5.changeunits(numunits); } break; } // create cashregister object particular selection cashregister transaction(price, numunits); // display totals cout << fixed << showpoint << setprecision(2); cout << "subtotal: $" << transaction.getsubtotal() << endl; cout << "sales tax: $" << transaction.getsalestax() << endl; cout << "total: $" << transaction.getpurchasetotal() << endl; // find out if user wants purchase item cout << "do want purchase item? enter y/n: "; cin >> cont; } while (cont != 'n' && cont != 'n'); system("pause"); return 0;
}
your loop never break
unless explicitly enter 110
'n' char in ascii codes or 78 'n'. change cont
declaration int cont;
char cont;
, won't infinite loop anymore, , condition valid possibly break
unless have hidden logical error require debug it.
Comments
Post a Comment