Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program in C++ that prompts the user for a number within the range of 1

ID: 3639486 • Letter: W

Question

Write a program in C++ that prompts the user for a number within the range of 1 through 5 then have the program print out the Roman numeral equivalent of that number listed in the table below. The program should respond to invalid input, (numbers not in the range 1-5) by informing the user of the mistake and looping back to ask for a valid number. Once a valid number has been entered and a Roman numeral output has been printed, ask the user if they wish to enter another number. See screen capture below for example
1 = I, 2= II, 3 = III, 4 = IV , 5 =V

Explanation / Answer

#include <iostream>

using namespace std;

int main()
{
   double num = 0;
   char response = 'y';

   while (toupper(response) == 'Y')
   {
      //get number 1-5
      cout << "Enter a number (1-5): ";
      cin >> num;
      while (num < 1 || num > 5)
      {
         cout << "Invalid number "
              << "Enter a number (1-5): ";
         cin >> num;
      }
      //print the appropriate roman number
      cout << num << " = ";
      if (num == 1)
         cout << "I";
      else if (num == 2)
         cout << "II";
      else if (num == 3)
         cout << "III";
      else if (num == 4)
         cout << "IV";
      else
         cout << "V";
      cout << endl;
      //ask for another turn
      cout << "Do you want to enter another number? (y/n) ";
      cin >> response;
      cout << endl;
   }

   cout << "End - Normal termination. ";
   cin.ignore();
   cin.get();
   return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote