I am trying to write a program that allows the user to select from a menu various types of nuts. After the user enters their choice, the Cost Per Pound is Displayed. Then the user inputs the number of pounds they want to purchase and the Total is calculated.
Can anyone review my code and point me in the right direction?
PURPOSE: Calculate Total Cost of Selected Item
COMPILER: Dev C++
LIMITATIONS:
*/
#include <cstring>
#include <iostream>
#include <iomanip>
using namespace std;
int main(void);
{
string Choice;
int num;
int Pounds;
double CostPerPound;
double TotalCost;
#include <iostream>
usingnamespace std;
int main()
{
int num;
float pounds;
float CostPerPound;
float TotalCost;
cout << "Please enter a number (1 - 5) to display what you want." << endl;
cin >> num;
switch (num)
{
case 1:
cout << "You have chosen Almonds." << endl;
cout << "The cost per pound is $1.99." << endl;
CostPerPound = 1.99;
break;
case 2:
cout << "You have chosen Cashews." << endl;
cout << "The cost per pound is $1.59." << endl;
CostPerPound = 1.59;
break;
case 3:
cout << "You have chosen Peanuts." << endl;
cout << "The cost per pound is $0.99." << endl;
CostPerPound = 0.99;
break;
case 4:
cout << "You have chosen Pecans." << endl;
cout << "The cost per pound is $1.09." << endl;
CostPerPound = 1.09;
break;
case 5:
cout << "You have chosen Pistachios." << endl;
cout << "The cost per pound is $2.09." << endl;
CostPerPound = 2.09;
break;
default:
cout << "You entered a wrong selection." << endl;
system("PAUSE");
return (0);
break;
}
cout << "How many pounds would you like of your item?" << endl;
cin >> pounds;
TotalCost = pounds * CostPerPound;
cout << "Your total cost is $" << TotalCost << "." << endl;
cout << "Thank you for shopping at Nut Market!" << endl;
system("PAUSE");
return(0);
}
I don't think you really need a string. And you have to put the cin >> numBEFORE the switch function.
I also set the number of pounds to a float in case you want to buy pounds that aren't natural numbers. You might buy 2.43 pounds of nuts - but not 2 only.