Thread: Help with this program

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    5

    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. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well if you put all the MF in single quotes, like 'M', then it should be at least compilable.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    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. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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".
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Apr 2011
    Posts
    5
    Quote Originally Posted by Salem View Post
    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;
       }
    Last edited by Rockie; 10-06-2011 at 09:40 AM.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    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 {
      }
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Oct 2011
    Posts
    10
    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";
    Last edited by cullman; 10-06-2011 at 02:17 PM.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Oct 2011
    Posts
    10
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help converting array program to link list program
    By hsmith1976 in forum C++ Programming
    Replies: 0
    Last Post: 02-14-2010, 09:50 PM
  2. Replies: 1
    Last Post: 03-03-2009, 04:47 PM
  3. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  4. Replies: 5
    Last Post: 08-16-2007, 11:43 PM
  5. Replies: 18
    Last Post: 11-13-2006, 01:11 PM