Home > Software engineering >  i need a way to make this code work regarding switch
i need a way to make this code work regarding switch

Time:04-01

i cannot make the cout appear within my switch, i hope someone here could help me

this is the code given to me, aslo please if possible the simplest way of work around, thanks

#include <iostream>

using namespace std;

int main() {
    double fh, cs;

    cout << "Enter Fahrenheit: ";
    cin >> fh;

    cs = (fh - 32) * 5 / 9;
    cout << "Temperature in Celsius: " << cs;

    switch (cs < 40)
    {
    case 1:
        switch (cs <= 0) {
        case 1:
            cout << "\nFreezing weather";
            break;
        }
        break;
    case 2:
        switch ((cs >= 1) < 10) {
        case 1:
            cout << "\nVery Cold weather";
            break;
        }
    case 3:
        switch ((cs >= 11) < 20) {
        case 1:
            cout << "\nCold weather";
            break;
        }
    case 4:
        switch ((cs >= 21) < 30) {
        case 1:
            cout << "\nNormal in Temp";
            break;
        }
    case 5:
        switch ((cs >= 31) < 40) {
        case 1:
            cout << "\nIt's Hot";
            break;
        }


    case 0:
        cout << "\nIt's Very Hot";
        break;
    }

    return 0;
}

CodePudding user response:

the outter switch cs < 40 you can only get 0 if cs >= 40 or 1 if cs < 40, you can not get other cases like 2,3,4,5. Why not just use if statement.

CodePudding user response:

You are using switch the wrong way. Each of the expression written under switch will only evaluate to 0 or 1. So other cases are not reachable. What you are trying to do can be better achieved with if/else.

  • Related