have no idea why this message is appearing. I only started learning C++ yesterday so need some help. i'm pretty sure the program is correct but it malfunctions
this is my code
//the number game
#include <iostream>
#include <sstream>
using namespace std;
int main ()
{
int n = 123; //answer
int c = 0; //counter
int g; //guess
int d; //higher lower indicator
string e;
string name; //player name
cout << "Welcome to the number game. Press enter to continue.";
getline ( cin, e );
cout << '\n' << "Please enter your name. : ";
getline ( cin, name );
cout << '\n' << "the aim of the game is to guess what number i'm thinking of.";
cout << '\n' << "the number is between 0 and 1000.";
cout << '\n' << "I will tell you if the number you guessed is higher or lower";
cout << '\n' << "than the actual number.";
cout << '\n' << "What is your first guess? : ";
cin >> g;
// if number is above 1000
while ( g > 1000 )
{
cout << '\n' << "please pick a number between 0 and 1000. : ";
cin >> g;
}
// if number is not correct
while ( g != n )
{
if ( g < n )
d = 1; //go higher
else if ( g > n )
d = 2; //go lower
// if lower
if ( d == 1 )
cout << '/n' << "the number i am thinking of is higher than ";
//if higher
else if ( d == 2 )
cout << '\n' << "the number i am thinking of is lower than ";
cout << g << ".";
//adding 1 to counter
c+=1;
//statistics
cout << '\n' << name << '\n' << "number of guesses. : ";
cout << '\n ' << "what is your next guess. : ";
cin >> g;
}
//ending when correct answer entered
cout << '\n' << "Well done " << name << " the number is " << n;
cout << '\n' << "It took you " << " guesses to find the answer.";
cout << '\n' << "Press enter to exit.";
getline ( cin, e );
return 0;
}
You have a space after your \n. You also have a '/n' somewhere which won't do what you want it to.
Another thing, you should start your counter at 1. It is only incremented when the user guesses wrongly, so it is always 1 lower than it should be.
You have a bit in the middle that is a bit pointless as well.
while ( g != n )
{
if ( g < n )
d = 1; //go higher
elseif ( g > n )
d = 2; //go lower
// if lower
if ( d == 1 )
cout << '/n' << "the number i am thinking of is higher than ";
//if higher
elseif ( d == 2 )
cout << '\n' << "the number i am thinking of is lower than ";
cout << g << ".";
//adding 1 to counter
c+=1;
//statistics
cout << '\n' << name << '\n' << "number of guesses. : ";
cout << '\n ' << "what is your next guess. : ";
cin >> g;
}
That can be rewritten as:
1 2 3 4 5 6 7 8 9 10 11
while ( g != n )
{
if ( g < n )
cout << '\n' << "the number i am thinking of is higher than ";
else
cout << '\n' << "the number i am thinking of is lower than ";
cout << g << ".";
c++;
cout << '\n' << "what is your next guess. : ";
cin >> g;
}
If g != n then it will either by higher or lower. If it's not lower, than there's no need to check if it's higher. You also don't need your 'd' variable. It's not doing anything useful. You might as well output when you're checking if it's higher or lower. c++ is equivalent to c += 1 but it just looks a bit cleaner.
You also don't actually output the number of guesses at the end.
All your '\n's can actually go at the start of the strings they precede. For example: cout << "\nPress enter to exit.";