Ages

This is a discussion on Ages within the C++ Programming forums, part of the General Programming Boards category; Hey guys I just started working on a problem that states "Write a C++ program for a theater that will ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    7

    Ages

    Hey guys

    I just started working on a problem that states "Write a C++ program for a theater that will keep track of a movie attendee. The program should ask the user to enter their eage and gender. The user should enter a negative number when there are no more ages to enter. The program should then break down the attendees by age and gender. The following age categories should be used:

    0-18 years
    19-30 years
    31 - 40 years
    41 - 60 years
    Over 60 years

    The program should also display how many males and females attended the movie, in addition to computing and displaying the age of all attendees, the age of the youngest attendee, and that of the oldest.

    Code:
    #include <iostream>
    #include <iomanip>
    #include <cmath>
    
    
    using namespace std;
    
    
    int main()
    {
    	int age(0), sex(0), youngest(999), oldest(0), M(0), F(0), age1(0), age2(0), age3(0), age4(0), age5(0), totalage(0), totalcount(0);
    	
    	do
    	{
    		cout << "Enter sex \n" <<endl;
    		cin >> sex;
    			
    			if (sex != M || sex != F)
    			{
    			cout << "Invalid sex entered \n" << endl;
    			break;
    			}
    			
    		cout << "Enter age of attendee \n" << endl;
    		cin >> age;
    
    
    			if (age < 0)
    			{
    			cout << "Invalid age \n" << endl;
    			break;
    			}
    			
    
    
    			if (age >=0 && age <= 18)
    					++age1;
    			
    
    
    			if (age >= 19 && age <= 30)
    					++age2;
    			
    
    
    			if (age >= 31 && age <= 40)
    					++age3;
    			
    
    
    			if(age >=41 && age <=60)
    					++age4;
    			
    
    
    			if (age >=61 && age <=100)
    					++age5;
    			
    
    
    			if (age < youngest)
    					youngest += age;
    
    
    			if (age > oldest)
    					oldest +=age;
    
    
    		++ totalcount;
    		++ totalage;
    			
    
    
    	} while (age > 0 );
    
    
    	
    	return 0;
    
    
    }
    This is what I have so far and I am very close to breaking it, but I can't seem to reach that point. Help?

  2. #2
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    Code:
    int sex(0), M(0), F(0);
    
    ...
    
    cout << "Enter sex \n" <<endl;
    cin >> sex;
                 
    if (sex != M || sex != F)
    {
        cout << "Invalid sex entered \n" << endl;
        break;
    }
    When you ask cin to read in an int, it's going to look for digits, and if it doesn't find digits (ie, if you type in 'F' or 'M') cin is going to fail.
    On the other hand, if you ask cin to read in a char, things will work more like I think you expect it to: cin will read in one character and put that in your variable.

    Second,
    Code:
    (sex != M || sex != F)
    What does this actually do? Keep in mind, you're comparing integers, and variable names shouldn't affect the result of the comparison.
    Last edited by bernt; 11-30-2011 at 09:47 AM.
    Consider this post signed

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    7

    Ages

    The purpose of that second statement is to give an error when the user enters another variable, say....A or B. Although I changed what you suggested, I am still getting an error.

  4. #4
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    But when you enter 'M' or 'F' in the console, cin does nothing to associate those characters with a variable in your program. And when you compare (sex != M || sex != F), that has nothing to do with whether sex contains 'M' or 'F'.

    Let's consider what's in these variables (sex, M, and F) once you get to that point.
    Say sex contains the character 'M'. M and F are set to 0, per the values you gave for initializing them. Then this statement becomes
    Code:
    if ('M' != 0 || 'M' != 0)
    Say sex contains 'F' or 'A' or '0' ('0' != 0, by the way, characters are stored according to the ASCII code)... do you see why this is incorrect?
    Consider this post signed

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    7

    Ages

    I have been working on this code for SO long now And I'm really close to breaking it :/ But I'm stuck.

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,631
    You must learn to differentiate

    if (X == A)
    and
    if (X == 'A')

    The first says, if X is equal to the contents in A, and the second one says if X is equal to the character A.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Homework: Program to work out geological ages
    By traed in forum C++ Programming
    Replies: 6
    Last Post: 04-25-2011, 08:04 PM
  2. People that take AGES to reply!
    By kevinj in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 02-25-2004, 07:35 AM
  3. Ages
    By Vicious in forum A Brief History of Cprogramming.com
    Replies: 56
    Last Post: 05-19-2002, 07:56 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21