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?
#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
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.
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)
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
^
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
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.
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
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