i need to use the loop construct to write a program for the users to guess a secret character, which is k, (between lower case letter āaā and āzā) stored inside the program. The user is allowed to have a maximum of 3 guesses. and need to show up 3 different kind of result, the first one:The user do not get the correct character and he/she had used up the quota (i.e., 3 guesses).second one: The user finally got the secret character after 3 guesses. and the last one is :The user used less than 3 guesses and got the secret number.
please helping to know how to write this program, and understand the logic in this question. thank you
there is my code:
#include <iostream>
using namespace std;
int main ()
{
char lowerChar = 'a';
char higherChar = 'z';
char secretChar = 'g';
char inputChar;
int choice = 3;
int index = 0;
while (choice--)
{
cout << "Please enter a letter between " << lowerChar << " and " << higherChar << " : ";
cin >> inputChar;
if ( inputChar < secretChar )
{
cout << "Incorrect! Please enter the letter bigger than " << inputChar << ".\n";
}
else if ( inputChar > secretChar )
{
cout << "Incorrect! Please enter the letter smaller than " << inputChar << ".\n";
}
else if ( inputChar = secretChar)
{
cout << "Congratulation! The input character equals to the secret character." << endl;
}
}
return 0;
}
But we I run it, I can't get these 3 different scenarios at the end:
1) sorry, you have used up 3 times! program terminated.
2)you have used 3 times to guess the secret letter. program terminated.
3)you have used 2 times to guess the secrt letter.
char secret = /*...*/
char user_guess;
int i = 3;
while(i--) {
user_guess = /*...*/
if (user_guess == secret)
break;
}
After the loop you have 3 possibilities:
1) user_guess != secret: user did not guess character
2) user_guess == secret and i == 0: user got correct character on the last guess
3) user_guess == secret and i != 0: user gueesed the character and still has guesses to spare.