can i get a solution for this??

creating a class with function to get data and display the same. n then creating the array for the same object of the class eg( if my class is person and p is the object) can we use array for p where i can take multiple inputs?
i tried it out but didn't got the desired o/p

#include<iostream>
#include<string>
class person
{
string name[];
int age;
public:
void getdata()
{
cout<<"name";
cin>>name;
cout<<"age";
cin>>age;
}
void display()
{
cout<<name;
cout<<age;
}
}p;
void main()
{
int p[3];
int i;
for(i=0;i<4;i++)
{ p[i]=p.getdata();}
for(i=0;i<4;i++)
{ p[i]=p.display();}
}
If you want to create something like a database, object p would not help! p must be an array and the p in main() must not be an array. it is not even needed. Corrected code with brief explanation:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include<iostream>
#include<string>
#include<cstdlib>           //header files you know them

using namespace std;     //this namespace is to make cout and cin visible.

class person
{
    string name;
    int age;

    public:
        void getdata()
        {
            cout<<"name: ";
            getline(cin,name,'\n');     //this is to accept a line. ie: it waits for the user to hit enter, 
                                        //and is good b'cause it allows the user to leave spaces. eg: Micheal Sambo.
                                        // cin, will read up to micheal because of the space but getline will read the whole line!!
            cout<<"age: ";
            cin>>age;
            cin.ignore();     //to flush the buffer in order to avoid running into codes without waiting for user input
        }
        void display()
        {
            cout<<name<<endl;    //endl - end line just like '\n'
            cout<<age<<endl<<flush<<"\n";   //flush - also to flush buffer. note used with cout.
        }
}p[5];   //array of a class. an array of type person with five elements

int main()
{
    int i;

    for(i=0;i<5;i++)
    {
        p[i].getdata();
    }
    system("CLS");     //to clear screen defined in cstdlib header file
    for(i=0;i<5;i++)
    {
        p[i].display();
    }
    cin.sync();  //also to clear buffer
    cin.get();    //wait for user to hit enter
}
Last edited on
@ Aceix: Not to be too picky but you should be careful saying things like "always include XXX...". I'm referring to your comment on Line 5 regarding the inclusion of the std namespace. In this particular case the std namespace is really only useful for console programming with the STL. You would rarely use it if you are writing with an API like WinAPI, SDL, SFML or others like it for example. The problem comes up because it defines A LOT more then cout and cin and although memory isn't as scarce of a resource as it once was due to things like the amount of memory in modern systems and the optimization of your code by the linker and compiler, it's still bad practice to include things that you're not using.

Also, although Line 28 is fine, it may not be clear to a new user what you are doing, you may want to mention in your comments that this is the declaration of the array. This is also a global variable, which given to scope of the 'person' array, should be avoided.
Last edited on
Well, "always do X" rules solely exist for beginners. No experienced programmer would pay them any heed.
using doesn't include anything, so this is not a matter of memory.
The std namespace has dozens of templates, classes and structs. If you told me that they were all going to be included anyway or that they were optimized out at compile time then I could bring myself to believe it, otherwise all of those things are using up space on the v-table at run time. Maybe I am just being pedantic, but for me it was harder to unlearn habits like this then if I were to not learned them in the first place.
Ok thanks for the info but the reason why I do that is because std::cout confuses most beginners...even me(sometimes)!!!
@Computergeek01: you don't pay for what you don't use.
¿Never heard of `undefined reference' when they put the templates in a cpp?

However, there is the compilation time overhead (I hate you CImg.h)
thanxx guys :D n specially aceix
Topic archived. No new replies allowed.