Assistance with data validation within if else or switch statements

Hi,

This code is supposed to go through some calculations regarding fat content for various meal types. I have everything right, as far as I know ( I am at a very beginner level). The code compiles. But my problem is if I enter something other than B, D, or L. The code just quits. I would like to find a way to display a specific error message but do not know where to fit it within this without getting errors. This is for a homework assignment, I am just stuck on how to validate the B, D, or L entry. I appreciate any assistance. I'm fairly confident that there are other things that could be done better ways, but this is my main concern right now. Thank you for any help or pointing me in the right direction.


#include <iostream>
#include <iomanip>

using namespace std;

int main ()

{

int calories, fatgrams, MINcalorie=0, MINfatgram=0;
const double breakfastlow=10, lunchlow=20, dinnerlow=30;
double percentage, caloriesoffat;
char choice;


cout << "Please tell me what type of meal you had\n";
cout << "\n";

cout << " Meal Choices \n";
cout << "\n";

cout << " B for Breakfast\n";
cout << " L for lunch\n";
cout << " D for dinner\n";

cin >> choice;

cout << "How many calories: \n";
cin >> calories;
cout << "How many grams of fat: \n";
cin >> fatgrams;

caloriesoffat = fatgrams*9;
percentage = (caloriesoffat/calories)*100;

cout << fixed << showpoint << setprecision(1);

switch (choice)

if (calories >= MINcalorie && fatgrams >=MINfatgram)
{
case 'b':
case 'B': cout <<"Your food has " << percentage << "% calories from fat.\n";
{
if (percentage<breakfastlow)
cout << "It is a low calories food!\n";
}

break;

case 'l':
case 'L': cout << "Your food has " << percentage << "% calories from fat. \n";
{
if (percentage<lunchlow)
cout << "It is a low calories food!\n";
}

break;

case 'd':
case 'D': cout << "Your food has " << percentage << "% calories from fat. \n";
{
if (percentage<dinnerlow)
cout << "It is a low calories food!\n";
break;

}

}
return 0;


}
next time you post, please use the code tags in the Format pane in the right, and also, learn how to indent your code. however this is the indented version: ( i haven't change anything, just the alignment )

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
73
74
75
#include <iostream>
#include <iomanip>

using namespace std;

int main ()

{

    int calories, fatgrams, MINcalorie=0, MINfatgram=0;
    const double breakfastlow=10, lunchlow=20, dinnerlow=30;
    double percentage, caloriesoffat;
    char choice;


    cout << "Please tell me what type of meal you had\n";
    cout << "\n";

    cout << " Meal Choices \n";
    cout << "\n";

    cout << " B for Breakfast\n";
    cout << " L for lunch\n";
    cout << " D for dinner\n";

    cin >> choice;

    cout << "How many calories: \n";
    cin >> calories;
    cout << "How many grams of fat: \n";
    cin >> fatgrams;

    caloriesoffat = fatgrams*9;
    percentage = (caloriesoffat/calories)*100;

    cout << fixed << showpoint << setprecision(1);

    switch (choice)

        if (calories >= MINcalorie && fatgrams >=MINfatgram)
        {
            case 'b':
            case 'B': 
                cout <<"Your food has " << percentage << "% calories from fat.\n";
                {
                    if (percentage<breakfastlow)
                        cout << "It is a low calories food!\n";
                }
            break;

            case 'l':
            case 'L': 
                cout << "Your food has " << percentage << "% calories from fat. \n";
                {
                    if (percentage<lunchlow)
                        cout << "It is a low calories food!\n";
                }
            break;

            case 'd':
            case 'D':
                cout << "Your food has " << percentage << "% calories from fat. \n";
                {
                    if (percentage<dinnerlow)
                        cout << "It is a low calories food!\n";
                break;

                }

        }
    
    return 0;


}

I don't know if that is what you mean, but you've improperly used switch and if statements

EDIT:
Regarding your problem, you can:

1. Add a a loop with choice that keeps repeating until the user entered the correct choice:
1
2
3
4
5
6
cin >> choice;

while ( choice != 'b' || choice != 'l' || choice != 'd' ) {
    cout << "Invalid choice" << endl;
    cin >> choice;
}


2. If you want to exit right away but display first a message:
1
2
3
4
5
6
7
cin >> choice;

if ( choice != 'b' || choice != 'l' || etc.. ) {
    cout << "Invalid choice is made, Press any key to exit...";
    cin.get();
    return 0;
}
Last edited on
Thank you for the etiquette lesson your solution. Since I am new, all of this reads like a foreign language to me, so I forget how to make it look good for those that know what they are looking for.

I really appreciate you taking the time to help me with this. Is there a way that I need to declare the b, l, d? I have tried your second solution, but am getting a:

""*" is not declared in this scope"

error for each letter when I attempt to compile.

Thanks!
I'm sorry i'm wrong, i forgot to enclosed the letters in single quotes etc: 'b'

That is because the letters compared are interpreted as variable names when not enclosed in single quotes.
Last edited on
Nevermind, I figured it out. I just define it as a char . Thanks!
I guess I am doing something wrong still, it is now giving me the error message for any value i input, even if it is b, l, or d.
So, would my method of just defining as char variables at the beginning of my code work

-> Yes, but it is a waste of memory since you'll not going to change the value of it anywhere in your code thus it is just better to just put it in if and enclose it in single quotes,, or in bigger projects, it may be better to use #define or const char[]

maybe you have the error because you did not initialize b, l, or d to any value and compare it with choice
Last edited on
Topic archived. No new replies allowed.