Multiplication

Pages: 12
Can you guys find anything wrong with line 28? I'm trying to get it to do the math to find out what the answer is, but it keeps "failing" because answer always = 0 from my original initiated integer. any ideas?
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
#include <stdio.h>
#include <math.h>
#include <time.h>
#include <stdlib.h>

int numbergen(int&); //initializes number generator function less than 9

int main(void)
{
	int number1=0;
	int number2=0;
	int answer=0;
	int userAnswer=0;
	int totalQuestions=0;
	int countWrong=0;
	int countCorrect=0;

	srand( time(NULL));

	printf( "Welcome to the Multiplication tester, enter -1 to quit! " );
		
	while ( userAnswer != -1 )
	{ //start while loop for quiz
		
		printf("\nWhat is %d multiplied by %d?\n",numbergen(number1), numbergen(number2));
		scanf( "%d", &userAnswer );
		answer=number1*number2;
		printf("answer is %d\n", answer);
		while (userAnswer != answer)
		{//start while
			printf("Try again!");
				scanf("%d", &userAnswer);
				++countWrong;//used to determine grade at the end(I thought this would be cool to do)
					++totalQuestions;//used to determine grade at the end(I thought this would be cool to do)
			}//end while
		if (userAnswer == answer )
		{//start if
			printf("Very Good!");
			++countCorrect;//used to determine grade at the end(I thought this would be cool to do)
				++totalQuestions;//used to determine grade at the end(I thought this would be cool to do)
		}//end if
		
	}//end while loop
	if (answer = -1)
	{
		printf("You got %d Wrong and %d Correct!", countWrong, countCorrect);
		printf("you got a %d!", countCorrect/totalQuestions);
	}
	getchar();
}//end main

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


I Updated the code to use a kill character rather than counters
Last edited on
The problem is lines 23 and 41. You're not passing Fnumber by reference in the call to numbergen(). Therefore number1 and number2 never get updated and the result of the multiplication on line 25 is always 0.
You're passing number1 and number2 to the function numbergen by value, which means a copy of the value in the variable (i.e. 0) is being passed to the function. The function then returns a value from Fnumber, but that doesn't change the original value of number1 or number2 in the main function. So when you multiply number1 * number2 to get the answer, you're multiplying 0 by 0.

If you want the numbergen function to be able to change the original value in number1 and number2, you can pass by reference using the & operator instead of by value. e.g. int numbergen(int &Fnumber)

Your function prototype should reflect that the function takes one parameter by reference int numbergen(int&); - right now it's int numbergen(void); meaning that it takes no parameters.
Last edited on
how do I get it to update? something simple?
You can pass by reference using the reference operator or by creating pointers to the variable to be updated.

There's a section on functions on this site which has more detail on passing by value vs. passing by reference. http://www.cplusplus.com/doc/tutorial/functions/ (scroll down a bit)
I'm not sure what half that stuff means.... I'm pretty new, can you show me what the code would look like?
will I be doing something like this:
printf("\nWhat is %d multiplied by %d?\n",numbergen(number1&), numbergen(number2&))

Or do I have to use two variables in the numbergenerator? and put the ampersands in the intFnumber ?
?
Last edited on
function prototype at the top - tells the compiler the function will take a reference to an integer type variable
int numbergen(int&);

function call - stays the same
printf("\nWhat is %d multiplied by %d?\n",numbergen(number1), numbergen(number2));

function itself - tells compiler that Fnumber is not a new variable, just a reference (or alias) to the original variable. So any change to Fnumber is a change to the original variable.
int numbergen(int& Fnumber)
it doesn't let me do that, I get syntax errors stating "missing ) before & and missing { before &

I updated my code with what I got so far.
Your updated code compiles OK for me using ideone.com. Which compiler are you using?
Visual Studio Express 2012
I'm using C also not C++ if that makes a difference

on ideone.com
I get:

