Assignment for School. Missing piece??

Hi everyone, I'm a noob with C++ right now so any help/suggestions would be most appreciated.
The main problem that I'm having with my code right now is that I need this loop:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

		for (int i = 0; i = sizes[3]; i++)
		{
			if (i = small)
			{
				sizes[3]++;
			}
			if (i = medium)
			{
				sizes[3]++;
			}
			if (i = large)
			{
				sizes[3]++;
			}
		}


		return sizes[3];


with an array to be passed to the main to have when making a selection.
I have tried putting this code into purchaseMenu, to return the array, but making purchaseMenu an int causes the while loop at the very bottom of the below code not to work. I'm not sure what I'm missing.

What needs to happen is the user first buys their cups of coffee then they return to the main menu to see options based on what they have bought, such as how many cups in total, how many of each cup, how much they owe, etc..

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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include <iostream>
#include <iomanip>

using namespace std; 

const double small_Cup = 1.75, med_Cup = 1.90, large_Cup = 2.00;


int mainMenu()
// function for the main menu in which the user chooses what they want to do or view. 
{


	int ret = 0;
	cout << "You may return to this prompt at any time by pressing #." << endl;
	cout << "What would you like to do? Select the desired number." << endl;
	cout << "1. Purchase coffee." << endl;
	cout << "2. View how many cups of coffee of each size has been sold." << endl;
	cout << "3. View the total amount of cups of coffee sold." << endl;
	cout << "4. View how much money is due." << endl;
	cout << "5. End." << endl;
	cin >> ret;

	return ret;
}


int productMenu(int small, int medium, int large)
// function to view the sizes of cups and the cost of each one. the user may then make a selection.

{
	int smallCount = 0;
	int medCount = 0;
	int largeCount = 0; 

		cout << "Read over the menu and enter the number of each product you would like." << endl; 
		
		cout << "Small(9oz.)................. $ " << showpoint << setprecision(3) << small_Cup << endl; 
		cout << "Medium(12oz.)................ $ " << showpoint << setprecision(3) << med_Cup << endl; 
		cout << "Large(15oz.)................. $ " << showpoint << setprecision(3) << large_Cup << endl; 
		
		cout << "How many small cups of coffee would you like? ";
		cin >> small;
		cout << endl; 

		cout << "How many medium cups of coffee would you like? ";
		cin >> medium; 
		cout << endl; 

		cout << "How many large cups of coffee would you like? ";
		cin >> large; 
		cout << endl; 
}
	

void amountDue(int small, int medium, int large)
//function to get the total amount due for all sizes.

{
	double amount;

	amount = (small_Cup * small) + (medium * med_Cup) + (large * large_Cup);
	cin >> amount; 
	cout << "The total amound due is: " << showpoint << amount << endl; 

}

void eachCup(int small, int medium, int large)
// function to view the total of each cup sold. 

{

	cout << "The total amount of Small Oz. cups sold is: " << small << "." << endl; 
	cout << "The total amount of Medium Oz. cups sold is: " << medium << "." << endl; 
	cout << "The total amount of Large Oz. cups sold is: " << large << "." << endl; 

}

void totalCups(int small, int medium, int large)
//function to view how many cups of coffee has been sold in all. 

{
	double allCups;

	allCups = small + medium + large;

	cout << "The total amount of cups of coffee sold is: " << allCups << endl; 

}


	



int main()
//main combines all of the functions to output the user's selected choice. 
{
	int small = 0;
	int medium = 0;
	int large = 0;

	int selection = 0;

	selection = mainMenu();
	do {
		switch (selection)

		{
		case 1:
			 (productMenu(small, medium, large));

			break;

		case 2:
			eachCup(small, medium, large);

			break;

		case 3:
			totalCups(small, medium, large);

			break;

		case 4:
			amountDue(small, medium, large);

			break;


		}

	} while (selection = mainMenu());
}



Thank you in advance :)
Last edited on
closed account (48T7M4Gy)
One problem if (i = small) etc should be if (i == small)

with an array to be passed to the main to have when making a selection

One way to handle this which will make life easier is to have all your relevant data arrays etc in main() and pass them to the functions for use and processing. Pass by reference or pass by value as appropriate.

Also, it's noticeable that your switch structure doesn't include a default case.

You will probably need several switches to correspond with the separate menus and that's where your # to escape comes in. Instead of selection being an int you might consider it being a char because of this.
Last edited on
I apologize, I actually meant to take out the # prompt because one I was trying to be too fancy for a simple homework assignment and 2 I have the mainMenu loop at the bottom so it's not needed.

