please help with C++ program

This is a discussion on please help with C++ program within the C++ Programming forums, part of the General Programming Boards category; write a c++ program to read an unspecified number of input records. each input record will contain a code and ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    7

    Unhappy please help with C++ program

    write a c++ program to read an unspecified number of input records. each input record will contain a code and a person's age. a code of 1 will indicate female; a code of 2 will indicate male; and a code of 0 will indicate end of input(exit code). Any other code is invalid and must display an appropriate error message. continue accepting and processing input records until the exit code is enter. Finally compute and display the following:
    1. number of males 21 years old or older.
    2. Number of females 21 years old or older.
    3.Average age of all persons under 21.
    4.total number of people


    Code:
    																					 
    #inlcude<iostream>
    
    int main()
    {
     int age, code, m_age1, male2, f_age1, f_age2
      count=0,max;
    
    			std::cout<<" Input code and Age or press 0 to exit and press <Enter>" <<std::endl
    	 
    	 while(code !=0)
    	  {
    		
    
    		std::cin>>code, age;
    		}
    
    	 if(m_age>=21)
    	   {
    	  m_age1=m_age1+m_total
    	  m_total=m_total+m_age1
    
    		std::cout<< m_total<<std::endl;
    
    		else if (f_age>=21)
    			 
    		 f_age1=f_age1+f_total
    		 f_total=f_total+f_age1
    	  
    
    		 std::cout<< f_total/ f_age1<<std::endl;
    
    		else
    
    		 f_age2=f_age2+f_total2
    		 f_total2=f_total+f_age2
    
    		  std::cout<<f_total2<<std::endl;
    		 }
    
    		  std::cout<< m_age2+f_age2 <<std::endl;
    		  std::cout<< f_total + m_total <<std::endl;
    }
    	 return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    The answer is 42.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    7
    whats 42???

  4. #4
    Registered User
    Join Date
    Mar 2008
    Posts
    18
    Quote Originally Posted by Desolation View Post
    The answer is 42.
    LOL
    It's the answer to everything!

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    7
    everything in the code???

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,407
    In other words, you need to tell us how does it not work.

    Oh, and kindly indent your code more consistently.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    7
    im asking and making sure im headed in the right direction.
    im new to c++ and afraid to admit that im kinda lost in the class...

  8. #8
    Registered User
    Join Date
    May 2006
    Posts
    903
    Quote Originally Posted by c++manofyear View Post
    whats 42???
    What's the question ?

  9. #9
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    Have you tried to compile this?

    I can see at least two errors. One being a missing colon, and another being your cin >> read statement is not right. Compile the code, then try to fix the errors. If you still need help then post the errors here.
    I'm just trying to be a better person - My Name Is Earl

  10. #10
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    there is a lot wrong here; you need to study much harder.

    you're reading age from stdin, and then performing logic on m_age and f_age; variables that are never set. because these variables are never set, your math will be wrong. however, even if you were assigning values to these variables, the way you're going about doing it is incorrect and overly convoluted. you need to pay more attention to the problem statement and what it's asking you.

    you never prompt the user for input after the first iteration of the while loop.

    you need to really think about what exactly you are instructing your program to do.

    you need to think about what the problem is asking you to do. think about the information you need to track.

    it looks like you have made the problem more difficult for yourself by haphazardly jumping into it without giving any forethought to what is being asked in the problem, not to mention your code is illegible due to poor formatting.

    do yourself a favor and think through how it needs to work before you start coding. draw a block diagram if you find it really confusing.

    i got my butt kicked a few times in school too, so i feel badly for you. here is an example of how your program might work.

    Code:
    int promptage()
    {
            int age;
            cout<<" Input age and press <Enter>" <<endl;
            cin >> age;
            return age;
    }
    
    int main()
    {
            #define stop 0
            #define female 1
            #define male 2
            int age, code, males=0,females=0,minors=0,totalMinorAge=0;
            while(code !=stop)
            {
                    cout<<" Input gender code or press 0 to exit and press <Enter>" <<endl;
                    cin>>code;
                    if(code!=stop)
                    {
                            switch(code)
                            {
                                    case female:
                                            age = promptage();
                                            if(age<21)
                                            {
                                                    totalMinorAge+=age;
                                                    minors++;
                                            }
                                            else
                                            {
                                                    females++;
                                            }
                                            break;
                                    case male:
                                            age = promptage();
                                            if(age<21)
                                            {
                                                    totalMinorAge+=age;
                                                    minors++;
                                            }
                                            else
                                            {
                                                    males++;
                                            }
                                            break;
                                    default:
                                            cout<< "Invalid Code"<<endl;
                                            break;
    
                            }
                    }
            }
            cout<<"Statistics:"<<endl;
            cout<<"Number of females over 21: "<<females<<endl;
            cout<<"Number of males over 21: "<<males<<endl;
            cout<<"Total number of people: "<<males+females+minors<<endl;
            if(minors>0)
            {
                    cout<<"Average Age of Minors: "<<(float)totalMinorAge/(float)minors<<endl;
            }
            cout << "Press Any Key to Exit\n";
            char c;
            cin >> c;
            return 0;
    }

  11. #11
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    Dont use #define in C++ unless its in a header file. Use a constant or an enumuation.

    To keep the console open, at the end, use:

    Code:
    std::cin.get()
    std::cin.ignore(); // if you use dev-c++
    Instead of all the char and cin >> code.
    I'm just trying to be a better person - My Name Is Earl

  12. #12
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    Quote Originally Posted by c++manofyear View Post
    whats 42???
    It's a reference found in Douglas Adams' book The Hitchhiker's Guide to the Galaxy. The number 42 turns out in the book to be the answer to life, the universe and everything.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  13. #13

    Join Date
    Apr 2008
    Location
    USA
    Posts
    76
    If you don't want to use macros:
    Code:
    #include <iostream>
    using namespace std;
    
    enum GenderCodes
    {
        STOP /* = 0 */,
        FEMALE /* = 1 */,
        MALE /* = 2 */
    };
    
    const int ADULT = 21;
    
    int main()
    {
        cout << "Gender Codes: 1 = Female, 2 = Male, 0 = Stop" << endl << endl;
        int input, nMales = 0, nFemales = 0, nAdultMales = 0, nAdultFemales = 0, nMinorAgeSum = 0;
        do
        {
            cout << "Please enter the Gender Code (0 to stop): ";
            cin >> input;
            if( input != STOP && input != MALE && input != FEMALE )
            {
                cout << "Invalid Gender Code. 1 = Female, 2 = Male, 0 = Stop." << endl << endl;
                continue;
            }
    
            if( input != STOP )
            {
                int age;
                cout << "Please enter the age of this person: ";
                cin >> age;
    
                if( age < ADULT )
                {
                    nMinorAgeSum += age;
                }
    
                switch( input )
                {
                    case MALE:
                        nMales++;
                        if( age >= ADULT )
                        {
                            nAdultMales++;
                        }
    
                        break;
    
                    case FEMALE:
                        nFemales++;
                        if( age >= ADULT )
                        {
                            nAdultFemales++;
                        }
    
                        break;
                }
            }
        } while( input != STOP );
    
        cout << "# Males:          " << nMales << endl;
        cout << "# Females:        " << nFemales << endl << endl;
        cout << "# Adult Males:    " << nAdultMales << endl;
        cout << "# Adult Females:  " << nAdultFemales << endl;
        cout << "# Adults Total:   " << (nAdultMales + nAdultFemales) << endl << endl; // #Adults = #Adult Males + #Adult Females
        cout << "# Minors:         " << (nMales + nFemales) - (nAdultMales + nAdultFemales) << endl; // #Minors = #Total - #Adults
        cout << "Avg. Minor Age:   " << ( nMinorAgeSum / ((nMales + nFemales) - (nAdultMales + nAdultFemales)) ) << endl << endl; // Avg. Minor Age = Sum / #Minors
        cout << "Total:            " << (nMales + nFemales) << endl; // Total = #Males + #Females
    
        system("PAUSE"); // For Dev-C++
        return 0;
    }

  14. #14
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Is this the Do Others' Homework day?

    While you may find the assignment challenging enough to attempt it yourself, refrain from posting full solutions.

    Now OP will just pick and hand in one of the solutions and learn nothing.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  15. #15
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Quote Originally Posted by anon View Post
    Is this the Do Others' Homework day?

    While you may find the assignment challenging enough to attempt it yourself, refrain from posting full solutions.

    Now OP will just pick and hand in one of the solutions and learn nothing.
    Thanks...was thinking the same thing. I'm working with three people now - professional programmers on salary - that run to me with every compile or run time error after doing zero analysis on their own. It's best to nip this kind of dependence in the bud.

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 12:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 12:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 09:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

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