Arrays and User Input

I have an assignment...

Implement a class Person with the following data members:
- name (string ) - name of this person
- age (integer) - age of this person

Write a program that reads in a list of names and ages and stores them in a one-dimensional array of Person objects. The maximum number of names that will be entered is 100 names. After reading in the list of names and ages, sort the list of people from the youngest (lowest age) to oldest (highest age). Then print out the name and age for each person in the sorted list.

Sample Output
Enter name (-1 to stop): Bart
Enter age of Bart: 10
Enter name (-1 to stop): Lisa
Enter age of Lisa: 8
Enter name (-1 to stop): Maggie
Enter age of Maggie: 1
Enter name (-1 to stop): Homer
Enter age of Homer: 36
Enter name ( -1 to stop): Marge
Enter age of Marge: 34
Enter name (-1 to stop):-1

Name: Maggie, age: 1
Name: Lisa, age: 8
Name: Bart, age:10
Name: Marge, age: 34
Name: Home, age: 36

I really have no idea how to do this problem. What I thought is I would start with a program that asks for the user to enter ages and stops repeating after entering -1 for the age. I could use a while loop to do this correct? I have been able to write a program with an array to get 5 ages and display them on the screen but cannot figure out how to write it with a while statement having the maximum number of objects in the array be 100 and stopping the user input after -1 is entered.

Would making an array that reads the user input of ages and displays them back be a good starting point for this problem? If so could someone please help me figure out what to do for this and if not please let me know where a good starting point would be. Below is my code for the 5 object array of ages.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;

int main()
{
	int age[5] = {0};
	
	for ( int sub = 0; sub < 5; sub +=1)
	{
		cout << "Enter the age: ";
		cin >> age[sub];
	}

	for (int sub = 0; sub < 5; sub +=1)
	{
		cout << "The ages are: ";
		cout << age[sub] << endl;
	}

	system("pause");
	return 0;
}
You can add an if statement that checks if age[sub] == -1 at the end of both loops (before the brace). If it does, use the break statement to stop the loop.

Btw, instead of calling your subscript variable "sub", common practice is to call it 'i' in the outermost loop, then if you use nested loops (a loop inside a loop is called a nested loop) you call the variable j in that one. If you need another nested loop, you use k, and so on (although you really shouldn't nest more than three levels deep).

I'd also recommend taking out system("pause") and replacing it with cin.get();
you need to use classes. create a Person class class ...

class Person{

string name;
int age;

}

this creates a constructor for the class that can now be used just like int or char.
ex. Person x;
Person abc;

each "Person" has their own name and age the same way an int has an associated value.
so after you create the class you can then use it in your main program.

ex. int main(){
Person a;

then create an array of "Person"s just as u would an int array.

Person arr[100];

create an int variable count that we ll use later to avoid substrings.
create a for loop with i while less than 100.

cout a question for name. then cin the name into a local string. then make that string equal to arr[i].name;

what this does is that the name field of Person i in the Person array, (arr[]) will be equal to the string entered. remember how we had string name in the class constructor?
Using .name or ."whatevervaribaleyouentered" will let you access this variable for that "Person."

do the same for the age by typing arr[i].age = whatever they entered.
dont forget to count ++ at the end of the loop.
close up the loop, then print everything out with a for loop resetting i, going until i = count;
cout << "Name: " << arr[i].name << " Age: " << arr[i].age << endl;

hope your gettin it ...
Tech junkie,

I am following you until I get to cin the name into a local string. I tried to put

1
2
3
cout << "Enter the name: ";
cin >> name;
name = arr[i].name;


but it keeps telling me that identifier name is undefined then I tried

1
2
3
cout << "Enter the name:";
cin >> Person::name;
name = arr[i].name;


and it tells me that name is inaccessible.

So I'm not really sure what to do. I think once I figure that out I will have no problem putting in the for statements and doing the loops.
I'm assuming you declared "string name" in the main program so based on the code you wrote, you're basically trying to give the "name" in the main program the value of arr[i].name but we never gave that a value.
So basically switch the equals statements around.

arr[i].name = name;

use the first code you tried. but this wont fix the error either way.
im pretty sure its because if not defined the class is set to private automatically.

so changing the the class to "public class Person" should open up access to the variables inside. i thought it was auto set to public but i guess not.

another way to do this would be to add accessing functions. basically u put these in the class function and declare it just as you would a regular function.

1
2
3
public int getAge(){
return age;
} 


think of this as a president and his secretaries. you cant speak to him directly but you can go through the secretaries who can get information from him. then use these the same way you would arr[i].age

arr[i].getAge() will give you the age value of that person. you can set it though, youd need another function for that but you ll prolly learn this in class soon
Topic archived. No new replies allowed.