Need help with my code please. c++

Hi, so I am having trouble writing some code. I am trying to write some code using functions. the problem I am trying to solve is this: Ask customers to select multiple products and quantities
Print the order summary, including the products, the quantities, and the total price for each product
Calculate and print the total price for the order.
I am quite new to programming so any help will be greatly appreciated as I am completely lost trying to figure this out. Thanks in advance for any help or advice. Here is what I have come up with this far:
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
  // Use of functions
#include <iostream>
#include <string>
using namespace std;

int displayProducts()
{
	char selection = 'a';
	do
	{
		cout << "Please select a product from the list below. " << endl;
		//display the menu
		cout << "Menu";
		cout << "======" << endl;
		cout << "1 - Apples" << endl;
		cout << "2 - Bananas" << endl;
		cout << "3 - Oranges" << endl;
		cout << "E - Exit " << endl << endl;
		cout << "Enter selection: ";
		// read user selection
		cin >> selection;

		switch (selection)
		{
		case '1':
			cout << "You selected - Apples" << endl;
			cout << "Enter quantity";
			int a;
			cin >> a;
			return a;
			break;
		case '2':
			cout << "You selected - Bananas" << endl;
			cout << "Enter quantity";
			int b;
			cin >> b;
			return b;
			break;
		case '3':
			cout << "You selected - Oranges" << endl;
			cout << "Enter quantity";
			int c;
			cin >> c;
			return c;
			break;
		case 'E':
		case 'e':
			cout << "Thank you." << endl;
			break;
			//if  1, 2, 3 or E is not selected then go to the default case
		default: cout << "Invalid selection. Please try again";
			// no break in the default case
		}
		cout << endl << endl;
	} while (selection != 'E' && selection != 'e');

}
void total()
{
	int a = displayProducts();
	int b = displayProducts();
	int c = displayProducts();
	int sum = a + b + c;
	cout << "Your total is " << sum << endl;
	cin.get();

}

int main()
{
	char selection = 'a';
	string name = "";
	string address;
	//ask the user for his or her name
	cout << "Please enter your name. ";
	getline(cin, name);
	//ask the user for his or her address 
	cout << "Please enter your address. ";
	getline(cin, address);
	//welcome the user by printing welcome and his or her name to the console
	cout << "Welcome " + name << ". Please press enter when you are ready to move on to the list of products." << endl;
	displayProducts();
	total();
	cin.get();

	return 0;
}

closed account (48T7M4Gy)
function displayProducts is supposed to return an int
Awesome, thank you for your help! Not sure why it even compiled and ran with out that. My main problem though is trying to print an order summary of what the user/customer ordered, such as each item they selected, how much each item cost separately then a total of everything they selected. For example if the customer wanted 1 apple, 1 banana and 1 orange where (to keep it simple) each cost $1.00. I can't seem to find a way implement some kind of order summary that can be reviewed by the customer.
closed account (48T7M4Gy)
This also might be an improvement because I don't know how much you know about passing arguments via function parameters - passing by reference. The way I show here means most of the work is done in the display function and a small bit via the summation function. depending where you are there are other ways.

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
// Use of functions
#include <iostream>
#include <string>
using namespace std;

int total(int a, int b, int c)
{
    return a + b + c;
}

void displayProducts()
{
    char selection = 'a';
    int a = 0;
    int b = 0;
    int c = 0;
    
    while (selection != 'E' and selection != 'e')
    {
        cout << "Please select a product from the list below. " << endl;
        //display the menu
        cout << "Menu";
        cout << "======" << endl;
        cout << "1 - Apples" << endl;
        cout << "2 - Bananas" << endl;
        cout << "3 - Oranges" << endl;
        cout << "E - Exit " << endl << endl;
        cout << "Enter selection: ";
        // read user selection
        cin >> selection;
        
        switch (selection)
        {
            case '1':
                cout << "You selected - Apples" << endl;
                cout << "Enter quantity ";
                cin >> a;
                break;
            case '2':
                cout << "You selected - Bananas" << endl;
                cout << "Enter quantity ";
                cin >> b;
                break;
            case '3':
                cout << "You selected - Oranges" << endl;
                cout << "Enter quantity ";
                cin >> c;
                break;
            case 'E':
            case 'e':
                cout << a << " apples\n";
                cout << b << " bananas\n";
                cout << c << " oranges\n";
                cout << total(a, b, c) << " pieces.\n";
                cout << "Thank you." << endl;
                break;
                //if  1, 2, 3 or E is not selected then go to the default case
            default: cout << "Invalid selection. Please try again";
                // no break in the default case
        }
        cout << endl << endl;
    }
}



int main()
{
    string name = "";
    string address;
    
    //ask the user for his or her name
    cout << "Please enter your name. ";
    getline(cin, name);
    
    //ask the user for his or her address
    cout << "Please enter your address. ";
    getline(cin, address);
    
    //welcome the user by printing welcome and his or her name to the console
    cout << "Welcome " + name << ". Please press enter when you are ready to move on to the list of products." << endl;
    displayProducts();
    
    return 0;
}
Please enter your name. aaa bbb
Please enter your address. 123 street st
Welcome aaa bbb. Please press enter when you are ready to move on to the list of products.
Please select a product from the list below.
Menu======
1 - Apples
2 - Bananas
3 - Oranges
E - Exit

Enter selection: 1
You selected - Apples
Enter quantity 5


Please select a product from the list below.
Menu======
1 - Apples
2 - Bananas
3 - Oranges
E - Exit

Enter selection: 2
You selected - Bananas
Enter quantity 6


Please select a product from the list below.
Menu======
1 - Apples
2 - Bananas
3 - Oranges
E - Exit

Enter selection: 3
You selected - Oranges
Enter quantity 23


Please select a product from the list below.
Menu======
1 - Apples
2 - Bananas
3 - Oranges
E - Exit

Enter selection: e
5 apples
6 bananas
23 oranges
34 pieces.
Thank you.


Program ended with exit code: 0
Last edited on
I decided to re-write your code a little

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
// Use of functions
#include <iostream>
#include <string>
using namespace std;

void total(int, int, int);
void displayProducts();

void displayProducts()
{
	int a = 0, b = 0, c = 0;
	char selection = 'a';
	do
	{
		
		cout << "Please select a product from the list below. " << endl;
		//display the menu
		cout << "Menu";
		cout << "======" << endl;
		cout << "1 - Apples" << endl;
		cout << "2 - Bananas" << endl;
		cout << "3 - Oranges" << endl;
		cout << "E - Exit " << endl << endl;
		cout << "Enter selection: ";
		// read user selection
		cin >> selection;
		 

		switch (selection)
		{
		case '1':
			cout << "You selected - Apples" << endl;
			cout << "Enter quantity. ";
			cin >> a;
			break;

		case '2':
			cout << "You selected - Bananas" << endl;
			cout << "Enter quantity. ";
			cin >> b;
			break;

		case '3':
			cout << "You selected - Oranges" << endl;
			cout << "Enter quantity. ";
			cin >> c;
			break;

		case 'E':
		case 'e':
			cout << "Thank you." << endl;
			break;

			//if  1, 2, 3 or E is not selected then go to the default case
		default: cout << "Invalid selection. Please try again";
			// no break in the default case
		}
		cout << endl << endl;
	} while (selection != 'E' && selection != 'e');

	cout << "\nYou ordered " << a << " Apples" << endl;
	cout << "You ordered " << b << " Bananas" << endl;
	cout << "You ordered " << c << " Oranges" << endl;

	total(a, b, c);
	 
}
void total(int a, int b, int c)
{
	 
	int sum = a + b + c;
	cout << "Your total is " << sum << endl;
	cin.get();

}

int main()
{
	char selection = 'a';
	string name = "";
	string address;
	//ask the user for his or her name
	cout << "Please enter your name. ";
	getline(cin, name);
	//ask the user for his or her address 
	cout << "Please enter your address. ";
	getline(cin, address);
	//welcome the user by printing welcome and his or her name to the console
	cout << "Welcome " << name << ". Please press enter when you are ready to move on to the list of products." << endl;
	displayProducts();
	 
	cin.get();
	return 0;
}


Hopefully this helps, if you don't understand anything..let me know

Also on line 81 don't use + use << like this:
 
cout << "Welcome " << name << " 
Last edited on
I think I get everything. I'm going over the difference between my original code and the code you altered to see where I could improve my code, and also how you implemented the order summary. I see in the beginning of your rewritten code you declared the functions "total and dispalyProducts" and the afterwards you defined them. Is there a reason you chose to do it that way rather than doing it all at once?
Those were just the function headers, my compiler would have given an error if I did not define them. But I guess the total function should really be an int like kemort has done.
Last edited on
Topic archived. No new replies allowed.