1.

Solve : [C++] Help with program?

Answer»

I'm trying to write a program that asks the user to enter one
of four different types of foods (Salty = s, Sweet = w, Protein = p & Carbohydrate = c).
Depending on the letter entered, I would like for my program to respond with a
sentence about that food type. If the user input is something other that the four letters,
my program should print "I'm sorry, I do not recognize your selection."
I also would like to allow the user input to be either in upper or lower case.

Here is what I have so far.
The program builds but it prints out these RESPONSE for the letter entered as well as the "I'm sorry" message.
PLEASE HELP

*/

#include

using namespace std;

INT main()
{
   
   char answer = 's' || 'w' || 'p' || 'c' || 'S' || 'W' || 'P' || 'C';
   

   cout << "What type of food is it? (Enter a letter): ";
   cout   << "alty S[W]eet [P]rotein [C]arbohydrate " <<endl;
   cin >> answer;
   
   do
   {
      if (answer != 's' || 'w' || 'p' || 'c' || 'S' || 'W' || 'P' || 'C');
      {
         cout << "I'm sorry, I do not recognize your selection. Please try again." <<endl; break;   
       }
   }

   while (true);
   {
      answer == 's' || 'S';
      {
         cout << "Be careful! Too much SALT can raise your blood pressure." <<endl;
      }
    
      answer == 'w' || 'W';
      {
         cout << "Too many SWEETS can increase your body fat and your blood sugar levels." <<endl;
      }
      
      answer == 'p' || 'P';
      {
         cout << "Protein is an important and neccessary macro nutrient." <<endl;
      }
      
      answer == 'c' || 'C';
      {
         cout << "Carbohydrate is important, but most people eat too much of it." <<endl;
      }
   }
    
}Does this have anything to do with homework?

Are you getting paid to write this?what you need to do, is analyze your requirements.

if you want it to only ask once, then a do loop is not necessary around the entire code.

Additionally I might question your usage of this:


Code: [Select]   char answer = 's' || 'w' || 'p' || 'c' || 'S' || 'W' || 'P' || 'C';
it doesn't do anything... and whatever it does assign will be meaningless.

Also I might point out the logic error in your If:

Code: [Select] if (answer != 's' || 'w' || 'p' || 'c' || 'S' || 'W' || 'P' || 'C');
what your doing here is saying, of answer is not 's' or if w, or if p... etc-
that is, you need to repeat the answer != portion in each one. One way I like to think of it is that each || basically separates entire CONDITIONS, that would have to stand on their own- for example if (answer!='s') makes sense, but what about if ('w')?


Code: [Select] if (answer != 's' || answer !='w' || answer !='p' || answer !='c' || answer !='S' || answer !='W' || answer !='P' || answer !='C');





also what purpose does the second while loop serve?



Discussion

No Comment Found