Compilation error comments (0)
stdin copy
Standard input is empty
compilation info
prog.c:6:18: error: expected ‘;’, ‘,’ or ‘)’ before ‘&’ token
int numbergen(int&); //initializes number generator function less than 9
^
prog.c: In function ‘main’:
prog.c:25:1: warning: implicit declaration of function ‘numbergen’ [-Wimplicit-function-declaration]
printf("\nWhat is %d multiplied by %d?\n",numbergen(number1), numbergen(number2));
^
prog.c:44:1: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
if (answer = -1)
^
prog.c: At top level:
prog.c:52:18: error: expected ‘;’, ‘,’ or ‘)’ before ‘&’ token
int numbergen(int& Fnumber)
^
prog.c: In function ‘main’:
prog.c:50:1: warning: control reaches end of non-void function [-Wreturn-type]
}//end main
^
Last edited on
It also looks like one of my while loops throws an infinite loop... but I'm not sure why..
Ah OK - looks like C does not support the pass by reference that C++ does. Looks like in C you would need to use a pointer -I found this tutorial on C pointers here http://www.cprogramming.com/tutorial/c/lesson6.html

This is what I got with the code in ideone.
http://ideone.com/8DjaPq
is what I'm doing possible with a pointer? Or do I need to rebuild my code, so that the multiplication and answer happen within the function?
Last edited on
I don't see why you couldn't do this with pointers in C. You could certainly use pointers in C++ to do it.

The infinite loop in ideone is because you can't (or I haven't figured out how to :)) interact with the program to keep trying to input the right answer.
what would my pointer code look like? Similiar t the ampersands? would this update my number1 and number2 values?
Since Arrary reference by default in C, I tried to use number1 and number2 as elements in an arrary:

#include <stdio.h>
#include <math.h>
#include <time.h>
#include <stdlib.h>

int numbergen(int Fnumber); //initializes number generator function less than 9

int main(void)
{
int n[2]={0,0};
int answer=0;
int userAnswer=0;
int totalQuestions=0;
int countWrong=0;
int countCorrect=0;

srand( time(NULL));

printf( "Welcome to the Multiplication tester, enter -1 to quit! " );

while ( userAnswer != -1 )
{ //start while loop for quiz

printf("\nWhat is %d multiplied by %d?\n",numbergen(n[0]), numbergen(n[1]));
scanf( "%d", &userAnswer );

answer = n[0] * n[1];

printf("answer is %d\n", answer);
while (userAnswer != answer)
{//start while
printf("Try again!");
scanf("%d", &userAnswer);
++countWrong;//used to determine grade at the end(I thought this would be cool to do)
++totalQuestions;//used to determine grade at the end(I thought this would be cool to do)
}//end while

if (userAnswer == answer )
{//start if
printf("Very Good!");
++countCorrect;//used to determine grade at the end(I thought this would be cool to do)
++totalQuestions;//used to determine grade at the end(I thought this would be cool to do)
}//end if

}//end while loop

if (answer = -1)
{
printf("You got %d Wrong and %d Correct!", countWrong, countCorrect);
printf("you got a %d!", countCorrect/totalQuestions);
}
getchar();

}//end main

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

}//end number generator


But I still have the same issue, answer always = 0
Last edited on
I'm usually coding in C++, but looking through the tutorial, pointer syntax in C seems to be the same as in C++. A pointer variable stores a memory address. The & operator in the context of using pointers will return the memory address of a variable. The * operator used with a pointer will return the value of the object that the pointer points to.

//declare variable as usual
int number1=0;
//declare pointer variable. The * tells the compiler that this is a pointer to an integer type variable. The assignment statement initializes the pointer to point to the memory address of variable number1.
int* ptrnumber1 = &number1;

//function prototype
int numbergen(int*);

//function call - send the pointer
numbergen(ptrnumber1)

//function
1
2
3
4
5
int numbergen(int* Fnumber)
{
	*Fnumber = 1+(rand()%9); //generates a random number 1-9
	return *Fnumber;
}//end number generator  
I don't see why you have to use pointers/references here at all.
You just need this:
1
2
3
4
int numbergen() // No need to pass anything
{
    return 1 + (rand() % 9);
}

Then just use the return value in your code:
22
23
24
25
26
27
28
29
while ( userAnswer != -1 )
{ //start while loop for quiz
    number1 = numbergen();
    number2 = numbergen();
    printf("\nWhat is %d multiplied by %d?\n", number1, number2);
    scanf( "%d", &userAnswer );
    answer=number1*number2;
    printf("answer is %d\n", answer);
Pages: 12