Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: [C++] Help with program  (Read 3356 times)

0 Members and 1 Guest are viewing this topic.

mvb5142

  • Guest
[C++] Help with program
« on: August 02, 2009, 11:02:30 PM »
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 <iostream>

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;
      }
   }
    
}

Quantos



    Guru
  • Veni, Vidi, Vici
  • Thanked: 170
    • Yes
    • Yes
  • Computer: Specs
  • Experience: Guru
  • OS: Linux variant
Re: [C++] Help with program
« Reply #1 on: August 07, 2009, 06:51:59 PM »
Does this have anything to do with homework?

Are you getting paid to write this?
Evil is an exact science.

BC_Programmer


    Mastermind
  • Typing is no substitute for thinking.
  • Thanked: 1140
    • Yes
    • Yes
    • BC-Programming.com
  • Certifications: List
  • Computer: Specs
  • Experience: Beginner
  • OS: Windows 11
Re: [C++] Help with program
« Reply #2 on: August 07, 2009, 07:08:57 PM »
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?
I was trying to dereference Null Pointers before it was cool.