Having trouble adding random numbers

N

Naya

Hi. I am working on a math tutoring program which generates two random
numbers (from 1 to 500) and asks the user to add them. How can I check
to see whether or not they put in the correct total?? How do you add
random numbers that are generated anyway??

#include <iostream>
#include <cstdlib>
#include <time>
#include <conio>
using namespace std;

int main()
{
unsigned seed;

cout << "Enter a seed value: " ;
cin >> seed;
srand(time(0));
cout << rand()%500 << endl;
cout << rand()%500 << endl;
cout << rand()%500 + rand()%500;
getch();
return 0;
}
Enter a seed value: 1
385
425
644
 
G

Gianni Mariani

Naya said:
Hi. I am working on a math tutoring program which generates two random
numbers (from 1 to 500) and asks the user to add them. How can I check
to see whether or not they put in the correct total?? How do you add
random numbers that are generated anyway??

You need to store the random numbers in a variable. Once they are
stored, they're no longer random.
 
M

Mike Wahler

Naya said:
Hi. I am working on a math tutoring program which generates two random
numbers (from 1 to 500) and asks the user to add them. How can I check
to see whether or not they put in the correct total?? How do you add
random numbers that are generated anyway??

Same way you'd add any two numbers; it doesn't
matter where they came from.

int r(rand() + rand());

-Mike
 
J

Jim Langston

Naya said:
Hi. I am working on a math tutoring program which generates two random
numbers (from 1 to 500) and asks the user to add them. How can I check
to see whether or not they put in the correct total?? How do you add
random numbers that are generated anyway??

#include <iostream>
#include <cstdlib>
#include <time>
#include <conio>
using namespace std;

int main()
{
unsigned seed;

cout << "Enter a seed value: " ;
cin >> seed;
srand(time(0));
cout << rand()%500 << endl;

int Rand1 = rand() % 500;
cout << Rand1 << endl;

You should be able to figure out the rest from that.
 
N

Naya

Same way you'd add any two numbers; it doesn't
matter where they came from.

int r(rand() + rand());

-Mike

When I do that, I get an error. Here's my program:
int main()
{
unsigned seed;
int r(rand()+ rand());



cout << "Enter a seed value: " ;
cin >> seed;

srand(time(0));
cout << rand()%500 <<endl;
cout << rand()%500 <<endl;
r(rand() + rand());
getch();
return 0;
}

When I did that, I got two errors:
Call to nonfunction, and
R is assigned a value that is never used

Please excuse me if I seem dumb, but I am really new at C++; so don't
be too hard on me :):)
 
Z

Zeppe

Naya said:
Hi. I am working on a math tutoring program which generates two random
numbers (from 1 to 500) and asks the user to add them. How can I check
to see whether or not they put in the correct total?? How do you add
random numbers that are generated anyway??

Your problem is that you have to generate only two random numbers, and
then calculate the sum. So you declare two variables

int rand1;
int rand2;

then you assign a rand() % 500 to each of them, and then you sum up
rand1 + rand2.

Regards,

Zeppe
 
M

Mike Wahler

Naya said:
When I do that, I get an error. Here's my program:

Where are the headers?
int main()
{
unsigned seed;
int r(rand()+ rand());

Here, 'rand()' is called, but you haven't seeded the
generator yet. You'll get the same values every time
you run the program.

The above line calls 'rand()' twice, adds the two
results and initializes the type 'int' object with
that sum.
cout << "Enter a seed value: " ;
cin >> seed;

Here, you request a seed value, but you never use it.
Not really an 'error', but pointless.
srand(time(0));

Here, you use time as the seed, which is a very
commone way to do it.
cout << rand()%500 <<endl;
cout << rand()%500 <<endl;
r(rand() + rand());

This statement is trying to call a function named 'r()',
but no such function has been declared.

Note that 'getch()' is not part of standard C++. Use
cin.get() or 'getchar()'.
return 0;
}

When I did that, I got two errors:
Call to nonfunction, and

Which is true. There's no function 'r'.
R is assigned a value that is never used

That's true as well. Again, not really an 'error',
just rather pointless to create a value and never use it.
This warning is referring to the 'r' in the second line
of 'main()'.
Please excuse me if I seem dumb, but I am really new at C++; so don't
be too hard on me :):)

I hope I wasn't. BTW which C++ book(s) are you using?

-Mike
 
N

Naya

Mike said:
Where are the headers?


Here, 'rand()' is called, but you haven't seeded the
generator yet. You'll get the same values every time
you run the program.

The above line calls 'rand()' twice, adds the two
results and initializes the type 'int' object with
that sum.


Here, you request a seed value, but you never use it.
Not really an 'error', but pointless.


Here, you use time as the seed, which is a very
commone way to do it.


This statement is trying to call a function named 'r()',
but no such function has been declared.


