CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 20
  1. #1
    Join Date
    Mar 2009
    Posts
    16

    I can't figure this out, can someone help me 'char' issues

    Here is the problem I am trying to create the program for:

    Create a program that displays the number of daily calories needed to maintain your current body weight.

    Female:
    Active - (12*body weight)
    Inactive - (10*body weight)

    Male:
    Active - (15*body weight)
    Inactive - (13*body weight)

    With my code it doesn't matter what letter I put in for gender it will go right past it and ask for activity level then weight and calculate

    (Active Female*weight)
    (Inactive Female*weight)
    (Active Male*weight)
    (Inactive male*weight)

    Then Stop...

    I think if I can get it to only accept Male/Female or Active/Inactive I'll be fine but I don't know how to do that... Would it be easier to use a switch? How would I go about doing it? sorry I'm totally new

    Please Help!!!








    Code:
    #include <iostream>
    
    using std :: cout;
    using std :: cin;
    using std :: endl;
    
    int main ()
    {
        //Variables
        double calories = 0.0;
        double weight = 0.0;
        char gender = ' ';
        char level = ' ';
        
        
        //enter gender
        cout << "Enter Gender To Calculate Daily Calories Needed To Maintain Weight: ";
        cin >> gender;
        gender = toupper (gender);
    
        
        //calculate
        if (gender == 'F')
        {
                   cout << "Enter Your Activity Level (A/I): ";
                   cin >> level;
                   level = toupper(level);
                   
                   
                   
                   if      (level == 'A'){
                           cout << "Enter Current Weight: ";
                           cin >> weight;
                           calories = (weight*12);
                           cout << "You Need This Many Calories Daily To Maintain Your Weight: " << calories <<endl;
                           }
                   
                   
                   else   { (level == 'I');
                           cout << "Enter Current Weight:  ";
                           cin >> weight;
                           calories = (weight*10);
                           cout << "You Need This Many Calories Daily To Maintain Your Weight: " << calories <<endl;
                           }
    }
        
        
    
        else (gender == 'M');           
       
                   cout << "Enter Your Activity Level (A/I): ";
                   cin >> level;
                   level = toupper(level);
                   
                   
                   if       (level == 'A'){
                            cout << "Enter Current Weight: ";
                            cin >> weight;
                            calories = (weight*15);
                            cout << "You Need This Many Calories Daily To Maintain Your Weight: " << calories <<endl;
                            }
                   
                   
                   else     {(level == 'I');
                            cout << "Enter Current Weight:  ";
                            cin >> weight;
                            calories = (weight*13);
                            cout << "You Need This Many Calories Daily To Maintain Your Weight:  " << calories <<endl;
                            }
         
    
                   
    // display output
                   
    system ("pause");
    return 0;
    
    } //end main function

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: I can't figure this out, can someone help me 'char' issues

    Worked perfectly for me if I enter M or F. Are you asking how to handle invalid input?

  3. #3
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: I can't figure this out, can someone help me 'char' issues

    Your else statement is ill-formatted.

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: I can't figure this out, can someone help me 'char' issues

    Quote Originally Posted by Lindley View Post
    Your else statement is ill-formatted.
    Good eye.

  5. #5
    Join Date
    Mar 2009
    Posts
    16

    Re: I can't figure this out, can someone help me 'char' issues

    Worked perfectly for me if I enter M or F. Are you asking how to handle invalid input?
    Well it probably multiplied your first entry by 12 which isn't the correct output unless you specifically put in Female Active...

    How would I get it to display an invalid input?


    Your else statement is ill-formatted.
    What do you mean? Sorry I'm new

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: I can't figure this out, can someone help me 'char' issues

    I entered M and A and followed it in the debugger. It chose the right code paths.

    He means this statement is missing the if part

    else (gender == 'M');

  7. #7
    Join Date
    May 2007
    Location
    Scotland
    Posts
    1,164

    Re: I can't figure this out, can someone help me 'char' issues

    Code:
    else if (gender=='M');
    {
      ...
    
    }
    Add the blue remove the red.
    Last edited by PredicateNormative; March 10th, 2009 at 09:14 AM.

  8. #8
    Join Date
    Mar 2009
    Posts
    16

    Re: I can't figure this out, can someone help me 'char' issues

    else if (gender=='M');
    {
    ...

    }
    Add the blue remove the red.
    That totally worked... Thanks... One last question... How do I get it to pop out an error code if the wrong input is used?

    I really do appreciate all your help, if/else statements kill me, for some reason I can never get them to work... Any tips for next time?

  9. #9
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: I can't figure this out, can someone help me 'char' issues

    Quote Originally Posted by austinguy27
    How do I get it to pop out an error code if the wrong input is used?
    An else branch would come in handy, i.e., if the gender is 'F', do something, else if the gender is 'M', do something else, else print error message.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  10. #10
    Join Date
    Mar 2009
    Posts
    16

    Re: I can't figure this out, can someone help me 'char' issues

    okay I thought my last question was the last one I had but if found something else.... If I enter anything in the "if (gender == 'F')" area it doesn't say "press any key to continue..." when it's done compiling... it does it for the males though

  11. #11
    Join Date
    Mar 2009
    Posts
    16

    Re: I can't figure this out, can someone help me 'char' issues

    so your saying something like this

    else if (I don't know what to put here)
    cout<< "input error" << endl;

  12. #12
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: I can't figure this out, can someone help me 'char' issues

    Quote Originally Posted by austinguy27 View Post
    okay I thought my last question was the last one I had but if found something else.... If I enter anything in the "if (gender == 'F')" area it doesn't say "press any key to continue..." when it's done compiling... it does it for the males though
    You probably put the system("pause") command inside the code block for your if(gender == 'M') statement.

  13. #13
    Join Date
    Mar 2009
    Posts
    16

    Re: I can't figure this out, can someone help me 'char' issues

    how about

    at the top of my body I put this

    if (gender != 'M' && gender != 'F')

    else

    cout<< "input error" << endl;

  14. #14
    Join Date
    Mar 2009
    Posts
    16

    Re: I can't figure this out, can someone help me 'char' issues

    no worries on the
    "if (gender != 'M' && gender != 'F')"
    part I got it to work

    I got that to work...

  15. #15
    Join Date
    May 2007
    Location
    Scotland
    Posts
    1,164

    Re: I can't figure this out, can someone help me 'char' issues

    laserlights suggestion of
    Code:
    if (gender == 'F')
    {
      ...
    }
    else if (gender == 'M')
    {
      ...
    }
    else
    {
      std::cerr << "Input error" << std::endl;
    }
    is, in my mind, a better idea than:
    Code:
    if (gender!='F' && gender!='M')
    {
      std::cerr << "Input error" << std::endl;
    }
    else
    {
      if(gender=='F')
      {
        ...
      }
      else
      {
        ...
      }
    }
    as the latter idea disrupts the natural flow of the code, requires unnecessary nesting of condition statements, and makes one of the condition statements more complex. Try to keep nesting to a minimum and condition statements as simple as possible - it makes code far more readable and less error prone.
    Last edited by PredicateNormative; March 10th, 2009 at 10:21 AM.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured