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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
//Brandon Butts
#include <iostream>
#include <iomanip>
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//Brandon Butts
#include <iostream>
#include <iomanip>
#include <cstdlib>
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.