Function syntax

Hi everyone, the training scars from learning with VB are kicking in.

What is wrong with my function call/syntax?

I tried using a ref variable and it just got screwed up worse.

Thanks as always!



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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;


//main
int main()
{
	//dec vars
	int choicer (int choice);
	int ans1, ans2, choice, corr, incorr;
	int	num1, num2, num3, num4;
	int	adcnt = 0, subcnt = 0, multcnt = 0; 
	
	//intro text
	cout << '\n';
        cout << "Hello, this program will help you learn addition, subtraction and multiplication \n";
	cout << "please select one of the options from the menu to start: \n";
	cout << '\n';

	choicer(choice);
	
	  //set srand / set random numbers
		srand(time(NULL));
		num1 = rand() % 10 + 1;
		num2 = rand() % 10 + 1;
		num3 = rand() % 10 + 1;
		num4 = rand() % 10 + 1;

		//do while loop for math problems / exit program
		do
		{
	    //set switch for case choices
		switch(choice)
	      {
        //case 1 for addition
        case 1: adcnt++; 
			
			//ask addition problem
			cout << '\n';
			cout << "How much is " << num1 << " plus " << num2 << '\n';
			cout << "Enter your answer (-1 to return to the menu): \n";
			cin  >> ans1;
			//while loop for user input
			while (ans1 != num1 + num2)
             {	
				 //if statement to show summary / return to main menu
			     if (ans1 == -1)
			      {
				   cout << '\n';
				   cout << "SUMMARY: \n";
		                   cout << "Addition Problems Played: " << adcnt << '\n';
			       cout << "Number of times answered correctly: " << corr << '\n';
			       cout << "Number of times answered incorrectly: " << incorr << '\n';

				   choicer(choice);
			      }
				 //statement for incorrect answer
				   incorr++;
				   cout << '\n';
			           cout << "No. Please try again. \n";
				   cout << '\n';
				   cout << "Enter your answer (-1 to return to the menu): \n";
				   cout << "Please add " << num1 << " and " << num2 << '\n';
				   cin >> ans1;
				   incorr++;	    

             }
		   //do while loop for user input
		   do
		     {
			 //if statement for correct answer   
			   if (ans1 == num1 + num2)
			    {
				 corr++;
				 cout << '\n';
				 cout << "Very GOOD! \n";
				 cout << '\n';
				 cout << "How much is " << num3 << " plus " << num4 << '\n';
				 cout << "Enter your answer (-1 to return to the menu): \n";
				 cin >> ans1;
				 corr++;
				 cout << '\n';
				}
			         //summary / prompt for user to choose
			         cout << "SUMMARY: \n";
		                 cout << "Addition Problems Played: " << adcnt << '\n';
			         cout << "Number of times answered correctly: " << corr << '\n';
			         cout << "Number of times answered incorrectly: " << incorr << '\n';
			         cout << '\n';
				 cout << "Congratulations! You answered correctly! Choose an option! \n";

					 choicer(choice);
				 
			 }while (ans1 == num3 + num4);
		//case 2 for subtraction   
		case 2: subcnt++;
			
			//ask subtraction problem
			cout << '\n';
			cout << "How much is " << num1 << " minus " << num2 << '\n';
			cout << "Enter your answer (-1 to return to the menu): \n";
			cin  >> ans1;
		    //while loop for user input
			while (ans1 != num1 - num2)
			{
			   //if statement to show summary / return to main menu
			   if (ans1 == -1)
			    {
				 cout << '\n';
			     cout << "SUMMARY: \n";
		             cout << "Addition Problems Played: " << adcnt << '\n';
			     cout << "Number of times answered correctly: " << corr << '\n';
			     cout << "Number of times answered incorrectly: " << incorr << '\n';

				 main();
			    }
			   //statement for incorrect answer
				 incorr++;
				 cout << '\n';
			         cout << "No. Please try again. \n";
				 cout << "Enter your answer (-1 to return to the menu): \n";
				 cout << "Please add " << num1 << " and " << num2 << '\n';
				 cin >> ans1;
				 incorr++;
			      
		    }
		   //do while loop for user input
		   do
		     {
			 //if statement for correct answer   
			  if (ans1 == num1 - num2)
			   {
				corr++;
				cout << '\n';
				cout << "Very GOOD! \n";
				cout << '\n';
				cout << "How much is " << num3 << " minus " << num4 << '\n';
				cout << "Enter your answer (-1 to return to the menu): \n";
				cin >> ans1;
				corr++;
				cout << '\n';
				}
			         //summary / prompt for user to choose
			         cout << "SUMMARY: \n";
		                 cout << "Addition Problems Played: " << adcnt << '\n';
			         cout << "Number of times answered correctly: " << corr << '\n';
			         cout << "Number of times answered incorrectly: " << incorr << '\n';
			         cout << '\n';
			         cout << "Congratulations! You answered correctly! Choose an option! \n";

					 main();

			 }while (ans1 == num3 - num4);
		
		  }
        //do while loop for math problems / exit program
		}while(choice != 5);
		
    return 0;
}
int choicer (int choice2)
{
	cout << "1 Enter 1 for Addition \n";
	cout << "2 Enter 2 for Subtraction \n";
	cout << "5 Enter 5 to Exit \n";
	cout << '\n';
	cin  >> choice2;
	return choice2;
	
}
On line 165: change the fist occurrence of "int" to void" and add an ampersand before "choice2". (Why is it called choice2?)
Delete line 172.

Otherwise line 24 does nothing at all.
What happened?,
you are the same guy! >.<

http://www.cplusplus.com/forum/beginner/111280/
@eye n rique: I'm not sure how that's relevant to this topic?
Thank you!

LB: I thought the param variables had to have unique names?

eyenrique: Teacher still hasn't handed back... he's not the cream of the crop as far as teachers go lol.
Last edited on
Nevermind, got it!

Thanks again!
Last edited on
dp13 wrote:
I thought the param variables had to have unique names?
Unique with respect to what? Every variable ever in your entire program? No, they only have to be unique amongst other parameters in the same function.
Last edited on
@LB its eyenrique and
How relevant is to this topic?
Its the same exercise, ....

@dp13
I thought you understood the exercise,
maybe i am not good enough for teaching Dx
eyenrique you've helped me a ton, thanks! I wasn't talking about you, just my prof.

Thank you for the info LB & eyenrique, that's why I love this forum!


Last edited on
Topic archived. No new replies allowed.