457,938 Members | 1,578 Online Need help? Post your question and get tips & solutions from a community of 457,938 IT Pros & Developers. It's quick & easy.

# Learn Multiplicatin Program

 P: n/a 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 #include #include 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; } Nov 14 '05 #1
4 Replies

 P: n/a In article , Richard wrote: 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. int main() { int response = 0; //return value of question int resp1; //assign value of repsonse to this variable int firstnum = random(); int secondnum = random(); You've made a typo here, you need to call your own random function here which is called 'random1'. Regards, -- Rob van der Leek | rob(at)ricardis(dot)tudelft(dot)nl Nov 14 '05 #2

 P: n/a Richard wrote: #include #include #include 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 GLOBAL declaration/scope int main() { int response = 0; //return value of question int resp1; //assign value of repsonse to this variable int firstnum = random(); int secondnum = random(); LOCAL declaration/scope int answer = firstnum * secondnum; This is a variable scope issue. If two variables have the same name but one is decalred globally and the other locally, the local variable is used. In your case, the global firstnum and secondnum (declared OUTSIDE main()) do not get initialized by random1() but the local firstnum and secondnum (declared INSIDE main()). If you have K&R2, re-read the section on variable scope. Drew Nov 14 '05 #3

 P: n/a ra******@excite.com (Richard) writes: [...] int random1(void) //function definition for 1st random number { int int1; srand( time( NULL ) ); int1 = 1 + ( rand() % 9 ); return int1; } [...] You're calling srand() once for each call to rand(). Don't do that. Call srand() once at the beginning of your program to initialize the generator, then call rand() as many times as you need to. (I think the C FAQ covers this, but I can't get to it at the moment; try .) -- Keith Thompson (The_Other_Keith) ks***@mib.org San Diego Supercomputer Center <*> We must do something. This is something. Therefore, we must do this. Nov 14 '05 #4 