R
Richard
I created a program which uses the srand function to generate random
numbers. The program is suppose to help an elementary student learn
multipliation and ask a question, "How much is 6 times 7?". The
problem is the random numbers are not showing up when I run the
program. Only two 0s are shown on the screen where the random numbers
should be, however it does appear to generate the random numbers when
I use a printf statement to see what they are. Also the program
should continue until the EOF is entered but it ends when the answer
is correct. Any suggestions would be appreciated.
TIA
Here is the code.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int random1(void); //function prototype for 1st random number
int question(int); //function prototype for question
int firstnum; //variable for 1st random number
int secondnum; //variable for 2nd random number
int main()
{
int response = 0; //return value of question
int resp1; //assign value of repsonse to this variable
int firstnum = random();
int secondnum = random();
int answer = firstnum * secondnum; //1st number times 2nd
number
//use a do-while loop to check the answer of the question
//if user enters the correct answer, end program, else keep
asking
//the same question until user get the question correct.
do
{
resp1 = question(response); //assign the value of
repsonse to resp1
if ( resp1 != answer )
printf("No, Please try again.\n\n");
else if ( answer == resp1 )
{
printf("Very good!\n\n");
break;
} //end else
} while ( resp1 != EOF );
return 0;
}
int random1(void) //function definition for 1st random number
{
int int1;
srand( time( NULL ) );
int1 = 1 + ( rand() % 9 );
return int1;
}
int question(int a)
{
printf("How much is %d times %d? (Enter EOF to end) ",
firstnum, secondnum );
scanf("%d", &a );
return a;
}
numbers. The program is suppose to help an elementary student learn
multipliation and ask a question, "How much is 6 times 7?". The
problem is the random numbers are not showing up when I run the
program. Only two 0s are shown on the screen where the random numbers
should be, however it does appear to generate the random numbers when
I use a printf statement to see what they are. Also the program
should continue until the EOF is entered but it ends when the answer
is correct. Any suggestions would be appreciated.
TIA
Here is the code.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int random1(void); //function prototype for 1st random number
int question(int); //function prototype for question
int firstnum; //variable for 1st random number
int secondnum; //variable for 2nd random number
int main()
{
int response = 0; //return value of question
int resp1; //assign value of repsonse to this variable
int firstnum = random();
int secondnum = random();
int answer = firstnum * secondnum; //1st number times 2nd
number
//use a do-while loop to check the answer of the question
//if user enters the correct answer, end program, else keep
asking
//the same question until user get the question correct.
do
{
resp1 = question(response); //assign the value of
repsonse to resp1
if ( resp1 != answer )
printf("No, Please try again.\n\n");
else if ( answer == resp1 )
{
printf("Very good!\n\n");
break;
} //end else
} while ( resp1 != EOF );
return 0;
}
int random1(void) //function definition for 1st random number
{
int int1;
srand( time( NULL ) );
int1 = 1 + ( rand() % 9 );
return int1;
}
int question(int a)
{
printf("How much is %d times %d? (Enter EOF to end) ",
firstnum, secondnum );
scanf("%d", &a );
return a;
}