### psudo random number generator help

Hey there again my fellow coders;

I seem to be getting stuck on something. Here is what I've got to do:

1. seed the random number generator
2. generate a target number between 0 and 99
3. loop
a. prompt the user to enter a guess between 0 and 99
c. if the guess and the target number are equal
i. print a success message (e.g., “Right” or “You Win”)
ii. break out of the loop
d. if the guess is less than 0, break out of the loop (a way to quit early)
e. if the guess is less than the target number, print “Low”
f. if the guess is greater than the target number, print “High”

here is what i have so far:

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455`` ``````#include #include #include using namespace std; int main () { int Guess; int Answer; srand((unsigned)time(NULL)); // seed the generator int target = rand() % 100; // numbers [0..99] do { cout << "Pick a number between 0 and 99: " << endl; cin >> Guess; if (Guess == Answer) cout << "Correct! You Win!!"<< endl; exit (0); else (Guess = 0) cout << "Pick a number larger than Zero; Terminating program" < Answer) cout } return 0; }``````

im getting an error with my else if or else ( and at the bottom too ) i don't know how to fix it
You should enclose in braces the code that belongs to if or else. For example instead of

 ``123`` `````` if (Guess == Answer) cout << "Correct! You Win!!"<< endl; exit (0);``````

you have to write

 ``12345`` `````` if (Guess == Answer) { cout << "Correct! You Win!!"<< endl; exit (0); }``````

The else statement may not have a condition. It is if else that may have a condition.
Last edited on
This is what I have now; what am I doing wrong?

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546`` ``````#include #include #include using namespace std; int main () { int Guess; int Answer; srand((unsigned)time(NULL)); // seed the generator int target = rand() % 100; // numbers [0..99] do { cout << "Pick a number between 0 and 99: " << endl; cin >> Guess; if (Guess == Answer) { cout << "Correct! You Win!!"<< endl; exit (0); } else if (Guess <= 0) { cout << "Pick a number larger than Zero; Terminating program" < Answer) { cout << "Your guess is higher than the number" << endl; } else if (Guess < Answer) { cout << "Your guess is lower than the number" << endl; while (Guess != 0); return 0; } }``````
Man, this is the exact program I have to do too, except I have to do it in Pep/8.

Edit: Other stuff I said was covered, so I'm just gonna delete it.

Your while condition should test for if the guess is less than 0, not just not equal to 0.

You're missing the closing brace of your last else if.
Last edited on
I think I've gotten some of it fixed;

 ``123456789101112131415161718192021222324252627282930313233343536373839`` ``````#include #include #include using namespace std; int main () { int Guess; srand((unsigned)time(NULL)); // seed the generator int Answer = rand() % 100; // numbers [0..99] do { cout << "Pick a number between 0 and 99: " << endl; cin >> Guess; if (Guess == Answer) { cout << "Correct! You Win!!"<< endl; exit (0); } else if (Guess <= 0) cout << "Pick a number larger than Zero; Terminating program" < Answer) cout << "Your guess is higher than the number" << endl; if (Guess < Answer) cout << "Your guess is lower than the number" << endl; } while (Guess != 0); return 0; }``````

no errors; but when it runs and i type a number it terminates
 ``12345678910`` ``````else if (Guess <= 0) // Need braces here cout << "Pick a number larger than Zero; Terminating program" < Answer) // Should be else if cout << "Your guess is higher than the number" << endl; if (Guess < Answer) // Should be else if cout << "Your guess is lower than the number" << endl;``````
Last edited on
when i do that I get the error:

Error: expected a statement

so now im really lost
try making it this way

 ``12345678910111213141516`` ``````if(guess==answer) { //bla,bla,bla,bla... } else if(guess<0) { //bla,bla,bla,bla... } else if(guess>answer) { //bla,bla,bla,bla... } else if(guess

REMEMBER to put the brackets
I think it runs; which is great news. I just need to test it
Thanks everyone

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243`` ``````#include #include #include using namespace std; int main () { int Guess; srand((unsigned)time(NULL)); // seed the generator int Answer = rand() % 100; // numbers [0..99] do { cout << "Pick a number between 0 and 99: " << endl; cin >> Guess; if (Guess == Answer) { cout << "Correct! You Win!!"<< endl; exit (0); } else if (Guess < 0) { cout << "Pick a number larger than Zero; Terminating program" < Answer) { cout << "Your guess is higher than the number" << endl; } else if (Guess < Answer) { cout << "Your guess is lower than the number" << endl; } } while (Guess != 0); return 0; }``````

so now I'm running into another issue; no matter what number i place its saying the number is lower; not sure what I can do to fix that one
Last edited on
Are you sure? because I just tried it and work perfectly
yes; because when I enter 1, it says lower... im gonna run it a few times
I think Visual Studio 2010 Professional just hates me:

fatal error LNK1169: one or more multiply defined symbols found
1>
1>Build FAILED.
jajajajajajaja must be a Visual Studio issue because I'm using Code Blocks and works fine
No, it's not visual studio.

You have a symbol multiply defined. There should be getting another error above the one you posted. That one will tell you what symbol is multiply defined. Can you post that error?
It has to be, I only use VS2010 because thats what the professor uses in class and grades with; oh and it was free... lol. I'm just gonna stick with what i have :)
I mean the problem is not with visual studio. You have a bug in your code and/or your project settings.

Don't blame the tool. The problem is not with the tool.

What's the full error message?
1>------ Build started: Project: guess, Configuration: Debug Win32 ------
1>Build started 9/21/2012 5:40:01 PM.
1>InitializeBuildStatus:
1> Touching "Debug\guess.unsuccessfulbuild".
1>ClCompile:
1> guess.cpp
1>ManifestResourceCompile:
1> All outputs are up-to-date.
1>guess_2.obj : error LNK2005: _main already defined in guess.obj
1>C:\Users\Colby Crowder\Documents\Visual Studio 2010\Projects\guess\Debug\guess.exe : fatal error LNK1169: one or more multiply defined symbols found
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:01.36
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
"1>guess_2.obj : error LNK2005: _main already defined in guess.obj"

thats the culprit!
I copied the code; trashed the original project file and started again with the copied code and BLAM! NO ERRORS ! :)

Thank you Google! :) And for all of you for the guidance
Topic archived. No new replies allowed.