Assistance with a "negative guess"??

So, I am working on a Random Number Guessing Game project, and i just realize I didn't need to capitalize all of that, but I am already past it. Moving on.

I have the program working almost as intended but I need some help with a part of it. The project states the following, which I do not understand how to go about doing it.

"The program should use a loop that repeats until the user correctly guesses the random number.
A negative guess should end the loop to mean the user has given up."

Now, I have the first part, but I included it for context. What I don't get is "A negative guess should end the loop to mean the user has given up."

Would this mean a guess outside of the 1-100 range is supposed to close the program out? If so, how would I incorporate that into my program?

I have included the program for assistance in making this work.

Also, I can only use the first 5 chapters of my Intro to C++ book, so advanced terms and such I can not use. I am not even sure I can use the boolean expressions here, just in case, how can I rewrite those booleans to work as intended as well?

Either, or both answers would be appreicated very much. Thank you ahead of time.

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
56
57
58
59
60
61
62
63
64
  #include <iostream> // library that contains basic input output functions
#include <cstdlib> // library that contains random numbers generation functions
#include <ctime> // library that contains time functions.
#include <string> // library to use strings

using namespace std;

int main()
{
	bool playAgain = true;
	while (playAgain == true) 
	{
		
		
		int randomNumber = rand() % 100 + 1; 
		bool correctGuess = false;
		int guess; 
		int attempts = 1; 

		srand(time(0));
		
		while (correctGuess == false)
		{
			
			if (attempts == 1) 
			{
				cout << "Enter Number : ";
			}
			else
			{
				cout << "Enter Number Again : "; 
			}
			cin >> guess; 

			if (guess == randomNumber)
			{
				cout << "Congratulation! You have guessed the correct number in " << attempts << " attempts" << endl;
				correctGuess = true; 
			}
			else
			{
				attempts++; 
				if (guess < randomNumber) //if user entered number is less than random number 
				{
					cout << "Too low, try again!" << endl;
				}
				else  //if user entered number is greater than random number
				{
					cout << "Too high, try again!" << endl;
				}
				if (guess )
			}
		}
		//Asking user if he/she want to continue game or not
		string choice;
		cout << "Press Y to play again or any other key to terminate : ";
		cin >> choice;
		//if user enters any other key than Y/y, set playAgain flag to false
		if (choice != "Y" && choice != "y")
		{
			playAgain = false;
		}
	}
	return 0; //Successful teermination of program returns zero 
If guess is less than 0, break out of the loop. Use an "else if" statement after you have check to see if the user guessed correctly.
so would that look something like the following?

write to line 51 (which is where i started it and should be currently ignored

1
2
3
4
5
if else (guess < 0 or >100)
{
      cout << "Thanks for playing, I'm sorry you didn't finish!" << endl;
      return 0;
}


It also occurs to me I dont know how to put "or" in there, what is the symbol for that?

Thanks again
When using the comparison operators you need to explicitly state what you are comparing. So,
if(guess < 0 || guess > 0)

Also, logical or is denoted by ||, not or.
Also, your project states "A negative guess should end the loop to mean the user has given up.", so you should only be check to see if a value is less than 0 i.e. negative. Your position of this snippet of code is also a bit off. Think about the flow of your program. It's stipulated that if the user enters a negative number, they have given up. The game should end immediately then. Where you're currently placing it now, it still checks the negative number, which is not a valid guess, against the correct number. The game needs to end before then.
Topic archived. No new replies allowed.