Solved

Help in C++

Posted on 2003-12-07
5
230 Views
Last Modified: 2010-04-02
hi

im trying to do a program about an array of ppl.

i need to let the user input the details of 10 or less ppl. and the information to be stored in th program. the program should consis of two loops th 1st loop should continue repeating round and allows the user to enter the next person until the array is full up or the user decides to stop. the 2nd loop is shold print out all the dtails entered by the usr. i need to delare an array of 19 ppl and an integer that will hold the numbr of ppl so far.

this is wat i done so far....and now im confused.....n stuck plz help me

#include <iostream.h>

class person
{
private:

int age;

public:

void set_age(int a);
int get_age();
void print();
void input();
};
void person::input()
{
cout<<"age?"<<endl;
cin>>age;
}
void person::set_age(int a)
{
age = a;
}
int person::get_age()
{
return age;
}
void person:rint()
{
cout << age<<endl;
}
int main()
{
person[10];
int num_stored;
}

please help
0
Comment
Question by:UJ
5 Comments
 
LVL 1

Accepted Solution

by:
meow00 earned 60 total points
ID: 9893472
Hello,

    I am not quite sure what u need, but several things :
 
1.
-----------------------
 void person:rint()
{
cout << age<<endl;
}
----------------------
should be "void person::print()". typo ?

2. In main, the declaration of person is not proper. I would change to :
-----------------------------------------
int main()
{
 person PPL[10];
 int num_stored;
 for(int i=0; i<10;i++){
  PPL[i].input() ;
 }
 return 0 ;
}
-----------------------------------------

 It would be good if u can describe more clear about what u need !

 meow ....
0
 

Author Comment

by:UJ
ID: 9893756
i  need to let the user input the details of 10 or less ppl. and the information to be stored in th program. i need to delare an array of 10 ppl and an integer that will hold the numbr of ppl so far.

the program should consist of two loops th 1st loop should continue repeating round and allows the user to enter the next person until the array is full up or the user decides to stop. the 2nd loop is shold print out all the dtails entered by the user.
0
 
LVL 3

Expert Comment

by:RJSoft
ID: 9894355
You have not instructed your person objects to do anything.

int main()
{
person[10];
int num_stored;
}


First you need to create the 10 people. You did that with this statement

person[10];

This means that you now have 10 objects (or instances) of the class person.

Begin Outer loop//////////////////////////

Next you need to load those 10 persons.

I am not going to give you the exact code, but give you hints...

Remember person is an array of objects (arrays can be indexed, array[0])


index = zero (valid is 0 to 9)

begin load loop

clear the screen here.

prompt "Enter person age or X to exit"
INPUT = get input value from user.

if user entered X break this loop.

person is indexed to first instance (index value).SetAge(INPUT);

index is advanced by 1.

if index is equal to 10 then break this loop.

end load loop


Now you have some or all of your persons loaded with values.

Also it is possible that the user did not enter anything here. So you might want to check for that condition, if so say someting like.

Press Y if you wish to exit app? if so break the outer loop. That is because you probably dont want to print an empty person.

Note index still contains the value of how many of the 10 persons got an age value. So you can use it in the next printing loop part.

//printing loop
int X=0;
while(1)
{

print the value of the person using the X to index from person to person of your array and of each person use the accessor function GetAge to return that persons age. Use newline at the end of each print statement to go down a line.

X++;
if(X==index)break;
}//endwhile and end printing loop

You might like a pause statement here. Sometimes that can getch() ya.

Now you printed out what the user stated. Time to do it all over again.

So here is the end of the outer loop but before you do the whole thing over you might want to reset your index. Either at the top of the loop with index =0 or here at the bottom. Just so you dont keep adding and adding to your idex which will cause an error if you go past 9 (remember valid is 0 to 9 in C/C++ array)

End outer loop/////////////////////////////////

RJ

0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question