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:
// Use of functions
#include <iostream>
#include <string>
usingnamespace 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;
}
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.
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.
// Use of functions
#include <iostream>
#include <string>
usingnamespace 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.
// Use of functions
#include <iostream>
#include <string>
usingnamespace 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
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.