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)
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include "stdafx.h"
#include "math.h"
#include <cstdio>
#include <iostream>
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<<A[i][j] <<"=" <<R1[a][b];
//Cierre Matriz rango1
}}}

else if (A[i][j]>=19<=30)
{
c1=c1+1;
//Matriz rango2
for(int a=1 ;a<=c1 ; a++)
{
for (int b=1;b<=m; b++)
{
R2[a][b]=R2[(A[i][j])][c1];
}
}
cout<<"Pertenecientes al segundo Rango:";
for(int a=1 ;a<=c1 ; a++)
{
for (int b=1;b<=m; b++)
{
cout<<A[i][j] <<"=" <<R2[a][b];
//Cierre Matriz rango2
}}}

else if (A[i][j]>=31<=60)
{
c1=c1+1;
//Matriz rango3
for(int a=1 ;a<=c1 ; a++)
{
for (int b=1;b<=m; b++)
{
R3[a][b]=R3[(A[i][j])][c1];
}
}
cout<<"Pertenecientes al tercer Rango:";
for(int a=1 ;a<=c1 ; a++)
{
for (int b=1;b<=m; b++)
{
cout<<A[i][j] <<"=" <<R3[a][b];
}}
//Cierre Matriz rango3
}

else if (A[i][j]<60)
{
c1=c1+1;
//Matriz rango4
for(int a=1 ;a<=c1 ; a++)
{
for (int b=1;b<=m; b++)
{
R4[a][b]=R4[(A[i][j])][c1];
}
}
cout<<"Pertenecientes al cuarto Rango:";
for(int a=1 ;a<=c1 ; a++)
{
for (int b=1;b<=m; b++)
{
cout<<A[i][j] <<"=" <<R4[a][b];
}}
//Cierre Matriz rango4

}}

std::getchar(); 
std::getchar(); 

std::getchar(); 
std::getchar(); 

std::getchar(); 
std::getchar(); 

}


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.
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
#include <iostream>
#include <vector>
#include <algorithm>

int main(void) {
    int numberPpl, age, i = 0;
    std::vector<int> 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<int>::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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <map>
#include <string>

int main()
{
        // A multimap to hold name and age
	std::multimap<std::string, int> mPeople;

	std::string name;
	int age;
        // Input name and age
	while(std::cin >> name >> age)
		mPeople.insert(std::pair<std::string, int>(name, age)); // Insert the name/age pairs
	
	std::cout << "\nRange (0-18):" << std::endl;	
	for(std::multimap<std::string, int>::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.