Hi, I appreciate the fact that you are putting effort to solve your problem.
Before i give you a working code, let me walk you through a story.
In the world, there are about 3 billion people, an English man will refer to one of these 3 billion as a Person. Each person like you and i have
- name
- age
- height etc
- weight
At each time, you know how to describe yourself e.g My name is Somebody, I am 2 years old and 98 cm tall. From height and weight, one can derive the Body Mass Index (BMI).
The same applies to this your program.
You have beverages each has a name, a price (per unit), quantity ordered and from the price and quantity ordered, one can derive the total cost for the beverage.
Now in programming terms, we can contain a Beverage in what is called
Class
or
Struct
difference=>
https://stackoverflow.com/a/92951
If you don't know what these mean then your lecturer wanted you to research it, atleast a Struct
http://www.cplusplus.com/doc/tutorial/structures/
I directed you to that thread because this solution
http://www.cplusplus.com/forum/beginner/218985/#msg1009561 uses a struct and it was appropriate for the problem.
Read
http://www.cplusplus.com/doc/tutorial/structures/ to get a better understanding. Without which, you might find it difficult understanding what i have done.
This is how you should target your problem
1. Make a Struct (i call it Beverage) that holds each beverage's name, price, quantity and any other function that you would like to have eg.
info()
to display what you write in
viewOptionX()
2. To get a total, you have to hold these beverages in some sort of container. Use
std::vector
http://www.cplusplus.com/reference/vector/vector/ if you have reached there otherwise use
c array
http://www.cplusplus.com/doc/tutorial/arrays/
3. Make a function to get quantity
4. Make a function to display summary of all orders
5. Make a function to calculate total cost of all orders
This is how i did it
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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
|
// Use of functions
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
/**
* @struct Beverage
*
* @brief Let each beverage have a name, price and quantity
* and even information about itself
*/
struct Beverage
{
string name; /**< The name */
int quantity{}; /**< The quantity */
double price{}; /**< The price */
void info()
{
cout << setw(10) << left << "Name" << name << "\n"
<< setw(10) << left << "Price" << "$" << price << "\n"
<< setw(10) << left << "Quantity" << quantity << "\n"
<< setw(10) << left << "Total" << "$"
<< quantity * price << "\n" << endl;
}
void simpleInfo()
{
cout << "Selection: " << name << ", price per unit: $" << price << endl;
}
Beverage() {}
~Beverage(){}
Beverage(string n, double p) {
name = n;
price = p;
}
}; // END OF STRUCT
double totalCost(Beverage beverages[], int numOfBeverages);
void orderSummary(Beverage beverages[], int numOfBeverages);
int getQuantity();
void doShopping();
void displayMenu(string userName)
{
cout << endl << endl
<< userName
<< ", Please select the beverage you would like to purchase from the menu: "
<< endl;
cout << "Drink Menu" << endl;
cout << "========" << endl;
cout << "1 - Water $1" << endl;
cout << "2 - Soda $2" << endl;
cout << "3 - Iced Tea $3" << endl;
cout << "X - Exit " << endl << endl;
}
int main(void)
{
doShopping();
return 0;
}
/**
* @fn int getQuantity()
*
* @brief Gets the quantity
* NB: There is no attempt to check for correct input
* if you want to check that the number is really valid,
* check this
* http://www.cplusplus.com/forum/beginner/230666/#msg1043359
* or other solutions in the same thread or google
*
*
* @return The quantity.
*/
int getQuantity() {
int quantity = 0;
cout << "Enter quantity : ";
cin >> quantity;
return quantity;
}
/**
* @fn double totalCost(Beverage beverages[], int numOfBeverages)
*
* @brief Total cost
*
* @param beverages The beverages.
* @param numOfBeverages Number of beverages.
*
* @return The total cost.
*/
double totalCost(Beverage beverages[], int numOfBeverages) {
double totalCost = 0;
for (int i = 0; i < numOfBeverages; i++) {
totalCost += (beverages[i].price*beverages[i].quantity);
}
return totalCost;
}
/**
* @fn void orderSummary(Beverage beverages[], int numOfBeverages)
*
* @brief Order summary for items selected
*
* @param beverages The beverages.
* @param numOfBeverages Number of beverages.
*/
void orderSummary(Beverage beverages[], int numOfBeverages)
{
cout << "\n======= ORDER SUMMARY ====" << endl;
cout << "Items selected" << endl << endl;
for (int i = 0; i < numOfBeverages; i++) {
// only show beverages that has at least one order
if (beverages[i].quantity > 0) {
beverages[i].info();
}
}
}
void doShopping()
{
// declare our beverages
Beverage water("Water", 1),
soda("Soda", 2),
iceTea("Ice Tea", 3);
// make an array to hold the beverages
// I strongly recommend using std::vector for this
Beverage beverages[] = { water, soda, iceTea };
char selection = ' ';
string name = "";
//Ask user for her/his name
cout << "Please enter your name ==> ";
getline(cin, name);
//display user name
cout << "Hello " + name << endl;
do
{
system("cls");
// display menu
displayMenu(name);
// read user selection
cout << "Your selection: ";
cin >> selection;
switch (selection)
{
case '1':
beverages[0].simpleInfo();
beverages[0].quantity += getQuantity();
break;
case '2':
beverages[1].simpleInfo();
beverages[1].quantity += getQuantity();
break;
case '3':
beverages[2].simpleInfo();
beverages[2].quantity += getQuantity();
break;
case 'X':
case 'x':
orderSummary(beverages, 3);
if (totalCost(beverages, 3) > 0) {
cout << "\nGrand total = $" << totalCost(beverages, 3) << endl;
}
cout << "\nThank you!!!" << endl;
break;
// other than 1, 2, 3 and X...
default: cout << "Invalid selection. Please try again";
// no break in the default case
}
cout << endl << endl;
} while (selection != 'X' && selection != 'x');
}
| |
Three things to note:
1. Always use code tags
http://www.cplusplus.com/articles/jEywvCM9/
2. As a beginner, paste your questions in the beginner forum. Your are more likely to get help there.
3. This forum is not an SOS forum so wanting help ASAP is not the best way of asking for help. People come here to help at their spare time.