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 when 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 secret letter.
which kind of things should i add in this code in order to get these 3 different scenarios.
thanks
elseif ( inputChar = secretChar) // Note: = assign -> == compare
{
cout << "Congratulation! The input character equals to the secret character." << endl;
}
is wrong. Change it to:
1 2 3 4 5
else // Note: if is unnecessary. The case is already determined by the previous ifs
{
cout << "Congratulation! The input character equals to the secret character." << endl;
break; // Note: this ends the loop
}
But we I run it, I can't get these 3 different scenarios at the end:
Use choice (actually: 3 - choice) to determine the number of guesses. choice == -1 -> "sorry, you have used up 3 times!"
this is my new code, but it does not work well. please help me fix it, or give me some hits. thanks
#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;
break;
}
while (choice >= 0)
{
if (choice == 0)
{
cout << "\nSorry, you used up your quota ( 3 times). " << endl;
cout << "Program terminated." << endl;
}
if (choice > 0 && choice != 0)
{
cout << "\nYou used " << 3 - choice << " time(s) to guess the secret char." << endl;
cout << "program terminated." << endl;
break;
}