### Problem with nested do while loop.

Okay, so the premise is a shopping program with three different calculators in it.

Im having a problem with calling and using the menu function, no matter what its launching the best buy func.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293`` ``````//Brandon Butts #include #include using namespace std; //function prototypes void bestbuy (double p1, double p2, double p3, double & lowest, int & number); void discountresults (double price, double discpercent, double & discountprice, double & discountamount); // void howmany (double monavail, double cost, double & remaining, int & number); char menu (); int main() { int number = 0; double price1, price2, price3, lowest = 0, origprice, discountpercent, discountprice = 0, discountamount = 0; char option; do {option = menu(); if (option = 'B') { cout << "Best Buy Calculator" << endl; cout << "Enter the first price." << endl; cin >> price1; cout << "Enter the second price." << endl; cin >> price2; cout << "Enter the third price." << endl; cin >> price3; cout << " " << endl; bestbuy (price1, price2, price3, lowest, number); cout << lowest << " is the lowest price." << endl; cout << number << " is the lowest value." << endl; system ("pause"); } else if (option = 'D') { cout << "Discount Calculator" << endl; cout << "Enter the product price." << endl; cin >> origprice; cout << "Enter the discount percentage." << endl; cin >> discountpercent; cout << " " << endl; discountresults (origprice, discountpercent, discountprice, discountamount); cout << "The discount amount is " << discountamount << endl; cout << "The discount cost is " << discountprice << endl; system ("pause"); } } while (option != 'Q'); system ("pause"); return 0; } //inputs three prices, returns the number of the lowest price and what that price is void bestbuy (double p1, double p2, double p3, double & lowest, int & number) { if (p1 < p2 && p3) { lowest = p1; number = 1; } else if (p2 < p1 && p3) { lowest = p2; number = 2; } else { lowest = p3; number = 3; } } //inputs the price and discount percent, then calculates the discounted price void discountresults (double price, double discpercent, double & discountprice, double & discountamount) { discountamount = price * discpercent; discountprice = price - discountamount; } //inputs the amount of available funds and cost, and outputs how many you can purchase and the leftover funds void howmany (double monavail, double cost, double & remaining, int & number) { number = monavail / cost; remaining = monavail - cost * number; } //provides a menu for the program char menu () { char option; cout << " " << endl; cout << "What would you like to do?" << endl; cout << "(B)est Buy Calculator" << endl; cout << "(D)iscoutn Calculator" << endl; cout << "(H)ow Many? Calculator" << endl; cout << "(Q)uit" << endl; cin >> option; return option; }``````
You declared the char option twice. Once on line 16 and once in the menu() function at line 84. I believe you have to name the char in the menu() function different and it should work, but I prolly have less C++ experience then you so don't take my word for it. :)

Also, not sure how that works using system("pause") without including the <cstdlib>.

Hmm, tried changing the char in menu() to something else and it did the same thing.

BAH now I see it. You are assigning 'B' to option on 19. To use the 'equals' conditional you have to use ==.

Here's what it should look like, with the <cstdlib> included on mine to make it work for me heh.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657`` ``````//Brandon Butts #include #include #include using namespace std; //function prototypes void bestbuy (double p1, double p2, double p3, double & lowest, int & number); void discountresults (double price, double discpercent, double & discountprice, double & discountamount); // void howmany (double monavail, double cost, double & remaining, int & number); char menu (); int main() { int number = 0; double price1, price2, price3, lowest = 0, origprice, discountpercent, discountprice = 0, discountamount = 0; char option; do { option = menu(); if (option == 'B') { cout << "Best Buy Calculator" << endl; cout << "Enter the first price." << endl; cin >> price1; cout << "Enter the second price." << endl; cin >> price2; cout << "Enter the third price." << endl; cin >> price3; cout << " " << endl; bestbuy (price1, price2, price3, lowest, number); cout << lowest << " is the lowest price." << endl; cout << number << " is the lowest value." << endl; system ("PAUSE"); } else if (option == 'D') { cout << "Discount Calculator" << endl; cout << "Enter the product price." << endl; cin >> origprice; cout << "Enter the discount percentage." << endl; cin >> discountpercent; cout << " " << endl; discountresults (origprice, discountpercent, discountprice, discountamount); cout << "The discount amount is " << discountamount << endl; cout << "The discount cost is " << discountprice << endl; system ("PAUSE"); } } while (option != 'Q'); system ("PAUSE"); return 0; }``````

Last edited on
Topic archived. No new replies allowed.