I am a beginner and I am make a number guessing game I am stuck on the numbers from 1-12. Can you help me??
#include <iostream>
using namespace std;
int main()
{
char ans1;
char ans2;
char ans3;
char ans4;
char ans5;
char ans6;
char ans7;
char ans8;
char ans9;
cout <<"is your number greater than 50??"<<endl;
cin >>ans1;
if (ans1=='n'){cout <<"is your number greater than 25??"<<endl;
cin >>ans2;
if (ans2=='n'){cout <<"is your number greater that 12??"<<endl;
cin >>ans3;
if (ans3=='n' ){cout <<"is your number greater than 6??" <<endl;
cin >>ans4;
if (ans4=='y'){cout <<"is your number even??"<<endl;
cin >>ans5;
if (ans5=='n'){cout<< "is your number greater than 9??"<<endl;}
cin >>ans9;
if (ans9=='y'){cout <<"is your number 11??"<<endl;}
else if (ans9=='n'){cout <<"is your number 7??"<<endl;}}}
else if (ans5=='y'){cout<< "is your number greater than 10??"<<endl;
cin >>ans6;
if (ans6=='y'){cout <<"is your number 12??"<<endl;}
else if (ans6=='n'){cout <<"is your number 8??"<<endl;
cin >>ans7;
if (ans7=='n'){cout<<"your number is 10"<<endl;
if (ans4=='n'){cout<< "is your number greater than 3??"<<endl;
cin >>ans8;
if (ans8=='y'){cout <<"is your number 5??"<<endl;}
else if (ans8=='n'){cout <<"is your number 1??"<<endl;}}
cin >>ans7;
if (ans7=='n'){cout <<"your number is 3"<<endl;}
else if (ans5=='y'){cout<< "is your number greater than 4??"<<endl;
cin >>ans6;
if (ans6=='y'){cout <<"is your number 6??"<<endl;}
else if (ans6=='n'){cout <<"is your number 2??"<<endl;
cin >>ans7;
if (ans7=='n'){cout<<"your number is 4"<<endl;}}}}}}}}
A simple loop would be better suited for a guessing game to eliminate all those IF's, not exactly the same as your objective but this will give you some ideas :)
#include <iostream>
#include <time.h> // for time(0) in seed
usingnamespace std;
int main()
{
bool guessing = true;
int randomNum;
int guess;
srand(time(0));
// let computer think of a number.
randomNum = rand() % 12 + 1; // between 1 and 12
cout << "I have thought of a number between 1 and 12" << endl;
cout << "Can you guess what it is?" << endl << endl;
do
{
cout << "Enter your guess: ";
cin >> guess;
// simple checks on guess
if (guess == randomNum) {
cout << "Well done!, you guessed my number!" << endl;
guessing = false; // end game.
}
elseif (guess > randomNum) // are we higher?
cout << "Your too high, go lower!" << endl;
elseif (guess < randomNum) // are we lower
cout << "Your too low, go higher!" << endl;
} while (guessing);
return 0;
}
but the hard part is that you can only ask 7 questions and only say yes or no so the first question would be is your number greater than 50 if it is not then the second would be is your number greater than 25 then it would be 12 then 6 and then is your number odd and is yes then is your number greater than 3 if it is not it would be 1 if it is ask if 5 if the answer is no then it would be 3. The range is from 1-100