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
Solved

Help in C++

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

792 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