void problem

i found this on one of the archive of the board but the program shuts down then i input a product


#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

void display_menu();
void tax(float);
float total = 0;

int main()
{
char choice;
display_menu();
cout << setiosflags(ios::showpoint) << setiosflags(ios::fixed) << setprecision(2);

cout << "Welcome to the Rodeo Snack Bar!" << endl;
cout << "Your current total is: $"<< total << endl;
cout << "Please enter the letter of your selection: ";
cin >> choice;
cout << endl;

switch (choice)
{

case 'S': case 's':
cout << "Sandwich - $5.00" << endl;
total+=5;
display_menu();
break;
case 'C': case 'c':
cout << "Chips - $1.50" << endl;
total+=1.5;
display_menu();
break;
case 'P': case 'p':
cout << "Pickle - $0.75" << endl;
total+=.75;
display_menu();
break;
case 'B': case 'b':
cout << "Brownie - $1.00" << endl;
total+=1;
display_menu();
break;
case 'R': case 'r':
cout << "Regular Drink - $2.00" << endl;
total+=2;
display_menu();
break;
case 'L': case 'l':
cout << "Large Drink - $3.50" << endl;
total+=3.5;
display_menu();
break;
case 'X' : case 'x':
cout << "Canceled, please start over." << endl;
total = 0;
display_menu();
break;
case 'T' : case 't':
tax(total);
break;
default:
display_menu();
}


}




void display_menu()
{
cout << endl;
cout << "S - Sandwich $5.00" << endl;
cout << "C - Chips $1.50" << endl;
cout << "P - Pickle $0.75" << endl;
cout << "B - Brownie $1.00" << endl;
cout << "R - Regular Drink $2.00" << endl;
cout << "L - Large Drink $3.50" << endl;
cout << "X - Cancel sale and start over" << endl;
cout << "T - Total the sale" << endl;
cout << "All items have additional 8.25% tax." << endl;
}


void tax(float total)
{

cout << "Sub-total: " << total << endl;
cout << "+ Tax : " << total*0.0825 << endl;
cout << "Total : " << total + (total*0.0825)<< endl;
}
what's the problem with it? it works fine with me.
it does run. but when you chose a letter it shuts down.
well that's normal. it shuts down because it already finish it's task.. some IDE like visual studio stops it from closing for you to see the output, but unfortunately some IDEs don't..

you may want to add system("pause"); or cin.get(); at the end of your main() to stop it manually..

note:
system("pause"); is evil..
but i think it should add the products that you buy it says 0.00 in your cost meaning it does not do anything yet
yes it does sums only a product.. i think the program lacks a loop.. a while loop should be able to fix it.
where should i place it. what kind of compiler do you use. i currently use a dev-C++ and it does not solve in my complier.
i am using Code::Blocks IDE with Mingw compiler, anyway Dev-C++ also uses Mingw compiler..

i tweak your code above a little, i hope you would learn from 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
#include <iostream>
#include <iomanip>
#include <string>
#include <conio.h>

using namespace std;

void display_menu();
void tax(float);
float total = 0;

int main() {
    char choice;
    bool more = true;
    cout << setiosflags(ios::showpoint) << setiosflags(ios::fixed) << setprecision(2);

    while(more) { //loop until the user wants more
        cout << "-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n";
        cout << "Welcome to the Rodeo Snack Bar!" << endl;
        display_menu();
        cout << "Please enter the letter of your selection: ";
        cin >> choice;
        switch (choice) {
        case 'S':
        case 's':
            cout << "Sandwich - $5.00" << endl;
            total+=5;
            break;
        case 'C':
        case 'c':
            cout << "Chips - $1.50" << endl;
            total+=1.5;
            break;
        case 'P':
        case 'p':
            cout << "Pickle - $0.75" << endl;
            total+=.75;
            break;
        case 'B':
        case 'b':
            cout << "Brownie - $1.00" << endl;
            total+=1;
            break;
        case 'R':
        case 'r':
            cout << "Regular Drink - $2.00" << endl;
            total+=2;
            display_menu();
            break;
        case 'L':
        case 'l':
            cout << "Large Drink - $3.50" << endl;
            total+=3.5;
            break;
        case 'X' :
        case 'x':
            cout << "Canceled, please start over." << endl;
            total = 0;
            break;
        case 'T' :
        case 't':
            tax(total);
            more = false; //stop the loop
            return 0; //end the program
            break;
        default:
            cout << "Ooops! something went wrong.. :(\n\n";
            return 0; //end the program
        }
        cout << "Your current total is: $ "<< total << "\n\n";
    }
    return 0;
}




void display_menu() {
    cout << endl;
    cout << "S - Sandwich $5.00" << endl;
    cout << "C - Chips $1.50" << endl;
    cout << "P - Pickle $0.75" << endl;
    cout << "B - Brownie $1.00" << endl;
    cout << "R - Regular Drink $2.00" << endl;
    cout << "L - Large Drink $3.50" << endl;
    cout << "X - Cancel sale and start over" << endl;
    cout << "T - Total the sale" << endl;
    cout << "All items have additional 8.25% tax." << endl;
}


void tax(float total) {
    cout << endl;
    cout << "Sub-total: " << total << endl;
    cout << "+ Tax : " << total*0.0825 << endl;
    cout << "Total : " << total + (total*0.0825) << endl;
}


ps: please use code tags next time you post codes in the forum..
http://cplusplus.com/articles/firedraco1/
wow you made it work!!!! thx a lot!!!
Topic archived. No new replies allowed.