### Is there any way to do this?

I need a program who ask for a number of persons (n persons)
and asks for each person's age...

then it should remember the number of person and it's ages...
and then print the different range lists of the persons:

Range 1: 0-18 years
Range 2:19-30 years
Range 3:31-60 years
Range 4:61years or more.

for example this needs to be the output:

 enter number of persons:6 enter age for person 1: 35 enter age for person 2: 40 enter age for person 3: 14 enter age for person 4: 78 enter age for person 5: 24 enter age for person 6: 12 Range 1 persons (0-18): person 3: 14 years person 6: 12 years Range 2 persons (19-30): person 5: 24 years Range 3 persons (31-60): person 1: 35 years person 2: 40 years Range 4 persons (61->): person 4: 78 years

This is my source code(It's not working)
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112`` ``````#include "stdafx.h" #include "math.h" #include #include using namespace std; int main() { //inicio int A[100][100],R1[100][100],R2[100][100],R3[100][100],R4[100][100]; int n; cin>> n; int m=1,c1=1,c2=1,c3=1,c4=1; for(int i=1; i<=n;i++) { for(int j=1;j<=m;j++) { cin >>A[i][j]; if (A[i][j]>=0<=18) { c1=c1+1; //Matriz rango1 for(int a=1 ;a<=c1 ; a++) { for (int b=1;b<=m; b++) { R1[a][b]=R1[(A[i][j])][c1]; } } cout<<"Pertenecientes al Primer Rango:"; for(int a=1 ;a<=c1 ; a++) { for (int b=1;b<=m; b++) { cout<

Is there any easier way to do that, or what's the mistake in my code?
please can some one help me out?
There is a much easier way to do this. I'll post my solution soon.
 ``123456789101112131415161718192021222324252627282930313233343536373839`` ``````#include #include #include int main(void) { int numberPpl, age, i = 0; std::vector aContainer; std::cout << "Number of people: "; std::cin >> numberPpl; for(i; i < numberPpl; ++i) { std::cout << "Age: "; std::cin >> age; aContainer.push_back(age); } sort(aContainer.begin(), aContainer.end()); bool range[4] = { false, false, false, false }; std::vector::const_iterator iter = aContainer.begin(); for(i = 1; iter != aContainer.end(); ++iter, ++i) { if(*iter >= 0 && *iter <= 18 && !range[0]) { range[0] = true; std::cout << "\nRange 0 - 18:" << std::endl; } else if(*iter >= 19 && *iter <= 30 && !range[1]) { range[1] = true; std::cout << "\nRange 19 - 30:" << std::endl; } else if(*iter >= 31 && *iter <= 60 && !range[2]) { range[2] = true; std::cout << "\nRange 31 - 60:" << std::endl; } else if(*iter >= 61 && !range[3]) { range[3] = true; std::cout << "\nRange 61 -> :" << std::endl; } std::cout << "Person " << i << ": " << *iter << std::endl; } std::cin.get(); return 0; }``````

Eh, it could be better.
Last edited on
 ``123456789101112131415161718192021222324`` ``````#include #include #include int main() { // A multimap to hold name and age std::multimap mPeople; std::string name; int age; // Input name and age while(std::cin >> name >> age) mPeople.insert(std::pair(name, age)); // Insert the name/age pairs std::cout << "\nRange (0-18):" << std::endl; for(std::multimap::const_iterator it = mPeople.begin(); it != mPeople.end(); ++it) { // If the age is between 0 and 18. Print out the name and age if(it->second >= 0 && it->second <= 18) std::cout << it->first << " " << it->second << std::endl; } return 0; }``````
He didn't want to store their names, did he?
Topic archived. No new replies allowed.