void function with 4 parameters that must display

Pages: 12
The void funcnction( voidcost() ) displays everything except the variable tax, tip, and totBill in 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
#include <iostream>
#include <iomanip>

using namespace std;


    void display()
    {
    cout << "*** BASEBALL GAME SNACK MENU ***" << endl;
    cout << "----------------------------------------------" << endl;
    cout << "1   Hamburger 	        $6.00" << endl;
    cout << "2   Hotdog		$4.50" << endl;
    cout << "3   Peanuts	        $3.75" << endl;
    cout << "4   Popcorn	        $5.50" << endl;
    cout << "5   Soda		$2.80" << endl;
    cout << "6   Chips		$1.00" << endl;
    cout << "7   Water		$2.00" << endl;
    cout << "8   end order  "<< endl << endl;

    }

    double item;
    double total;
    double tax;
    double tip;
    double totBill; 

    void cost(double, double, double, double)
    {
        cout << "The total is: " << setprecision(2) << fixed << total << endl;
        cout << "Tip comes to: " << setprecision(2) << fixed << tip << endl;
        cout << "Tax on your purchase is: " << setprecision(2) << fixed << tax << endl;
        cout << "Your total bill is: " << setprecision(2) << fixed << totBill << endl;
    }

    void Tcash();

int main()
{
    {//displays menu
    display();
    }

    while (item != 8)
    {
        //displays enter item until 8 is selected
    cout << "Enter Menu Item:" << endl;
    cout << "----------------" << endl;
    cin >> item;
    }


    //assigns dollar value to items
    if (item = 1)
       {
        item = 6.00;
       }
    else if (item = 2)
       {
        item = 4.50;
       }
    else if (item = 3)
       {
        item = 3.75;
       }
    else if (item = 4)
       {
        item = 5.50;
       }
    else if (item = 5)
       {
        item = 2.80;
       }
    else if ( item = 6)
       {
        item = 1.00;
       }
    else if ( item = 7)
       {
        item = 2.00;
       }
    else if (item = 8)
    {
        item = 0;
    }
    
    total += item;
    tax = total * .065;
    tip = total * .20;
    totBill = total + tax + tip;

//displays the bill
{
    cost ();
}

    return 0;
}

Why are you using those global variables (lines 22 - 26)?

Why do you have unnamed parameters for your cost() function? Those parameters should have names (probably the same names as those global variables) and you should be using those parameters in your function. You should also be using some parameters in the function call at line 94.


I have tried it with names ove the variables as well, I thought I read if a variable was called out of main it was not global. def need to try again let me make those changes again. I had the variables called in the output of cost before the display output would that be considered local? I have moved things around so much trying i am even getting confused.
Same no values being carried or assigned ??
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
#include <iostream>
#include <iomanip>

using namespace std;


    void display()
    {
    cout << "*** BASEBALL GAME SNACK MENU ***" << endl;
    cout << "----------------------------------------------" << endl;
    cout << "1   Hamburger 	        $6.00" << endl;
    cout << "2   Hotdog		$4.50" << endl;
    cout << "3   Peanuts	        $3.75" << endl;
    cout << "4   Popcorn	        $5.50" << endl;
    cout << "5   Soda		$2.80" << endl;
    cout << "6   Chips		$1.00" << endl;
    cout << "7   Water		$2.00" << endl;
    cout << "8   end order  "<< endl << endl;

    }

    
    void cost(double total, double tax, double tip, double totBill)
    {
    double item;
    double total;
    double tax;
    double tip;
    double totBill; 
    }
    
    void Tcash();

