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:
#include <iostream>
usingnamespace 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;
}
elseif (input == 2)
{
totalChildren += 1;
}
elseif (input == 3)
{
totalTeens += 1;
}
elseif (input == 4)
{
totalAdults += 1;
}
elseif (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;
}
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
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++;
elseif(peopleTypes[i] == 2)
child++;
elseif(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++;
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?