Note that 'getch()' is not part of standard C++. Use
cin.get() or 'getchar()'.


Which is true. There's no function 'r'.


That's true as well. Again, not really an 'error',
just rather pointless to create a value and never use it.
This warning is referring to the 'r' in the second line
of 'main()'.


I hope I wasn't. BTW which C++ book(s) are you using?

-Mike

Well, I am using Starting Out With C++ Early Objects 5th ed. by Tony
Gaddis, Judy Walters, and Godfry Muganda. The program that I am doing
is a math tutor program that requires me to create a program that
would:
(1) generate two random numbers
(2) ask the user to input their answer of the sum of the two random
numbers
(3) If the user's answer is correct, it dispays Congratulations! on
the screen.
(4) If the user's answer is not correct, it displays the correct
score.

I know that I may have the use the if else if statement in this
program. What do you think??
 
G

Gianni Mariani

Naya said:
Mike Wahler wrote: ....
I know that I may have the use the if else if statement in this
program. What do you think??

I could write it without an if statement but that is irrelevant.

Here is a hint - find two random numbers:

int v1 = rand();
int v2 = rand();


add them

v1+v2

compare them

(v1+v2)==v3

Try to put the rest together yourself.
 
N

Naya

Gianni said:
I could write it without an if statement but that is irrelevant.

Here is a hint - find two random numbers:

int v1 = rand();
int v2 = rand();


add them

v1+v2

compare them

(v1+v2)==v3

Try to put the rest together yourself.

I tried that, and this is what happened:
#include <iostream>
#include <cstdlib>
#include <time>
#include <conio>
using namespace std;

int main()
{

int v1 = rand()%500,
v2 = rand()%500,
v3;

cout << "\t\tMATH TUTOR 101\n\n";
cout << "\t\t ***********\n\n";
cout << "Here are your two numbers. " << endl;

srand(time(0));
cout << rand()%500 <<endl;
cout << rand()%500 <<endl;

v3 = v1 + v2;

cout << "The sum of these two values are" <<v3 <<endl;


getch();
return 0;
}

When I tried that, here was my output:
MATH TUTOR 101

***********

Here are your two numbers.
15
421
The sum of these two values are612

My sum stays the same no matter what numbers are put in. I tried, but
I couldn't get it to work.
 
V

Victor Bazarov

Naya said:
[..]
I tried that, and this is what happened:
#include <iostream>
#include <cstdlib>
#include <time>
#include <conio>
using namespace std;

int main()
{

int v1 = rand()%500,
v2 = rand()%500,

So, here you initialise your 'v1' and 'v2' with some random
numbers. That's very good.
v3;

cout << "\t\tMATH TUTOR 101\n\n";
cout << "\t\t ***********\n\n";
cout << "Here are your two numbers. " << endl;

srand(time(0));
cout << rand()%500 <<endl;
cout << rand()%500 <<endl;

And here you output, what, two *other* random numbers? Don't
you want to actually output 'v1' and 'v2'?
v3 = v1 + v2;

cout << "The sum of these two values are" <<v3 <<endl;


getch();
return 0;
}

When I tried that, here was my output:
MATH TUTOR 101

***********

Here are your two numbers.
15
421
The sum of these two values are612

My sum stays the same no matter what numbers are put in. I tried, but
I couldn't get it to work.

Well, you might want to think about random numbers and what calling
'rand()' does...

V
 
M

Mike Wahler

Naya said:
I tried that, and this is what happened:
#include <iostream>
#include <cstdlib>
#include <time>
#include <conio>
using namespace std;

int main()
{

Call 'srand()' here.
int v1 = rand()%500,
v2 = rand()%500,

Now you have two random numbers.
v3;

cout << "\t\tMATH TUTOR 101\n\n";
cout << "\t\t ***********\n\n";
cout << "Here are your two numbers. " << endl;

srand(time(0));
cout << rand()%500 <<endl;
cout << rand()%500 <<endl;

... so why generate two more?
v3 = v1 + v2;

cout << "The sum of these two values are" <<v3 <<endl;


getch();
return 0;
}

When I tried that, here was my output:
MATH TUTOR 101

***********

Here are your two numbers.
15
421
The sum of these two values are612

My sum stays the same no matter what numbers are put in. I tried, but
I couldn't get it to work.

As I wrote in my other reply:

'rand()' is called, but you haven't seeded the
generator yet. You'll get the same values every time
you run the program.

Call 'srand()' *before* calling 'rand()' if you want
different numbers each time.

-Mike
 
N

Naya

Mike said:
Call 'srand()' here.


Now you have two random numbers.


.. so why generate two more?


As I wrote in my other reply:

'rand()' is called, but you haven't seeded the
generator yet. You'll get the same values every time
you run the program.

Call 'srand()' *before* calling 'rand()' if you want
different numbers each time.

-Mike

This problem has been SOLVED.
Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top