int main()
{
    {//displays menu
    display();
    }

    while (item != 8)
    {
        //displays enter item until 8 is selected
    cout << "Enter Menu Item:" << endl;
    cout << "----------------" << endl;
    cin >> item;
    total+=item;
    }

    
    //assigns dollar value to items
    if (item = 1)
       {
        item = 6.00;
       }
    else if (item = 2)
       {
        item = 4.50;
       }
    else if (item = 3)
       {
        item = 3.75;
       }
    else if (item = 4)
       {
        item = 5.50;
       }
    else if (item = 5)
       {
        item = 2.80;
       }
    else if ( item = 6)
       {
        item = 1.00;
       }
    else if ( item = 7)
       {
        item = 2.00;
       }
    else if (item = 8)
    {
        item = 0;
    }
       
    tax = total * .065;
    tip = total * .20;
    totBill = total + tax + tip;

    voidcost(total, tax, tip, totBill);
            
system (pause);
}
//displays the bill
    
    void cost(total, tax, tip, totBill)
    {
    cout << "The total is: " << setprecision(2) << fixed << total << endl;
    cout << "Tip comes to: " << setprecision(2) << fixed << tip << endl;
    cout << "Tax on your purchase is: " << setprecision(2) << fixed << tax << endl;
    cout << "Your total bill is: " << setprecision(2) << fixed << totBill << endl; 
    }


 
Your cost() function implementation should look more like (starting at line 23):
1
2
3
4
5
6
7
void cost(double total, double tax, double tip, double totBill)
{
    cout << "The total is: " << setprecision(2) << fixed << total << endl;
    cout << "Tip comes to: " << setprecision(2) << fixed << tip << endl;
    cout << "Tax on your purchase is: " << setprecision(2) << fixed << tax << endl;
    cout << "Your total bill is: " << setprecision(2) << fixed << totBill << endl; 
}


Lines 94 - 100 are not required.

Lines 25 - 29 should be moved into main() and don't forget to initialize them as well.

By the way did you compile the code from your latest try?

Last edited on
yes it ran and ouput was there but variables were not carried correctly, I need a way where the 25-29 will not be declared globally also
Here is what I have I am not sure about the variables not being declared globally still
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
#include <iostream>
#include <iomanip>

using namespace std;


    void display()
    {
    cout << "*** BASEBALL GAME SNACK MENU ***" << endl;
    cout << "----------------------------------------------" << endl;
    cout << "1   Hamburger 	        $6.00" << endl;
    cout << "2   Hotdog		$4.50" << endl;
    cout << "3   Peanuts	        $3.75" << endl;
    cout << "4   Popcorn	        $5.50" << endl;
    cout << "5   Soda		$2.80" << endl;
    cout << "6   Chips		$1.00" << endl;
    cout << "7   Water		$2.00" << endl;
    cout << "8   end order  "<< endl << endl;

    }

    
    void cost(double total, double tax, double tip, double totBill)
    {
    double item;
    double total;
    double tax;
    double tip;
    double totBill; 
    }
    
    void Tcash();

int main()
{
    {//displays menu
    display();
    }

    while (item != 8)
    {
        //displays enter item until 8 is selected
    cout << "Enter Menu Item:" << endl;
    cout << "----------------" << endl;
    cin >> item;
    total+=item;
    }

    
    //assigns dollar value to items
    if (item = 1)
       {
        item = 6.00;
       }
    else if (item = 2)
       {
        item = 4.50;
       }
    else if (item = 3)
       {
        item = 3.75;
       }
    else if (item = 4)
       {
        item = 5.50;
       }
    else if (item = 5)
       {
        item = 2.80;
       }
    else if ( item = 6)
       {
        item = 1.00;
       }
    else if ( item = 7)
       {
        item = 2.00;
       }
    else if (item = 8)
    {
        item = 0;
    }
       
    tax = total * .065;
    tip = total * .20;
    totBill = total + tax + tip;

    //displays the bill
    void cost(double total, double tax, double tip, double totBill)
    {
    cout << "The total is: " << setprecision(2) << fixed << total << endl;
    cout << "Tip comes to: " << setprecision(2) << fixed << tip << endl;
    cout << "Tax on your purchase is: " << setprecision(2) << fixed << tax << endl;
    cout << "Your total bill is: " << setprecision(2) << fixed << totBill << endl; 
    }

            
system (pause);
}



 
Guess from here I need to go study local variables some more
Sorry, I misread the page on local and global. But the problem remains with the values not being passed correctly??
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>

