'If' statement confusion

Hi everyone, new to the forums here - as well as coding in C++.
Trying to make the old favorite number guessing game just to make sure I'm not completely terrible at the basics.
The problem I'm having is that every time it gets to the point in the code I stated (via comment) the program ignores the else statement even if the conditions for the original if statement aren't met. I've looked things up online and gone over it multiple times but I can't for the life of me work out what I've missed - have I simply overlooked an error or is it somehow completely wrong? I'm not getting any errors in my IDE so it's making it even more difficult. Thanks for everyones help in advance. I'm sure it's a stupid mistake. :/

There's also another problem since if you enter a non int answer when prompted to guess the program goes nuts and starts spamming 'Guess is too low" on every single line infinitely until you end the program. Thanks again!

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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
 // ConsoleApplication2.cpp : Defines the entry point for the console application.


//Sources
#include "stdafx.h"
#include <iostream>
#include <cmath>
#include <time.h>

//Main function
int main()
{
	using namespace std;


	//Variables
	int randNum;
	int guess;
	char playAgn;
	

	//Start
	restart:

	//Random number gen
	srand(time(NULL));
	randNum = (rand() % (100 - 1)) + 1;


	cout << "Guess the number!" << endl;

	//Further guesses loop point
	tryAgain:

	cout << "What is your guess?" << endl;
	cin >> guess;

	
	//If guess is correct
	if (guess == randNum)
		{
		cout << "Correct! The answer is " << randNum << endl;
		cout << "Would you like to play again? Y/N" << endl;
		cin >> playAgn;

		/* - PROBLEM HERE -
		* The program will work up until this point given the guess is correct, after which regardless if
		* you type in Y, y, or some other random character it will always loop back to the start 
		* as if you answered 'y'.
		*/


			//If 'y'
			if (playAgn == 'Y' || 'y')
			{
				goto restart;
			}

			//If 'n'
			else
			{

				goto endProg;
			}

		}
	

	//If guess is greater
	else if (guess > randNum)
	{
		cout << "Your guess was too high. Try again!" << endl;
		goto tryAgain;
	}

	//If guess is less
	else if (guess < randNum)
	{
		cout << "Your guess was too low. Try again!" << endl;
		goto tryAgain;
	}
        //Second problem!
	//If guess is not valid - Does not work!

	else
	{
		goto tryAgain;
	}

	//Eng of program
	endProg:
	cout << "Goodbye!" << endl;
	return 0;

}
Last edited on
closed account (SECMoG1T)
This should solve your problem
1
2
3
4
if (playAgn == 'Y' ||playAgn== 'y')
 {
	goto restart;
 }


For these:- non int answer your will need to check the state of your stream, when you key in a non-int value an error state is set;

1
2
3
4
5
6
7
8
cout << "What is your guess?" << endl;
cin >> guess;

if(!cin)
{
  cin.clear();
  goto restart;/// just an example..
}


Also please avoid using goto's they are really terrible and make code hard to read , instead use loops.
Last edited on
Thanks man that's done it. And I wasn't aware of that, I'll try and stick to loops from now on if that's the case.
Thanks again!
Topic archived. No new replies allowed.