Proper function placement assistance

Can someone look at this code and tell me if I am on the right track? I am trying to modularize this program using three functions, besides the MAIN

Its obviously wrong because it won't compile for me. Any help would be appreciated.
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
/*
Week 4 Program

Lets continue with our Coke Machine program from week 3. 
Now, you will need to add 3 functions to it and move the 
appropriate code into them. Create an InsertMoney function 
and move all of the money insertion code into it. Create a 
Menu function that will display the drink choices and Create 
a Change function that will give the user his or her change.

Here’s how the main function will look

main()

 //Beginning of loop

Call the insert money function

Call the DisplayMenu function

Call the GiveChange function

//end of loop

//end of main
 
At this point, a menu( which is also a separate function) will
display saying something like:
 
What would you like?

Press 1 for Coke

Press 2 for Dr. Pepper

Press 3 for Pepsi

Once they make their choice, you can dispense the drink and any
change that they may owe. Lets use a function for this also. 

To be clear, I’m asking you to create three functions, not counting Main.

1.    You should have one function to accept and keep track of the money.

2.    One function to display the menu

3.    One function to dispense the drink and give the change

You may choose to add more functions to your program. If you wish to do so, 
or if you wish to have one function call another one rather than having to 
return to main each time, that’s fine.
*/

#include <iostream>
using namespace std;
void insertMoney();
void displayMenu();
void giveChange();

int main()
{
insertMoney();
displayMenu();
giveChange();
return 0;
}

void insertMoney()
{
	// declare and initiate variables and one array
	double selection = 0;
	double sodaPrice = 1.00;
	double inMachine = 0;
	double newDep = 0;
	double change = 0;
	char more = 'y';
    int a[] = {0,0,0};
  

    
while (more == 'y')
    {
    // This while loop will keep executing until the logic is false
    while (inMachine < sodaPrice)
    {
    // Outputing to console display and passing inputs to variables
    cout << "Please insert $" << sodaPrice << endl;
    cout << "You have deposited: $" << inMachine << endl;
    // Subtracting the amount in the machine from the cost of the soda
    cout << "Amount Needed: $" << (sodaPrice - inMachine) << endl;
    cout << "Amount you wish to deposit: $";
    cin >> newDep;
    inMachine = (newDep + inMachine);
    //Some fancy CLEAR SCREENS
    system("CLS");
    }

void displayMenu()
{
    cout << "Thank you! Please select a type of soda:" << endl << endl;
    cout << "Coke              -  " << 1 << endl;
    cout << "Pepsi             -  " << 2 << endl;
    cout << "Dr. Pepper        -  " << 3 << endl << endl;
    cin >> selection;
    
    if (selection == 1)
    {
    // Incrementing the array for each soda bought
    a[0] = a[0]+1;
    cout << "Thank you and enjoy your Coke!" <<endl;
    } 
    else if (selection == 2)
    {
    a[1] = a[1]+1;
    cout << "Thank you and enjoy your Pepsi!" <<endl;
    }
    else if (selection == 3)
    {
    a[2] = a[2]+1;
    cout << "Thank you and enjoy your Dr. Pepper!" <<endl;
    }
}      
void giveChange()
{
    if (inMachine > sodaPrice)
	{
    cout << "Your change is: $" << (inMachine - sodaPrice) << endl;
             }
    // Clearing the variables
    inMachine = 0;
    selection = 0;
    cout << "Would you like to purchase another soda? Y or N ";
    cin >> more;
        system("CLS");
         }
         // Displaying the totals of soda bought
         cout << "You have purchased " << a[0] << " Coke(s)" << endl;
         cout << "You have purchased " << a[1] << " Pepsi(s)" << endl;
         cout << "You have purchased " << a[2] << " Dr. Pepper(s)" << endl;
         system("PAUSE");
 }
Put two braces after line 96.

Delete the brace in line 135.
I added two '}' braces after 96 and deleted the one '}' on 135.

Did you compile and run this or just a visual inspection? Because I get the following errors:

1
2
3
4
5
6
7
8
9
10
11
 C:\Documents and Settings\Doug\Desktop\SodaMachineWithFuncs_CPP.cpp In function `void insertMoney()': 

98 C:\Documents and Settings\Doug\Desktop\SodaMachineWithFuncs_CPP.cpp expected primary-expression before "void" 

98 C:\Documents and Settings\Doug\Desktop\SodaMachineWithFuncs_CPP.cpp expected `;' before "void" 

123 C:\Documents and Settings\Doug\Desktop\SodaMachineWithFuncs_CPP.cpp expected primary-expression before "void" 

123 C:\Documents and Settings\Doug\Desktop\SodaMachineWithFuncs_CPP.cpp expected `;' before "void" 

141 C:\Documents and Settings\Doug\Desktop\SodaMachineWithFuncs_CPP.cpp expected `}' at end of input 
I just did a quick visual inspection first.

After compiling it I found that you need to move all the variables from inside the insertMoney function to outside the main function.

You could pass the variables into the function by reference to eliminate the global variables.

Here is the fixed code:
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
#include <iostream>
using namespace std;

// declare and initiate variables and one array
	double selection = 0;
	double sodaPrice = 1.00;
	double inMachine = 0;
	double newDep = 0;
	double change = 0;
	char more = 'y';
    int a[] = {0,0,0};

void insertMoney();
void displayMenu();
void giveChange();

int main()
{
	
insertMoney();
displayMenu();
giveChange();
return 0;
}

void insertMoney()
{
    
while (more == 'y')
    {
    // This while loop will keep executing until the logic is false
    while (inMachine < sodaPrice)
    {
    // Outputing to console display and passing inputs to variables
    cout << "Please insert $" << sodaPrice << endl;
    cout << "You have deposited: $" << inMachine << endl;
    // Subtracting the amount in the machine from the cost of the soda
    cout << "Amount Needed: $" << (sodaPrice - inMachine) << endl;
    cout << "Amount you wish to deposit: $";
    cin >> newDep;
    inMachine = (newDep + inMachine);
    //Some fancy CLEAR SCREENS
    system("CLS");
    }
	}
}


void displayMenu()
{
    cout << "Thank you! Please select a type of soda:" << endl << endl;
    cout << "Coke              -  " << 1 << endl;
    cout << "Pepsi             -  " << 2 << endl;
    cout << "Dr. Pepper        -  " << 3 << endl << endl;
    cin >> selection;
    
    if (selection == 1)
    {
    // Incrementing the array for each soda bought
    a[0] = a[0]+1;
    cout << "Thank you and enjoy your Coke!" <<endl;
    } 
    else if (selection == 2)
    {
    a[1] = a[1]+1;
    cout << "Thank you and enjoy your Pepsi!" <<endl;
    }
    else if (selection == 3)
    {
    a[2] = a[2]+1;
    cout << "Thank you and enjoy your Dr. Pepper!" <<endl;
    }
}      
void giveChange()
{
    if (inMachine > sodaPrice)
	{
    cout << "Your change is: $" << (inMachine - sodaPrice) << endl;
             }
    // Clearing the variables
    inMachine = 0;
    selection = 0;
    cout << "Would you like to purchase another soda? Y or N ";
    cin >> more;
    system("CLS");
         // Displaying the totals of soda bought
         cout << "You have purchased " << a[0] << " Coke(s)" << endl;
         cout << "You have purchased " << a[1] << " Pepsi(s)" << endl;
         cout << "You have purchased " << a[2] << " Dr. Pepper(s)" << endl;
         system("PAUSE");
 }
Last edited on
Topic archived. No new replies allowed.