?
Solved

Help in C++

Posted on 2003-12-07
5
Medium Priority
?
250 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 1

Accepted Solution

by:
meow00 earned 240 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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 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.

649 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