using namespace std;


    void display()
    {
    cout << "*** BASEBALL GAME SNACK MENU ***" << endl;
    cout << "----------------------------------------------" << endl;
    cout << "1   Hamburger 	        $6.00" << endl;
    cout << "2   Hotdog		$4.50" << endl;
    cout << "3   Peanuts	        $3.75" << endl;
    cout << "4   Popcorn	        $5.50" << endl;
    cout << "5   Soda		$2.80" << endl;
    cout << "6   Chips		$1.00" << endl;
    cout << "7   Water		$2.00" << endl;
    cout << "8   end order  "<< endl << endl;

    }

    
    void cost(double total, double tax, double tip, double totBill)
    
    {
    cout << "The total is: " << setprecision(2) << fixed << total << endl;
    cout << "Tip comes to: " << setprecision(2) << fixed << tip << endl;
    cout << "Tax on your purchase is: " << setprecision(2) << fixed << tax << endl;
    cout << "Your total bill is: " << setprecision(2) << fixed << totBill << endl; 
    }
    void Tcash();

int main()
{
    double total;
    double tax;
    tax = total * .065;
    double tip;
    tip = total * .20;
    double totBill;
    totBill = total + tax + tip;
    
    {//displays menu
    display();
    }

    while (item != 8)
    {
        //displays enter item until 8 is selected
    cout << "Enter Menu Item:" << endl;
    cout << "----------------" << endl;
    cin >> item;
    total+=item;
    }

    
    //assigns dollar value to items
    if (item = 1)
       {
        item = 6.00;
       }
    else if (item = 2)
       {
        item = 4.50;
       }
    else if (item = 3)
       {
        item = 3.75;
       }
    else if (item = 4)
       {
        item = 5.50;
       }
    else if (item = 5)
       {
        item = 2.80;
       }
    else if ( item = 6)
       {
        item = 1.00;
       }
    else if ( item = 7)
       {
        item = 2.00;
       }
    else if (item = 8)
    {
        item = 0;
    }

    //displays the bill
    void cost(double total, double tax, double tip, double totBill)
            
return 0;
}

Does this code even compile?

I see at least one error in the code so it shouldn't compile.

If you're getting compile errors then you need to post them, all of them, exactly as they appear in your development environment.


no compiler errors
this is the outpput error variables are not carried
8
The total is: 6.00
Tip comes to: 0.00
Tax on your purchase is: 0.00
Your total bill is: 0.00

?
These are the compiler errors
item is not declared.
Missing semicolon at line 92

There are logic errors too. Line 92 is a declaration, not a call.

Multiple places = is used instead of == in an if statement, for example line 58.
you ar e not understanding I am trying here as well. Sorry, I am also using netbeans on my linux and not codeblocks as I normally do on my Windows.
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
#include <iostream>
#include <iomanip>

using namespace std;


    void display()
    {
    cout << "*** BASEBALL GAME SNACK MENU ***" << endl;
    cout << "----------------------------------------------" << endl;
    cout << "1   Hamburger 	        $6.00" << endl;
    cout << "2   Hotdog		$4.50" << endl;
    cout << "3   Peanuts	        $3.75" << endl;
    cout << "4   Popcorn	        $5.50" << endl;
    cout << "5   Soda		$2.80" << endl;
    cout << "6   Chips		$1.00" << endl;
    cout << "7   Water		$2.00" << endl;
    cout << "8   end order  "<< endl << endl;

    }

    
    void cost(double total, double tax, double tip, double totBill)
    
    {
    cout << "The total is: " << setprecision(2) << fixed << total << endl;
    cout << "Tip comes to: " << setprecision(2) << fixed << tip << endl;
    cout << "Tax on your purchase is: " << setprecision(2) << fixed << tax << endl;
    cout << "Your total bill is: " << setprecision(2) << fixed << totBill << endl; 
    }
    
    //calculates amtTendered
    void Tcash(double totBill, double amtTendered)
    {
            cout << "enter amount tendered: " << endl;
    cin >> amtTendered;
    if (amtTendered >= totBill)
    {
        cout << "Your change is: " << amtTendered - totBill << endl;
    }
    else if (amtTendered < totBill)
    {
        cout << "Not enough tendered!!!!" << endl;
    }
    
    }

