1. Help with this program

char sex; // 'M' == male 'F' == female
int age; // Infant age less than 2
// toddlers age greater than or equal 2 and less than 4
// child age greater than equal to 4 and less than 13
// teen age greater than equal 13 less than 20
// adult age greater than or equal to 20 and less than 55
// senior age greater than or equal 55
Based on the information provided above, write a program that finds female teen and male child.
The output should look like the following:
Please fill out the following information.
Sex: Age:
A female teen or a male child found
or
Neither a female teen nor a male child found

Display either of the two messages based on the result of the input data.

Code:
#include "stdafx.h"
#include <iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])

{
char sex;
int  age;

cout << "Sex (M or F):";
cin >> sex;
cout <<"Age: ";
cin >> age;

if ((sex == M) || (sex == F) && (age <2))
else if ((sex == M) || (sex == F) && (age >=2 && age < 4))
else if ((sex == M) || (sex == F) && (age >= 4 && age < 13))
else if ((sex == M) || (sex == F) && (age >= 13 && age < 20))
else if ((sex == M) || (sex == F) && (age >= 20 && age < 55))
else if ((sex == M) || (sex == F) && (age >= 55))

cout << "\nSex:";
cout << gender;
cout << "\nAge:";
cout << age << endl;

return 0;
}
This is what i got so far following an example from the book. I just want to know if I'm going in the right direction some advice would be helpful as to where to go next from here. I still figuring out how to declared the ages but i need a little help. Thanks

2. Well if you put all the MF in single quotes, like 'M', then it should be at least compilable.

3. you need to have something (either a semicolon, or braces) after each if/else if in order to make it compile. next, you need to put some code inside those braces if you want it to actually do something.

4. When you type X where X is any identifier except for built-in keywords, the compiler will search for the meaning of this identifier. It may be a type, a variable, etc. But it may a string or character. To tell the compiler that it is a character you are typing, surround it with quotes, eg 'X'. To tell the compiler it is a string, surround it with double quotes, eg "X".

5. Originally Posted by Salem
Well if you put all the MF in single quotes, like 'M', then it should be at least compilable.
That's one of the things that i did when i went through it again and I have changed it but I still need some advice since a friend told me this " the logic of your program does not answer the question". I thought i was answering it.

Here is my code after fixing it:

Code:
#include "stdafx.h"
#include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
int age;
char sex;

cout << "Please fill out the following information." << endl;

cout <<"\nSex: ";
cin >> sex;
cout << "Age: ";
cin >> age;

if ((sex == 'M') || (sex == 'F') && (age <2))
{ 	if (sex == 'M' && age < 2)
cout << "\nA male infant found" << endl;
if (sex == 'F' && age < 2)
cout << "\nA female infant found" << endl;
}
else if ((sex == 'M') || (sex == 'F') && (age >=2 && age < 4))
{ 	if (sex == 'M' && age >=2 && age < 4)
cout << "\nA male  toddler found" << endl;
if (sex == 'F' && age >=2 && age < 4)
cout << "\nA female toddler found" << endl;
}
else if ((sex == 'M') || (sex == 'F') && (age >= 4 && age < 13))
{ 	if (sex == 'M' && age >= 4 && age < 13)
cout << "\nA male child found" << endl;
if (sex == 'F' && age >= 4 && age < 13)
cout << "\nA female child found" << endl;
}
else if ((sex == 'M') || (sex == 'F') && (age >= 13 && age < 20))
{ 	if (sex == 'M' && age >= 13 && age < 20)
cout << "\nA male teen found" << endl;
if (sex == 'F' && age >= 13 && age < 20)
cout << "\nA female teen found" << endl;
}
else if ((sex == 'M') || (sex == 'F') && (age >= 20 && age < 55))
{ 	if (sex == 'M' && age >= 20 && age < 55)
cout << "\nA male adult found" << endl;
if (sex == 'F' && age >= 20 && age < 55)
cout << "\nA female adult found" << endl;
}
else if ((sex == 'M') || (sex == 'F') && (age >= 55))
{ 	if (sex == 'M' && age >= 55)
cout << "\nA male senior found" << endl;
if (sex == 'F' && age >= 55)
cout << "\nA female senior found" << endl;
}
return 0;
}

6. Code:
if ((sex == 'M') || (sex == 'F') && (age <2))
{ 	if (sex == 'M' && age < 2)
cout << "\nA male infant found" << endl;
if (sex == 'F' && age < 2)
cout << "\nA female infant found" << endl;
}
Consider this, where you're not copy/pasting the same conditions multiple times.
Code:
if ( age < 2 ) {
if ( sex == 'M' ) {
}
else if ( sex == 'F' ) {
}
else {
}
}

7. well looks like i dont know how to post because I just replied with this long thing but it came out to be code /code for some reason...

but what i said pretty much was couldnt you just do and it would work the same way?

Code:
if ((sex == 'M') && (age < 2))
cout << "Male Infant";
if ((sex == 'F') && (age < 2))
cout << "Female Infant";

8. You can, but again, what Salem is implying is that (age < 2) is a condition that must apply both the if statements. Hence, you end up copying the same condition multiple times.
This is a no-no. If you have the condition in one, you have to change it in all places, and that becomes a nightmare the more times it's duplicated.
So why not break out that condition so that it occurs only in one place? It saves some keystrokes too.

9. yeah I didn't understand what his code was at first and then i realized that he is saying If the age is less than two then it is gonna be a male if the user enters M or a female if they enter F...This will definetly help me in my programming class