What's wrong with my program? (Basic Function Calling)

I need to write a program that generates a random question ten times. The program then has to be organized using function calls, as well as, keep track of the amount of right and wrong answers.
My issue is that when I run the program the "for()" loop doesn't even run. It simply displays the title and my name and then promptly ends the program. I made the mistake of foolheartedly writing the program in one go and tried to find out what was causing the issue but I was stumped.
I hope the code is easy to read and thank you to anyone who takes the time to look through it. (Though I'm sure there have been people who've posted hundreds of lines of code before...)
The code itself is run on Visual Studio 2019.
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
#include <iostream>
#include <time.h>
#include <stdlib.h>
#include <cassert>

using namespace std;
void randwrong() {
	int number;

	number = rand() % 4;

	if (number == 0) {

		cout << "No. Please try again.\n";
	}
	if (number == 1) {

		cout << "Wrong. Try once more.\n";
	}
	if (number == 2) {

		cout << "Don't give up!\n";
	}
	if (number == 3) {

		cout << "No. Keep trying.\n";
	}
}
void randcorrect() {
	int number;

	number = rand() % 4;

	if (number == 0) {

		cout << "Very good!\n";
	}
	if (number == 1) {

		cout << "Excellent!\n";
	}
	if (number == 2) {

		cout << "Nice work!\n";
	}
	if (number == 3) {

		cout << "Keep up the good work!\n";
	}
}
int randomQuestion(int correctanswer) {
	int number = rand() % 10;

	if (number == 0) {

		cout << "What is 6 * 5? ";
		correctanswer = 30;
	}
	if (number == 1) {

		cout << "What is 7 * 3? ";
		correctanswer = 21;
	}
	if (number == 2) {

		cout << "What is 2 * 5? ";
		correctanswer = 10;
	}
	if (number == 3) {

		cout << "What is 9 * 4? ";
		correctanswer = 36;
	}
	if (number == 4) {

		cout << "What is 5 * 3? ";
		correctanswer = 15;
	}
	if (number == 5) {

		cout << "What is 3 * 11? ";
		correctanswer = 33;
	}
	if (number == 6) {

		cout << "What is 4 * 6? ";
		correctanswer = 24;
	}
	if (number == 7) {

		cout << "What is 6 * 8? ";
		correctanswer = 48;
	}
	if (number == 8) {

		cout << "What is 7 * 9? ";
		correctanswer = 63;
	}
	if (number == 9) {

		cout << "What is 3 * 4? ";
		correctanswer = 12;
	}
	return correctanswer;
}

int main()
{
	cout << "      Mulitplication Table Practice\n";
	cout << "	  by MY NAME\n\n";
	int rightanswer{}, answer;
	double correctcount = 0, percentage;

	for (int total = 0; total > 10; total++) {

		cout << "ENTER -1 TO QUIT\n";
		randomQuestion(rightanswer);
		cin >> answer;

		if (answer == rightanswer) {

			randcorrect();
			correctcount++;
		}

		else if (answer == -1) {
			return 0;
		}
		else {
			randwrong();
		}
	}
}


I then have a few things to calculate and display at the end like the percentage grade and the closing statements but they weren't apart of the issue so I didn't include them.
Last edited on
My issue is that when I run the program the "for()" loop doesn't even run
for (int total = 0; total > 10; total++)
Your for loop is never entered because the looping condition is never true.
You meant total < 10;

Other issues:
- You never use the value returned from your randomQuestion function.
- in your randomQuestion function, you shouldn't have any input parameters, because you overwrite the value anyway. Just declare correctanswer as local int variable within the function, not as a parameter.

1
2
3
4
5
6
int randomQuestion()
{
    int correctanswer = 0;
    // ...
    return correctanswer;
}
Last edited on
Thank you again for helping me Ganado. (Both times I've posted you've answered my question)
I was wondering if you could clarify what you meant in your second half about the return value.
This is where I use the returned value and when the function call is edited to your specification I get the same error of never having the right answer. Even when I use the original code I had written I find that the program is struggling to have a correct answer to compare to.

1
2
3
4
5
if (answer == correctanswer) {

			randcorrect();
			correctcount++;
		}


when I ask the program to display what it uses as the correct answer I get 0.
Last edited on
 
int randomQuestion(int correctanswer) {

correcvtanswer is pased by value - so any changes done to it within the functioin aren't passed back to the calling function. There's no need to have a parameter here.

1
2
3
4
int randomQuestion() {
    int correctanswer {};
.....
}


Then in main:

1
2
3
cout << "ENTER -1 TO QUIT\n";
randomQuestion(rightanswer);
cin >> answer;


becomes:
1
2
3
cout << "ENTER -1 TO QUIT\n";
rightanswer = randomQuestion();
cin >> answer;


Topic archived. No new replies allowed.