Answer to Question #166626 in C++ for Earl Davis Jr.

Question #166626

This is a very short trivia game. See if the user can guess the year that MDC (formerly Dade County Junior College) first started offering classes (1960). If the user guesses incorrectly, ask if they want another attempt. Once the user gets it right, report the number of guesses needed and end the program. 

 

Initial Output:

MDC was originally named Dade County Junior College.

 

Output (per loop)

In what year were classes first offered?  [user types: 1980]

That answer is incorrect. Do you want to try again?  [user types: y]

In what year were classes first offered?  [user types: 1960]

Your answer is correct! It took you 2 tries to guess it.

 

Notes and Hints:

1) You can use whatever loop you feel is best

2) You must accept the upper and lower case versions of Y and N in the user's reply

3) The program must end when the user gets it right. Do not ask if they want to continue. 


1
Expert's answer
2021-02-26T19:54:35-0500
#include <iostream>

int main()
{
    std::cout << "MDC was originally named Dade County Junior College.\n";
    
    int counter = 1;
    
    while(true)
    {
        std::cout << "In what year were classes first offered? ";

        int year;
        std::cin >> year;
        
            if(!std::cin)
            {
                std::cout << "Bad input\n";
                return 1;
            }
        
        if(year == 1960)
        {
            break;
        }

        ++counter;
        
        std::cout << "That answer is incorrect. Do you want to try again? ";
        char continueOrNot;
        std::cin >> continueOrNot;
        
            if(!std::cin)
            {
                std::cout << "Bad input\n";
                return 1;
            }

        if(toupper(continueOrNot) == 'Y')
        {
            continue;
        }
        
        if(toupper(continueOrNot) == 'N')
        {
            return 0;
        }

        std::cout << "Unexpected input\n";
        return 1;      
    }

    std::cout << "Your answer is correct! It took you " << counter
              << " tries to guess it.\n";
    
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment