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.
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;
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 )
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:
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 constchar[]
maybe you have the error because you did not initialize b, l, or d to any value and compare it with choice