CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jan 2009
    Posts
    1

    programming help

    I am a beginning student trying to work through homework. Can you please help me with this question:

    define an array named PeopleTypes that can store a maximum of 50 interger values that will be entered at the keyboard. Enter a series of 1s 2s 3s and 4s into the array where a 1 represents an infant, a 2 represents a child, a 3 represents a teenager, and a 4 represents an adult. any other interger value should not be accepted as valid input, and data entry should stop when a negative value has been entered.
    The program should count the number of each 1,2,3,4 in the array and output a list of how many infants, chidren, teenagers, adults there are.


    I can't seem to put it all together, here is some of my work so far:

    int main()
    {

    int i, peopletypes[50]; //declare array of int called peopleTypes with a limit [50]

    for(i= 0; i < 50; i++) //for loop
    {
    cout << "Enter type for # " << i; // prints the request
    cin >> peopletypes[i];
    if (peopletypes[i] < 1 || peopletypes[i] > 4) // must be between 1 and 4

    cout << "Your input must be between 1 and 4";


    else if (peopletypes[i] < 0) // must not be negative
    //quit and tally the totals


    }

    {

    int Infants = 0;
    int Children = 0;
    int Teens = 0;
    int Adults = 0;

    for(i = 0; i < 50; i++){ // TAlly the Scores

    if (peopletypes[i] = 1)
    Infants = Infants + 1;

    if (peopletypes[i] = 2)
    Children = Children + 1;

    if (peopletypes[i] = 3)
    Teens = Teens + 1;

    if (peopletypes[i] = 4)
    Adults = Adults + 1;
    }

    cout << "Infants :" << Infants << endl;
    cout << "Children :" << Children << endl;
    cout << "Teens :" << Teens << endl;
    cout << "Adults :" << Adults << endl;

  2. #2
    Join Date
    Jan 2009
    Posts
    35

    Re: programming help

    Code:
    int main()
    {
    
    int i, peopletypes[50]; //declare array of int called peopleTypes with a limit [50]
    
    for(i= 0; i < 50; i++) //for loop
    {
    cout << "Enter type for # " << i; // prints the request
    cin >> peopletypes[i];
    if (peopletypes[i] < 1 || peopletypes[i] > 4){ // must be between 1 and 4
    
    cout << "Your input must be between 1 and 4";
    i--;
    continue;//These two statements are to repeat entry for the cell
    }
    
    
    else if (peopletypes[i] < 0) // must not be negative
    break; //This exits the loop
    
    
    }
    
    int Infants = 0;
    int Children = 0;
    int Teens = 0;
    int Adults = 0;
    
    for(i = 0; i < 50; i++){ // TAlly the Scores
    
    if (peopletypes[i] = 1)
    Infants = Infants + 1;
    
    if (peopletypes[i] = 2)
    Children = Children + 1;
    
    if (peopletypes[i] = 3)
    Teens = Teens + 1;
    
    if (peopletypes[i] = 4)
    Adults = Adults + 1;
    }
    
    cout << "Infants :" << Infants << endl;
    cout << "Children :" << Children << endl;
    cout << "Teens :" << Teens << endl;
    cout << "Adults :" << Adults << endl;
    }
    Above is what I would do.

  3. #3
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: programming help

    There are still two problems with the above solution:
    1) What if something other than a number is entered? What if the user types "A", for instance? Try it. Not good. This needs to be handled.

    2) If a negative value is entered, then there will be fewer than 50 valid entries in the array. Your second loop needs to account for this.

  4. #4
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: programming help

    calc000,

    Please enable private messaging (this is covered in the "BEFORE you post" announcment at the top of the forum. Then contact me.

    Thanks

    There is no need to reply to this post on thread. I will delete it once PM is established...
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  5. #5
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: programming help

    Don't forget the difference between == and = when writing if statements.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured