Write a program that asks the user for a number in the range of 1 through 7. The program should display the corresponding day of the week, where 1 = Monday, 2 = Tuesday, 3 = Wednesday, 4 = Thursday, 5 = Friday, 6 = Saturday, and 7 = Sunday. The program should display an error message if the user enters a number that is outside the range of 1 through 7.

Respuesta :

ijeggs

Answer:

#include <iostream>

using namespace std;

int main()

{

int day;

cout<<"Enter a number for the day of the week"<<endl;

cin>> day;

switch (day) {

case 1: cout << "Monday"<< endl; break;

case 2: cout << "Tuesday" << endl; break;

case 3: cout << "Wednesday" << endl; break;

case 4: cout << "Thursday" << endl; break;

case 5: cout << "Friday" << endl; break;

case 6: cout << "Saturday" << endl; break;

case 7: cout << "Sunday" << endl; break;

default: cout<<"Error, You entered an invalid day"<<endl;

}

   return 0;

}

Explanation:

The program is written using a switch statement in C++ programming language. we start off by creating a variable day of type integer, which is received using the cin statement. We then use switch statement to test the value using the conditions stated in the question. We use the default statement which acts like an else  statement to handle the an invalid situation

ACCESS MORE