-
March 10th, 2009, 08:38 AM
#1
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
-
March 10th, 2009, 08:43 AM
#2
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?
-
March 10th, 2009, 08:48 AM
#3
Re: I can't figure this out, can someone help me 'char' issues
Your else statement is ill-formatted.
-
March 10th, 2009, 08:50 AM
#4
Re: I can't figure this out, can someone help me 'char' issues
Originally Posted by Lindley
Your else statement is ill-formatted.
Good eye.
-
March 10th, 2009, 09:01 AM
#5
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
-
March 10th, 2009, 09:09 AM
#6
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');
-
March 10th, 2009, 09:12 AM
#7
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.
-
March 10th, 2009, 09:24 AM
#8
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?
-
March 10th, 2009, 09:29 AM
#9
Re: I can't figure this out, can someone help me 'char' issues
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.
-
March 10th, 2009, 09:30 AM
#10
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
-
March 10th, 2009, 09:34 AM
#11
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;
-
March 10th, 2009, 09:34 AM
#12
Re: I can't figure this out, can someone help me 'char' issues
Originally Posted by austinguy27
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.
-
March 10th, 2009, 09:38 AM
#13
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;
-
March 10th, 2009, 09:45 AM
#14
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...
-
March 10th, 2009, 10:19 AM
#15
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|