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,
counter,
number1 = Random_num1();
number2 = Random_num2();
srand( time( 0 ) );
while ( counter != 4 )
{

cout << number1 << " + " << number2 << " = ";
counter++;
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

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,
>      counter,
> 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();

- Show quoted text -

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

> cout << number1 << " + " << number2 << " = ";
> counter++;
> 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

 Page 1 of 1 [ 2 post ]

Relevant Pages