Function Problem 
Author Message
 Function Problem

I'm working a program that creates multiplication
problemss for an elementary student.   I've done most of
the code but since this is my first time with functions
it's a little confusing.  The problem with my code is
that it there is a runtime error and it won't create the
random numbers.  If there are any suggestions for the
program code structure fell free to tell me, I'm trying
to learn so anything is good.  Here is my code:

#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int Random_num1 ( void );
int Random_num2 ( void );
int main()
{

         int number1,
             number2,
             panswer,  
             counter,
             R_answer;
         R_answer = number1 + number2;
         number1 = Random_num1();
         number2 = Random_num2();
         srand( time( 0 ) );
         while ( counter != 4 )
         {

         cout << number1 << " + " << number2 << " = ";
         cin >> panswer;
         counter++;
         if (   panswer != R_answer )
                 cout << " You're a loser ";
         else
                 cout << " Cool!" << endl;
         }
         return 0;

Quote:
}

int Random_num1 ( void )
{
           int num1 = 1 + rand() % 9;

         return num1;

Quote:
}

int Random_num2 ( void )
{
        int num2 = 1 + rand() % 9;

        return num2;

Quote:
}



Thu, 15 Dec 2005 10:29:13 GMT  
 Function Problem
See comments intermixed.

Quote:

> I'm working a program that creates multiplication
> problemss for an elementary student.   I've done most of
> the code but since this is my first time with functions
> it's a little confusing.  The problem with my code is
> that it there is a runtime error and it won't create the
> random numbers.  If there are any suggestions for the
> program code structure fell free to tell me, I'm trying
> to learn so anything is good.  Here is my code:

> #include <iostream>
> #include <ctime>
> #include <cstdlib>
> using namespace std;
> int Random_num1 ( void );
> int Random_num2 ( void );

You can get rid of one of the two functions above - they're identical, so
there's no reason to use two different functions.

Quote:
> int main()
> {

> int number1,
>      number2,
>      panswer,
>      counter,
>      R_answer;
> R_answer = number1 + number2;
> number1 = Random_num1();
> number2 = Random_num2();
> srand( time( 0 ) );

Your logic here is backwards - remember that statements are executed in the
order they're written.

srand(time(0));
number1 = Random_num();
number2 = Random_num();
R_answer = number1 + number2;

- Show quoted text -

Quote:
> while ( counter != 4 )
> {

> cout << number1 << " + " << number2 << " = ";
> cin >> panswer;
> counter++;
> if (   panswer != R_answer )
> cout << " You're a loser ";
> else
> cout << " Cool!" << endl;
> }
> return 0;
> }
> int Random_num1 ( void )
> {
>    int num1 = 1 + rand() % 9;

> return num1;
> }

Let's rename this one to Random_num()

Quote:
> int Random_num2 ( void )
> {
> int num2 = 1 + rand() % 9;

> return num2;
> }

And get rid of this one - it's just a copy of Random_num1().

HTH

-cd



Thu, 15 Dec 2005 11:43:35 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. pointer to function problem

2. Get Printer Function Problem

3. gcc random function problem

4. VC++6. template function problem

5. C function problems during porting from UNIX to Windows

6. Beginner with function problems

7. recursive functions problems wanted

8. gets() function problem...

9. "System" function problem in ANSI-C

10. RegisterServiceProcess function problem.

11. Bioscomm function problems

12. Repeated Function Problem :(

 

 
Powered by phpBB® Forum Software