So are you saying to add my loop / array into the main and only reference them in the functions?

So should I add the code under purchaseMenu under main()?
closed account (48T7M4Gy)
No apology necessary, after all it's your design. I agree with you that simplicity could be a virtue. Projects can grow out of control with too many options or where there are many levels of menus, especially if you haven't mapped out the logic and flow.

I would put the array and any other similar data in main() and pass by value or reference to the functions.

So should I add the code under purchaseMenu under main()?

Not necessarily. There is a modularity advantage in using functions though. Short answer is I wouldn't.
Last edited on
I have finally fixed it.
I agree w/ you about the functions, but the whole purpose of the assignment was to make and use user defined functions..

but here is the final code (aside from some few cosmetic issues).

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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131

 #include <iostream>
#include <iomanip>

using namespace std;

const double small_Cup = 1.75, med_Cup = 1.90, large_Cup = 2.00;


int mainMenu()
// function for the main menu in which the user chooses what they want to do or view.
{
        int ret = 0;

        cout << "What would you like to do? Select the desired number." << endl;
        cout << "1. Purchase coffee." << endl;
        cout << "2. View how many cups of coffee of each size has been sold." << endl;
        cout << "3. View the total amount of cups of coffee sold." << endl;
        cout << "4. View how much money is due." << endl;
        cout << "5. End." << endl;
        cin >> ret;

        return ret;
}


void productMenu(int sizes[3])
// function to view the sizes of cups and the cost of each one. the user may then make a selection.
{
        cout << "Read over the menu and enter the number of each product you would like." << endl;

        cout << "Small(9oz.)................. $ " << showpoint << setprecision(3) << small_Cup << endl;
        cout << "Medium(12oz.)................ $ " << showpoint << setprecision(3) << med_Cup << endl;
        cout << "Large(15oz.)................. $ " << showpoint << setprecision(3) << large_Cup << endl;

        cout << "How many small cups of coffee would you like? ";
        cin >> sizes[0];
        cout << endl;

        cout << "How many medium cups of coffee would you like? ";
        cin >> sizes[1];
        cout << endl;

        cout << "How many large cups of coffee would you like? ";
        cin >> sizes[2];
        cout << endl;
}

void amountDue(int small, int medium, int large)
//function to get the total amount due for all sizes.

{
        double amount = 0.00;

		amount = (small_Cup * small) + (medium * med_Cup) + (large * large_Cup);
        cout << "The total amound due is: $" << showpoint << amount << endl;

}

void eachCup(int small, int medium, int large)
// function to view the total of each cup sold.

{

        cout << "The total amount of Small Oz. cups sold is: " << small << "." << endl;
        cout << "The total amount of Medium Oz. cups sold is: " << medium << "." << endl;
        cout << "The total amount of Large Oz. cups sold is: " << large << "." << endl;

}

void totalCups(int small, int medium, int large)
//function to view how many cups of coffee has been sold in all.

{
        double allCups;

        allCups = small + medium + large;

        cout << "The total amount of cups of coffee sold is: " << allCups << endl;

}






int main()
//main combines all of the functions to output the user's selected choice.
{
        int small = 0;
        int medium = 0;
        int large = 0;
        int sizes[3];
        int selection = 0;

        selection = mainMenu();
        do {
                switch (selection)

                {
                case 1:
                        productMenu(sizes);
                        small = sizes[0];
                        medium = sizes[1];
                        large = sizes[2];
                        break;

                case 2:
                        eachCup(small, medium, large);

                        break;

                case 3:
                        totalCups(small, medium, large);
						
                        break;

                case 4:
                        amountDue(small, medium, large);
                        break;


                }

        } while (selection = mainMenu());


}

Last edited on
closed account (48T7M4Gy)
That's the way. (I realise now my 'short answer' above is unintentionally ambiguous.)
Haha, you're fine.
I corrected several errors from your input. :)

The only problem I have with the outcome is I don't know how to break the while loop after my case 5 (for default). It's not listed here, but I did submit it with one.

Thank you so much.
closed account (48T7M4Gy)
Maybe something like this, just fill up the cases with your stuff?

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
#include <iostream>

int main()
{
    char selection = '0';
    
    while (std::cin >> selection && selection != 'q')
    {
        switch (selection)
        {
            case '1':
                std::cout << "Case 1\n";
                break;
                
            case '2':
                std::cout << "Case 2\n";
                break;
                
            default:
                std::cout << "Invalid choice\n";
                break;
        }
    }
    
    std::cout << "End\n";
    
    return 0;
}
Last edited on
Topic archived. No new replies allowed.