noob with problems

i have to have a homework project in by Wed. at midnight. the instructions state: define an array 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 1 represents an infant, a 2 represents a child, a 3 represents a teen, and a 4 represents an adult who is present at a school function. any other interger value should not be accpted as valid input and data entry should stop when a negative value has been entered. the program should cout the number of each 1,2,3,and 4 in the array and the output alist of how many infants,children, teens, and adults were at the school function.


any ideas on how to attack this program is welcome, keep in mind i have no idea how to start and i am NOT a programmer
I wouldn't have even said you needed an array for this. Just create a loop and check the input each time, and then when somebody presses 1 for example add a number to a total for infant, child, tenn & adult. Try this code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
using namespace std;
int main()
{
	//create total variables:
	int totalInfants = 0;
	int totalChildren = 0;
	int totalTeens = 0;
	int totalAdults = 0;
	int input;
	cout << "Press 1 for an infant, 2 for a child, 3 for a teen and 4 for an adult. Enter a ";
	cout << "number less than 0 to finish and calculate totals" << endl;
	for (int i=0; i<50; i++)
	{
		cout << "Please enter type: ";
		cin >> input;
		if (input == 1)
		{
			totalInfants += 1;
		}
		else if (input == 2)
		{
			totalChildren += 1;
		}
		else if (input == 3)
		{
			totalTeens += 1;
		}
		else if (input == 4)
		{
			totalAdults += 1;
		}
		else if (input <= 0)
		{
			break;	//exit the loop if we entered a number less than or equal to 0. 
		}
	}
	// print totals:
	cout << "Number of infants: " << totalInfants << endl;
	cout << "number of children: " << totalChildren << endl;
	cout << "number of teens: " << totalTeens << endl;
	cout << "number of adults: " << totalAdults << endl;
	return 0;
}

closed account (z05DSL3A)
matty3269, it doesn’t really matter if you can do it a different way if the coursework says use an array you use an array.

sladeb
here is a basic input method for what is asked, you will need to flesh it out and do your totals…

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <stdio.h>
#include <iostream>

const int maxPeople = 50;

typedef int PeopleTypes;


int main(int argc, char *argv[])
{
    PeopleTypes PeopleArray[maxPeople];

    bool finished = false;
    int entreis = 0;
    do
    {
        int input =0;
        std::cin >> input;
        if(input < 0)
        {
            finished = true;
        }
        else if (input > 4 || input == 0)
        {
            //output invalid input!
        }
        else
        {
            PeopleArray[entreis++] = input;
  
            if  (entreis == maxPeople)
           {
               //output full measage
                finished = true;
            }
        }
    }while(!finished);

    for(int i =0; i <= entreis; i++)
    {
        std::cout << PeopleArray[i] << std::endl;
    }

  return 0;
}
Last edited on
grey wolf,
what do you mean when you say "flesh it out"?
and wounldn't i start with:

int PeopleTypes[50]
{
infants[0]=1
children[1]=2
teens[2]=3
adults[3]=4
Last edited on
closed account (z05DSL3A)
What I mean be ‘flesh out’ is you should put in the rest of the code to make this a fully functional program, as it is it is a skeletal structure.

Having read your original post again, I must have read “…define an array PeopleType that…” as “…define an array of PeopleTypes…”

So drop the
 
typdef int PeopleTypes; 


Change to start of the main to..

1
2
3
int main(int argc, char *argv[])
{
    int PeopleTypes[maxPeople];


and in the else section change:

PeopleArray[entreis++] = input;

To

PeopleTypes[entreis++] = input;

The code sets up and array to hold fifty integer values (define an array PeopleTypes that “can store a maximum of 50 interger values”)

The do loop is setup to received user input via the keyboard and will exit the loop when one of the following conditions is met. First if the user enters a negative number and second when fifty valid entries are received.

The next part “…the program should cout the number of each 1,2,3,and 4 in the array and the output alist of how many infants, children, teens, and adults were at the school function.” Is left for you to code

Hope that helps.
grey wolf,
would this work for the compulations for the number of 1s, 2s,3s, and 4s:

cout<< "Enter a type: ";
cin >> peopleTypes[i];
if(peopleTypes[i]== 1)
infant++;
else if(peopleTypes[i] == 2)
child++;
else if(peopleTypes[i] == 3)
teen++;
else if(peopleTypes[i] == 4)
adult++;
else
cout << "Invalid entry" << endl;
i++;
}while(peopleTypes[i-1]>0);

cout << "\nThere were: " << endl;
cout << infant << " infants" << endl;
cout << child << " children" << endl;
cout << teen << " teenagers" << endl;
cout << adult << " adults"<< endl;

closed account (z05DSL3A)
You need to validate your input before you put it into storage, that it why I use 'int input'. when I lnow that the input is valid I then put the value in storage "PeopleArray[entreis++] = input;" (this is equivalent to PeopleArray[entreis] = input; entreis++; ).

The last bit of my code is a bit of a hint as to how to loop through the array and this section of your code would fit in there. (as long as your define your variables before the loop [infant, child...])

1
2
3
4
5
6
7
8
9
if(peopleTypes[i]== 1)
    infant++;
else if(peopleTypes[i] == 2)
    child++;
else if(peopleTypes[i] == 3)
   teen++;
else  //  if(peopleTypes[i] == 4) as you have validated your input you don't 
        //  need to test this. If it is not 1, 2, or 3, it must be 4
    adult++;


...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <stdlib.h> //for system("pause") call
#include <stdio.h>
#include <iostream>

const int maxPeople = 50;

int main(int argc, char *argv[])
{
    int _peopleTypes[maxPeople];

    // TODO:: Output instruction.

    bool _finished = false;
    int _entreis = 0;
    do
    {

        int input =0;
        std::cout << "[" << _entreis << "]: " ;
        std::cin >> input;
        if(input < 0)
        {
            _finished = true;
        }
        else if (input > 4 || input == 0)
        {
            // TODO:: output invalid input message!
        }
        else
        {
            _peopleTypes[_entreis++] = input;
  
            if  (_entreis == maxPeople)
           {
               //output full measage
                _finished = true;
            }
        }
    }while(!_finished);

    int _infant = 0;
    int _child  = 0;
    int _teen   = 0;
    int _adult  = 0;

    for(int i =0; i <= _entreis-1; i++)
    {
        //std::cout << _peopleTypes[i] <<" ";

        if(_peopleTypes[i]== 1)
            _infant++;
        else if(_peopleTypes[i] == 2)
            _child++;
        else if(_peopleTypes[i] == 3)
            _teen++;
        else if(_peopleTypes[i] == 4)
            _adult++;
    }

    cout << "\nThere were: " << endl;
    cout << _infant << " infants" << endl;
    cout << _child << " children" << endl;
    cout << _teen << " teenagers" << endl;
    cout << _adult << " adults"<< endl;

    system("pause");

    return 0;
}

Last edited on
i don't know how to validate, i have debugged but it says it failed and i get error c1010 "unexpected end of file while looking for precompiled header directive" when i debug. what am i doing wrong?
closed account (z05DSL3A)
I'm asumming you are using a version of Visual Studio from the c1010 error, if so have a read of this:
http://support.microsoft.com/kb/815644
Topic archived. No new replies allowed.