Everything seems to work but the calculations do seem to be doing their job in the else if statements. Am I doing this wrong? I think I have the right idea, but I just don't know how to write it in the script... Oh and I also need the program to run with 10 guesses, then after those 10 guesses the program must shut down or try again.
#include <iostream>
using namespace std;
int main()
{
int secretnumber = 77;
int num, num2, guessattempts=1-10;
//Greeting to the user
cout <<"\t\t\tWelcome to the guessing game!\n";
cout <<"\t\t\t_____________________________\n";
//Directions for the user
cout <<"Directions: ";
cout <<"You will have a total of 10 guesses to figure\n";
cout <<"\t what is the secret number.\n";
cout <<"\t The secret number lies between 1 to 100.\n\n";
//User input
cout <<"\t Please enter your chosen number.\n\t ";
cin >> num;
if ((num <=0 || num > 100))
{
cout <<"\t Please enter a number from 1 to 100.\n\n";
}
else if ( (num <1 && num >77) )
{
num2 = num - secretnumber;
cout <<"\t You are " << num2 <<"away from the secret number.\n";
cout <<"\t Try again.\n";
cout <<"\t You have " <<guessattempts<<" guess attempts remaining.\n";
}
else if ((num <77 && num >100))
{
num2 = num + secretnumber;
cout <<"\t You are " << num2 <<" away from the secret number.\n";
cout <<"\t Try again.\n\n";
cout <<"\t You have " <<guessattempts<<" guess attempts remaining.\n";
}
else if (num == 77)
{
cout <<"\t Woah! You guessed the right number! Congrats!\n\n";
You can't have guessattempts = 1-10; To get 10 attempts you want to do this:
1 2 3 4 5 6 7
int guessattempts = 0;
while(guessattempts < 10) //This will give us 10 attempts.
{
//Execute the guessing code here.
++guessattempts; //We just had another guess, so increment the number of guesses.
}