C++ Practice

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;


// Switch Statement
switch (num)
{
case 1 :
cout << Choice = "Almonds" << endl;
CostPerPound = 1.99;
break;

case 2 :
cout << Choice = "Cashews" << endl;
CostPerPound = 1.59;
break;

case 3 :
cout << Choice = "Peanuts" << endl;
CostPerPound = .99;
break;

case 4 :
cout << Choice = "Pecans" << endl;
CostPerPound = 1.09;
break;

case 5 :
cout << "Pistachios" << endl;
CostPerPound = 2.09;
break;

default :
cout << "You entered a wrong selection" << endl;
break;
}

cout << endl;

cout << "Enter Choice: " << endl;
cin >> num;

cout << Choice << "Enter number of Pounds" << endl;
cin >> Pounds;

TotalCost = Pounds * CostPerPound;


cout << setiosflags(ios::fixed);

cout << setprecision(2);

cout << Choice <<", The Total Cost is: " << TotalCost;

cin.ignore ( numeric_limits<streamsize>::max(), '\n' );
cin.get();


return(0);

}
Something among these lines.
( Freehand, may not compile! )
( I don't use Dev C++, may have different syntax. )


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
/*
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;

cout << "*** Choices ***" << endl //Display choices of nut
<< "1 - Almonds" << endl
<< "2 - Cashews" << endl
<< "3 - Peanuts" << endl
<< "4 - Pecans" << endl
<< "5 - Pistachios" << endl
<< endl;


cout << "Enter Choice: " << endl; //moved up so switch statement would be valid
cin >> num;


// Switch Statement
switch (num)
{
case 1 : 
cout << "You chose Almonds!" << endl; //separated statements
Choice = "Almonds"; //set choice variable
CostPerPound = 1.99;
break;

case 2 :
cout << "You chose Cashews!" << endl;
Choice = "Cashews";
CostPerPound = 1.59;
break;

case 3 :
cout << "You chose Peanuts!" << endl;
Choice = "Peanuts";
CostPerPound = .99;
break;

case 4 :
cout << "You chose Pecans!" << endl;
Choice = "Pecans";
CostPerPound = 1.09;
break;

case 5 :
cout << "You chose Pistachios!" << endl;
Choice = "Pistachios";
CostPerPound = 2.09;
break;

default :
cout << "You entered an invalid selection!" << endl;
cin.ignore ( numeric_limits<streamsize>::max(), '\n' );
cin.get();
return 0;
break;
}

cout << endl;

cout << Choice << "Enter number of Pounds: " << endl;
cin >> Pounds;

TotalCost = Pounds * CostPerPound;


cout << setiosflags(ios::fixed);   // ?
                                   // What are these for?
cout << setprecision(2);           // ?

cout << "The Total Cost for your " << Choice << " is: " << TotalCost;

cin.ignore ( numeric_limits<streamsize>::max(), '\n' );
cin.get();


return(0);

}


Last edited on
Here - try this!

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
#include <iostream>
using namespace 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 >> num BEFORE 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.
Last edited on
Thanks CheesyBeefy and Bluezor. I will look at both solution later and let you guys know what happens.
Bluezor, your solution worked. Thanks again.
Topic archived. No new replies allowed.