Solved

Help in C++

Posted on 2003-12-07
5
225 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
MSVCR80.dll crash 2 126
C++ get user from AD  (VS6) 7 48
C++ to C# code conversion issue 4 89
White board coding practice 3 61
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now