Help counting people in my array.

This is a discussion on Help counting people in my array. within the C++ Programming forums, part of the General Programming Boards category; I wrote this program and so far so good but now I have to be able to count how many ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    23

    Help counting people in my array.

    I wrote this program and so far so good but now I have to be able to count how many 1's, 2's, 3's, and 4's were entered. I am at a complete loss of where to begin. Could someone help me get started?

    Code:
    #include "stdafx.h"
    using namespace std;
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	const int People = 5;
    	int i, PeopleTypes[People];
    
    cout << "Enter 1 for Infant, 2 for Child, 3 for Teenager, or 4 for Adult\n";
    cout << "for each person that attended the school function.\n\n";
    
    	for(i=0; i<People; i++)
    	{
    		cout << "Person #" << i+1 << ": ";
    		cin  >> PeopleTypes[i];
    	if (PeopleTypes[i]>4 || PeopleTypes[i]<1)
    	{
    	cout << "This is invalid input!\n\n";
    	
    	}
    	if (PeopleTypes[i]<0)
    		break;
    	}
    	
    
    	
    }

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I think you don't want to input to PeopleTypes[i] but to increment PeopleTypes[n] if n is within the bounds. As I understand PeopleTypes is an array of counters.

    Also, you probably want to loop more than People times, for example until a negative number is entered.
    Last edited by anon; 10-17-2007 at 05:20 PM.
    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).

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    You're going about the loop you have the wrong way. You are looping once per person time. You should be looping while the user inputs a number greater than 0.

    Then, inside that loop you should be counting how many times the user enters each person type.

    Does that make sense?

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    23
    It makes sense but how can I make it loop like that?

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    You need to loop while the user enters a number greater than 0.

    Now turn that phrase into code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Modify an single passed array element
    By swgh in forum C Programming
    Replies: 3
    Last Post: 08-04-2007, 09:58 AM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  3. Template Array Class
    By hpy_gilmore8 in forum C++ Programming
    Replies: 15
    Last Post: 04-12-2004, 12:15 AM
  4. Creating 2D arrays on heap
    By sundeeptuteja in forum C++ Programming
    Replies: 6
    Last Post: 08-16-2002, 12:44 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 09:31 AM

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