Hello Everyone
I have encountered a small problem with my attempt of creating our little numbers game.
The If statement doesnt work correctly. The counter is not updated if the correct guess is made and the cout that confirms the solution is not printed. The loop is ended though.
Ive tried moving the correct guess to the else statement but the problem still occurs.
What would be the correct if statement with the loop condition !=
Thanks for your Help!
#include <iostream>
using namespace std;
//Rules
//1.) Player 1 selects a random Number
//2.) Player 2 needs to guess the Number
//3.) The game will tell Player 2 whether the guess was too low or too high
//4.) The game will count the number of attempts Player 2 made
int main()
{
int p1Target;
int p2Guess;
cout << "Welcome to the game" << endl;
cout << "P1, Type a number between 1 - 50!" <<endl;
cin >> p1Target; //Number to be guessed
cout << "P2, Guess the number P1 thought of '\n' Tipp: It's between 1 & 50"<<endl;
cin >>p2Guess;
int numberOfAttempts = 0;
while (p2Guess != p1Target ){
if (p2Guess == p1Target){
cout<< "Correct. How did you know?" <<endl;
numberOfAttempts++;
}
else if (p2Guess < p1Target){
cout<< "That is too low"<<endl;5
cout<< "Try again!"<<endl;
cin>>p2Guess;
numberOfAttempts++;
}
else {
cout<<"That is too high" <<endl;
cout<< "Try again!"<<endl;
cin>>p2Guess;
numberOfAttempts++;
}
}
cout<< "Number of Attempts " <<numberOfAttempts <<endl;
return 0;
}