Ask Question

You are writing a number guessing program where the player must try to guess the secret number 14. This function will take a player's guess and tell them if they are either right or whether they should guess higher or lower on their next turn Write a function named "higher_lower" that takes an int as a parameter and returns "higher" if 14 is greater than the input and "lower" if 14 is less than the input, and "correct" if 14 is equal to the input.

+4
Answers (1)
  1. 23 May, 22:48
    0
    I am writing the code in C++. Let me know if your want the program in some other programming language.

    Function is given below:

    void higher_lower (int guess)

    {

    if (guess > 14)

    {

    cout<<"higher";

    }

    else if (guess < 14)

    {

    cout<<"lower";

    }

    else

    cout<<"correct";

    }

    Explanation:

    The complete code is given below:

    #include

    using namespace std;

    void higher_lower (int guess) {

    if (guess > 14) {

    cout<<"higher"; }

    else if (guess < 14) {

    cout<<"lower"; }

    else

    cout<<"correct"; }

    int main ()

    { int guessNo;

    cout<<"Enter a Number";

    cin>>guessNo;

    higher_lower (guessNo); }

    The function higher_lower takes an integer type variable guess as parameter. It has an IF statement that checks if that number is greater than the secret number 14, if this condition evaluates to true higher will be displayed in the output. If this condition evaluates to false then the program control moves to the else if part which checks if the number guessed by the user is less than the secret number 14 and if it evaluates to true lower is displayed on the output screen but if it evaluates to false then the final else part is executed which displays correct on the output.

    In the main () function the program prompts the player to enter a number as a guess and then calls the higher_lower function to tell the player if he is right or whether he should guess higher or lower on their next turn.

    If the program wants to keep the player guessing the number until he guesses the secret number then the following program can be used:

    #include

    using namespace std;

    void higher_lower (int guess) {

    while (guess!=14) {

    cout<<"Enter a Number"<
    cin>>guess;

    { if (guess > 14) {

    cout<<"higher"<
    else if (guess < 14)

    { cout<<"lower"<
    else

    cout<<"correct"<
    int main ()

    { int guessNo;

    higher_lower (guessNo); }

    Here the while loop will keep asking the player to guess the number until he guesses the correct secret number i. e. 14. The loop breaks and the program ends when the player guesses the correct number 14 or else the program will keep telling the player if he is either right or whether he should guess higher or lower on their next turn.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “You are writing a number guessing program where the player must try to guess the secret number 14. This function will take a player's guess ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers