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
b. read the guess
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <stdlib.h>
#include <time.h>
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" <<endl;
	exit (0);

	else  Guess > 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

1
2
3
	if (Guess == Answer)
	cout << "Correct! You Win!!"<< endl;
	exit (0);


you have to write

1
2
3
4
5
	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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <stdlib.h>
#include <time.h>
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" <<endl;
		exit (0);
	}

	else if (Guess > 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;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <stdlib.h>
#include <time.h>
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" <<endl;
		exit (0);

		if (Guess > 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
1
2
3
4
5
6
7
8
9
10
else if (Guess <= 0)
	// Need braces here
		cout << "Pick a number larger than Zero; Terminating program" <<endl;
		exit (0);
                // Close braces here
		if (Guess > 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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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<answer)
{
    //bla,bla,bla,bla...
}


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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include <stdlib.h>
#include <time.h>
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" <<endl;
	exit (0);
	}

	else if (Guess > 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.