Multiplication tester via C++

I'm working on a program that will allow my daughter to be quized by random generated multiplication facts, and let her know if she got the correct answer or not... fo some reason though Line 27 keeps telling me "number2" is not initialized, when it clearly is... any idea's why that would happen?

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
#include <stdio.h>
#include <math.h>
#include <time.h>
#include <stdlib.h>

int numbergen(void); //initializes number generator function less than 9
int multiplication(void); //initializes the multiplication of numbers

int main(void)
{
	int number1;
	int number2;
	int answer;
	int userAnswer;
	int counter=1;
	int quanity;

	srand( time(NULL));

	printf( "How many times do you want me to quiz you? " );
		scanf( "%d", &quanity );

	while ( counter <= quanity )
	{ //start while loop for quiz
		
		printf("How much is %d multiplied by $d",numbergen(number1), numbergen(number2));
		scanf("$d", &userAnswer);
	answer = number1 * number2;
		while (userAnswer != answer);
		{//start while
			printf("Try again!");
				scanf("$d", &userAnswer);
		}//end while
		if (userAnswer == answer )
		{//start if
			printf("Very Good!");
		}//end if
		++counter;
	}//end while loop

}//end main

int numbergen(int Fnumber)
{
	Fnumber = 1+(rand()%9); //generates a random number 1-9
	return Fnumber
}//end number generator




Last edited on
This is because the 'numbergen' function is taking the number by value. This means it actually performs a copy on the number, so the original value isn't modified. Also, you are never returning anything. Try making your numbergen function look like this:
1
2
3
4
int numbergen(int* number) {
    *number = (rand%9) + 1;  // generates a random number 1 - 9 (you had 0-8)
    return *number;
}


You also need to correctly modify your forwards declaration on line 6 to reflect this, as well as changing line 27 to the following:
printf("How much... ?", numbergen(&number1), numbergen(&number2));
(Obviously replacing the ellipses).

Also, just a pointer, you are actually using C, not C++ :)

EDIT:
Oh yes, and also you have an infinite loop: you never increment counter in your while loop. Just add a ++counter line somewhere in that loop (probably the end). In addition, the if statement on line 35 is redundant (it wouldn't have got there if that wasn't true).
Last edited on
Thank you NT!

I drew my code out how I thought it was going to look, without added the counters or anything yet, so thank you for finding those. I have updated the code above to fix the loop, and return a number from my function. I tried adding the ampersands into my numbers the numbergen is generating, but it doesn't like that either. *Original Code Updated with new info*
You still haven't modified your random number generator function to take a pointer to a variable rather than a copy. Also, you haven't changed your forward declaration on line 6 to reflect this:
1
2
3
4
5
6
7
8
9
/* int numbergen(void); */
int numbergen(int*);

...

int numbergen(int* number) {
    *number = (rand%9) + 1;
    return *number;
}


Then, you can change your printf function to use ellipses. I also noticed that you in some places used $d rather than %d, you should probably fix that.

On a mildly unrelated note, you seem to be sort of doing C++... you are programming in C while using C++ comments. Which do you want? C or C++? If you are going to use C++, there is a much easier way of doing this, but I won't go into it if you are going to be using C.
Thanks NT, I'm pretty sure I'm supposed to be in C, as this is all I've learned in my book so far. I'm not sure that learned about pointers rather than copy's, so I'm not sure how and why the ellipses are used. To do this normally in C, would I then need to create two number generators?

I may also be looking at this all wrong...I want my daughter to input the answer.
Next, the program checks the student’s answer. If it’s correct, display the message " Very good!" and ask another multiplication question. If the answer is wrong, display the message " No. Please try again."
and let her try the same question repeatedly until she gets it right.

A separate function should be used to generate each new question. This function should be called once when the application begins execution and each time the user answers the question correctly." So maybe I should be working on a function that does the math as well as the number generation...
Last edited on
update - I'm going to attempt to build my original code differently
Topic archived. No new replies allowed.