C++ bootcamp · Lab 42

Day of the week

easySwitch10 minLesson: Switch

Read the question, write C++ on the right, then Run or Check.

QuestionHint and solution stay closed until you open them

Read an integer 1–7 and print the weekday: 1 Monday, 2 Tuesday, …, 7 Sunday.

Any other number prints Invalid. Use a switch.

Input. One integer.

Output. Monday … Sunday, or Invalid.

Examples

Example 1
Input
1
Output
Monday
Example 2
Input
7
Output
Sunday
Example 3
Input
0
Output
Invalid
Hint
  1. switch (d) { case 1: … break; default: std::cout << "Invalid\n"; }
Show correct code

Peek only after you have tried. You can still Check your own version.

#include <iostream>

int main() {
  int d;
  std::cin >> d;
  switch (d) {
    case 1: std::cout << "Monday\n"; break;
    case 2: std::cout << "Tuesday\n"; break;
    case 3: std::cout << "Wednesday\n"; break;
    case 4: std::cout << "Thursday\n"; break;
    case 5: std::cout << "Friday\n"; break;
    case 6: std::cout << "Saturday\n"; break;
    case 7: std::cout << "Sunday\n"; break;
    default: std::cout << "Invalid\n"; break;
  }
  return 0;
}
main.cppC++17 · g++ · Ctrl + Enter runs
ResultIdle
Run to see output. Check grades the tests.