Using upper and lowercase cases in a switch

So I have my program below, and everything is running smoothly on the program. The only problem is, I need to make it so that for each case within the switch, the lowercase or uppercase letter will make it work. Any help? Thank you!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <iostream>
#include <cstdlib>
#include <iomanip>

using namespace std;

double percent_calc(int, int);

int main()
{
    char meal;
    int fat, calories;
    double percentage;
    
    cout << "What you have to eat:\nB - breakfast\nL - lunch\nD - dinner\n\n";
    cout << "What you have: ";
    cin >> meal;
    cout << "Enter the number of Calories in the meal: ";
    cin >> calories;
    cout << "Enter the grams of Fat in the meal: ";
    cin >> fat;
    percentage = percent_calc(calories, fat);

    switch (meal)
    {
        case 'B': cout << fixed << showpoint << setprecision(1);
                  if (percentage >=10)
                      cout << "Your food has " << percentage << "% calories from fat." << endl;
                  else {
                      cout << "Your food has " << percentage << "% calories from fat." << endl;
                      cout << "It is a low fat food." << endl;
                  }
                  break;

        case 'L': cout << fixed << showpoint << setprecision(1);
                  if (percentage >= 20)
                      cout << "Your food has " << percentage << "% calories from fat." << endl;
                  else {
                      cout << "Your food has " << percentage << "% calories from fat." << endl;
                      cout << "It is a low fat food." << endl;
                  }
                  break;
                         

        case 'D': cout << fixed << showpoint << setprecision(1);
                  if (percentage >= 30)
                      cout << "Your food has " << percentage << "% calories from fat." << endl;
                  else {
                      cout << "Your food has " << percentage << "% calories from fat." << endl;
                      cout << "It is a low fat food." << endl;
                  }
                  break;
    
        default: cout << "Error: " << meal << " is not a valid menu choice." << endl;
                 exit(0); 
    }

    return 0;
}

double percent_calc(int calories_calc, int fat_calc)
{
    int fat_Calories = fat_calc * 9;

    if (calories_calc == 0 || fat_calc == 0 || fat_Calories > calories_calc) {
        cout << "The number of calories or grams of fat imputted were incorrect.";
        exit(0);
    }

    else 
        return static_cast<double>(fat_Calories) / calories_calc * 100;
}
Yep, fall through.

1
2
3
 case 'B': 
 case 'b': cout << fixed << showpoint << setprecision(1);
          // etc etc 


Alternatively, you can convert the meal variable to uppercase before the switch.
Last edited on
Oh that's great, thank you so much.

I completely forgot about fall through as I didn't want it between the meals.
Another technique to keep in mind is the use of std::toupper or std::tolower to convert the case of a char variable before you test it, thereby avoiding testing twice.

The current solution is perfectly fine - this is just another idea to keep in mind

http://www.cplusplus.com/reference/std/locale/toupper/


Hope all goes well.
Topic archived. No new replies allowed.