Creating a Basic Guessing Game Question?

Hey I need help I am supposed to create a simple guessing game using c++

This is the instructions:
Complete project 5, page 283, as modified below, using C++.

Use an outer loop to control the number of rounds to play
Generate a new random number at the beginning of each round
Use an inner loop to control multiple guesses per round
Give the user feedback for each guess: High, Low, Exact
Terminate the inner loop with a correct guess or the maximum number of guesses
Ask the user whether they want to play another round at the end of the outer loop
Turn in a single zip file containing either your Visual Studio project folder or your .cpp file. Name the zip file “First_Last_HW5”, where “First Last” is replaced with your First and Last names.

This is what I have so far but I am stuck:
#include "stdafx.h"
#include <iostream>
#include <ctime>
using namespace std;



int main()
{
int count= 0;
int answer;
int guess;
srand(time(0));
guess = rand() %101
do{
cout << "Enter a number between 1 and 10";
cin>> answer;
}
if (answer > guess)
{
cout <<"Your guess is too high" << endl;
} else if (answer < guess)
{
cout << "Your guess is too low" << endl;
} else{count++;
}while (answer!=guess)
if (answer == guess)
cout <<"You guessed it" <<endl;

system("pause");
return 0;
}
The compiler says my do statement error expected a ";"
Last edited on
The interesting thing about the compiler error is they not only tell you what the problem is, but also what line it's on. Although in this case, the error was on the line before the line it told you (which is common, if you don't see the error on the actual line, try checking a line or 2 above/below it)

guess = rand() %101

notice that you're missing a semicolon on that line, which is exactly what the compiler error was telling you.

;P

Learning to read and understand compiler errors is actually an important skill to get the hang of when programming.
Last edited on
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
int main()
{
int count= 0;
int answer; 
int guess;
srand(time(0));
guess = rand() %101;
do{
cout << "Enter a number between 1 and 10";
cin>> answer;
}
if (answer > guess)
{
cout <<"Your guess is too high" << endl;
} else if (answer < guess) 
{
cout << "Your guess is too low" << endl;
} else{count++;
}while (answer!=guess);
if (answer == guess) 
cout <<"You guessed it" <<endl; 

system("pause");
return 0;
}


you forgot 2 semicolons on line 7 & 19, hopefully this helps. next time it would be helpful to tell us which line the error was on.
Last edited on
Alright thank you guys for all of your help I will try that.
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
#include "stdafx.h"
#include <iostream>
#include <ctime>
using namespace std;  



int main()
{
	int count= 0;
	int answer; 
	int guess;
	srand(time(0));
	guess = rand() %101;
	do{
		cout << "Enter a number between 1 and 10";
		cin>> answer;
	}
		while (answer > guess);
		{
			cout <<"Your guess is too high" << endl;
		}   else if (answer < guess); error here Intellisense: expected a statement
		{
			cout << "Your guess is too low" << endl;
		}   else if{count++);error here Intellisense: expected a statement
		}while (answer!=guess);
		if (answer == guess) 
		cout <<"You guessed it" <<endl; 
		
		system("pause");
	    return 0;



ok now this is my problem I have underlined and bolded where the errors are.
your errors are because you have a semicolon after the statment when it isnt needed and also in the last else if count++ isnt a condition you can check. you need to do
else{
count++;
}
Last edited on
read the second post in the beginner forum, it will tell you about that system pause you are using...
Hey I just re did my code in general and it worked better for me here it is

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
// Klaeton_Beyers_HW5.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <ctime>
using namespace std;  



int main()
	{
		
		int answer; 
		int guess;
		srand(time(0));
		guess = rand() %101;

		cout << "Enter a number between 1 and 10 ";
		cin>> guess;
		if (guess > answer);
		
			cout <<"Your guess is too high" << endl;
		
		 cout<< "Guess again" <<endl; 
		cin>> guess;
		if (guess <answer);

			cout <<"Your guess is too low" << endl; 
			cout <<"Guess again" << endl;
		cin>> guess;
		 if (guess = answer)
			cout<<"CONGRADULATIONS!!!! You have guessed my number"<<
	
		system("pause");
	    return 0;
        
	}

just wondering if I did anything wrong because I am confused about the random number and how to generate it.
Topic archived. No new replies allowed.