int main()
{
    double item;
    double total;
    total+=item;
    double tax;
    tax = total * .065;
    double tip;
    tip = total * .20;
    double totBill;
    totBill = total + tax + tip;
    double amtTendered;
    
    
    //displays menu
    display();
    
     while ( item !=8)
     {
     //displays enter item until 8 is selected
    cout << "Enter Menu Item:" << endl;
    cout << "----------------" << endl;
    cin >> item;
     
     }
    
    
    //assigns dollar value to items
    if (item == 1)
       {
        item = 6.00;
       }
    else if (item == 2)
       {
        item = 4.50;
       }
    else if (item == 3)
       {
        item = 3.75;
       }
    else if (item == 4)
       {
        item = 5.50;
       }
    else if (item == 5)
       {
        item = 2.80;
       }
    else if ( item == 6)
       {
        item = 1.00;
       }
    else if ( item == 7)
       {
        item = 2.00;
       }
    else if (item == 8)
    {
        item = 0;
    }
     
    //displays the bill
    void cost(double total, double tax, double tip, double totBill);

    //displays and calculates cash tendered
    void cash(double totBill, double amtTendered);



}
Last edited on
I dont believe void should be used when calling the function but I have still found no documentation on how to pass variables to functions with multiple parameters exactly in this way ???? Any ideas??? Possiblr declare const int for the values of the items?
Last edited on
Perhaps you could start with the following tutorial:
http://www.cplusplus.com/doc/tutorial/functions/
I am not using a return function and I am using variables declared locally. That tutorial is not even close to what I am doing ?
Read the tutorial, you're not going to find a tutorial that is doing exactly what you're trying to do. That tutorial tells you how to call functions that have parameters, that is what you don't seem to understand at this point. That tutorial also explains functions that have a void return type.

These are required and I cannot do it the way you are suggesting the parameter is also the int so its not working I have read that tutorial and others already that is why I am posting. That just gives me what I already have to throw gusses with , Thank you though

Requirements:
1. Functions :
a. Define a void function without parameters to display the food menu and prices
b. Define a void function with 4 parameters (bill, totBill, tax, tip) to display the bill
c. Define a void function with 2 parameters (totBill, amtTendered) to calculate and display the change due
2. Global variables may not be used. All variables used in functions must be passed by parameters or declared locally.
3. Program must be menu-driven
4. Input validation for menu item choice and amount tendered.
5. Verify that the customer tendered an amount that is equal to or greater than the total bill.
6. Output must be labelled and easy to read as shown in the sample output below. Only display 2 decimal points when displaying Total Amount Due and Change Due.
These are required and I cannot do it the way you are suggesting ....


Can I kindly point out that you can do it the way that it is being suggested.

1
2
3
4
//displays the bill
// don't put a return type when calling a function
// at the moment it is a function declaration
    void cost(double total, double tax, double tip, double totBill);


miah wrote:
I am not using a return function and I am using variables declared locally.


assignment wrote:
2. Global variables may not be used. All variables used in functions must be passed by parameters or declared locally.


So you don't have to have local variables for every function. The cost function is an example of where the variables are passed as parameters.

48
49
50
51
52
53
54
55
56
57
58
59
int main()
{
    double item; 
    double total;
    total+=item;
    double tax;
    tax = total * .065;
    double tip;
    tip = total * .20;
    double totBill;
    totBill = total + tax + tip;
    double amtTendered;


You are doing calculations before you have the values to do them with. That is before you have any code to get the values in the first place. The compiler executes the program in the order that you write the code in main, so you need to put that code which does a calculation after you get all the input.

Also, you have a variable item which you are using for 2 things: the cost of an item and the menu option. Create a new variable for the menu option.

You could have a list of variables with the prices of each one:

1
2
3
4
const double HamburgerPrice =  6.00;     
const double HotdogPrice = 4.50;
// ...
// the rest of them 


Then use those variable names in your code. If you are clever you can put all the prices into an array, then use the menu option to retrieve the value from the array. Not sure if you have learnt about arrays yet, that might be too advanced :+)

Good Luck !!
Last edited on
Pages: 12