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!
// 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()
{
usingnamespace 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
elseif (guess > randNum)
{
cout << "Your guess was too high. Try again!" << endl;
goto tryAgain;
}
//If guess is less
elseif (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;
}