Question

In: Computer Science

Question:   Can you please convert this program into switch case program using c++. Output should be...

Question:   Can you please convert this program into switch case program using c++.

Output should be same

int main()

{

    bool weekend = false;

    unsigned char day = 0;

    bool isDay = true;

    cout << " Enter a char for a day of week:";

    cin >> day ;

    if (day == 'M' || day == 'm')

        cout << " This is Monday" << endl ;

    else if (day == 'T' || day == 't')

        cout << " This is Tuesday" << endl ;

    else if (day == 'W' || day == 'w')

        cout << " This is Wednesday" << endl ;

    else if (day == 'R' || day == 'r')

        cout << " This is Thursday" << endl ;

    else if (day == 'F' || day == 'f')

        cout << " This is Friday" << endl ;

    else if (day == 'S' || day == 's')

    {

        cout << " This is Saturday" << endl ;

        weekend = true ;

    }

    else if (day == 'U' || day == 'u')

    {

        cout << " This is Sunday " << endl ;

        weekend = true ;

    }

    else

    {

        cout << day << " is not recognized..." << endl ;

        isDay = false;

Solutions

Expert Solution

Required Code:

#include <bits/stdc++.h>

using namespace std;

int main()

{

bool weekend = false;

unsigned char day = 0;

bool isDay = true;

cout << " Enter a char for a day of week:";

cin >> day ;
  
switch(day) //pass day as parameter of switch
{
case 'M': case 'm':
//as we have to use two character so we can use like that one case for 'M' and one for 'm'

cout << " This is Monday" << endl ;
break;
//we need to use break statement after each statement otherwise all cases it will execute

case 'T': case 't':   //as we have to use two character so we can use like that one case for 'M' and one for 'm'

cout << " This is Tuesday" << endl ;
  

case 'W': case 'w': //as we have to use two character so we can use like that one case for 'M' and one for 'm'

cout << " This is Wednesday" << endl ;
break;  
//we need to use break statement after each statement otherwise all cases it will execute

case 'R': case 'r': //as we have to use two character so we can use like that one case for 'M' and one for 'm'

cout << " This is Thursday" << endl ;
break;
//we need to use break statement after each statement otherwise all cases it will execute

case 'F':case 'f': //as we have to use two character so we can use like that one case for 'M' and one for 'm'

cout << " This is Friday" << endl ;
break;   
//we need to use break statement after each statement otherwise all cases it will execute

case 'S':case 's':  //as we have to use two character so we can use like that one case for 'M' and one for 'm'

cout << " This is Saturday" << endl ;
weekend = true ;
break;   
//we need to use break statement after each statement otherwise all cases it will execute

case 'U':case 'u':  //as we have to use two character so we can use like that one case for 'M' and one for 'm'
cout << " This is Sunday " << endl ;
weekend = true ;
break;
//we need to use break statement after each statement otherwise all cases it will execute
  
default:

cout << day << " is not recognized..." << endl ;

isDay = false;
}
}


Related Solutions

C++ Write a program using switch-case-break that will take an input number as for example 2...
C++ Write a program using switch-case-break that will take an input number as for example 2 and based on switch it will provide various powers of 2.
Take the Java program Pretty.java and convert it to the equivalent C program. You can use...
Take the Java program Pretty.java and convert it to the equivalent C program. You can use the file in.txt as sample input for your program. v import java.io.*; import java.util.*; public class Pretty { public static final int LINE_SIZE = 50; public static void main(String[] parms) { String inputLine; int position = 1; Scanner fileIn = new Scanner(System.in); while (fileIn.hasNextLine()) { inputLine = fileIn.nextLine(); if (inputLine.equals("")) { if (position > 1) { System.out.println(); } System.out.println(); position = 1; } else...
Please if you are able to answer the question below: Write C++ program using native C++...
Please if you are able to answer the question below: Write C++ program using native C++ (you can use STL)  that produces Huffman code for a string of text entered by the user.  Must accept all ASCII characters.  Pleas explain how you got frequencies of characters, how you sorted them, how you got codes.
Question: Write a complete C++ program that runs multiple tests and calculations using switch statement. Based...
Question: Write a complete C++ program that runs multiple tests and calculations using switch statement. Based on your input and the selection from the keyboard, the program does the followings. If the selection is 1, the program should test the input if it’s positive, negative or zero. For example, the output should be “The selection is 1 to test the input value if it’s positive, negative or equal to zero. The input value 7 is positive” If the selection is...
C++ program using Eclipse IDE The C++ program should have concurrency. The C++ program should create...
C++ program using Eclipse IDE The C++ program should have concurrency. The C++ program should create 2 threads that will act as counters. One thread should count down from 5 to 0. Once that thread reaches 0, then a second thread should be used to count up to 20.
***Using Java Using the switch/case construct, write a program that takes a character from the user...
***Using Java Using the switch/case construct, write a program that takes a character from the user and classifies it as a number (‘1’,’2’, ‘3’, …), a letter from the alphabet (‘A’, ‘a’, ‘B’, ‘b’, …, ‘Z’, ‘z’), an arithmetic operator (‘+’, ‘-‘, ‘/’, ‘*’, ‘%’), a comparison operator (‘<’, ‘>’, ‘=’), punctuation (‘!’, ‘?’, ‘,’, ‘,’, ‘:’, ‘;’), and all other characters as a Special Character, and informs the user of the same. If the user entered the character A,...
Using c# , Write a program using a switch statement that takes one character value from...
Using c# , Write a program using a switch statement that takes one character value from the user and checks whether the entered value is an arithmetic operator (+, -, * , /) If not the program display a message that it not of the operators ( (+, -, * , /) .
C++ program. Please explain how the code resulted in the output. The code and output is...
C++ program. Please explain how the code resulted in the output. The code and output is listed below. Code: #include <iostream> #include <string> using namespace std; int f(int& a, int b) {    int tmp = a;    a = b;    if (tmp == 0) { cout << tmp << ' ' << a << ' ' << b << endl; }    b = tmp;    return b;    return a; } int main() {    int a...
Convert the following C program into the RISC-V assembly code. You should look up a table...
Convert the following C program into the RISC-V assembly code. You should look up a table to convert the lines manually and you can use "ecall" when appropriate (Don't make it too complicated than it needs to be): #include int main() { int x = 30, y = 17; printf("x * y = "); printf("%d\n", x*y); return 0; }
Can you solve this C program by using Function? Q1. Write a C program to ring...
Can you solve this C program by using Function? Q1. Write a C program to ring the computer bell at any number of times you specify. Use the system clock as a delay, you need to include the time header file.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT