facing problem in getting input in classes

#include<iostream>
using namespace std;
class person
{
protected:
int id;
char name[50];
char add[100];
public:
void get_info();
void show_info();
person();
};
void person::get_info()
{
cout<<"enter your ID:"<<endl;
cin>>id;
cout<<"enter your name:"<<endl;
cin.get(name,50);
cout<<"enter your address:"<<endl;
cin.get(add,100);
}
void person::show_info()
{
cout<<"id:"<<id<<endl;
cout<<"name:"<<name<<endl;
cout<<"address"<<add<<endl;
}
person::person()
{
id=0;
name[0]='\0';
add[0]='\0';
}
class student:public person
{
private:
int rno;
int marks;
public:
void get_infos();
void show_infos();
student();
};
student::student()
{

rno=0;
marks=0;
}
void student::get_infos()
{
cout<<"enter your roll no:"<<endl;
cin>>rno;
cout<<"enter your marks:"<<endl;
cin>>marks;
}
void student::show_infos()
{
cout<<"roll no:"<<rno<<endl;
cout<<"marks:"<<marks<<endl;
}
int main()
{
student s;
s.get_info();
s.get_infos();
s.show_info();
s.show_infos();
return 0;
}
Last edited on
Please use code tags: Your code when posting in future::

See: http://www.cplusplus.com/articles/z13hAqkS/

Can you describe the problem a bit more?

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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include<iostream>
using namespace std;
class person
{
protected:
int id;
char name[50];
char add[100];
public:
void get_info();
void show_info();
person();
};
void person::get_info()
{
cout<<"enter your ID:"<<endl;
cin>>id;
cout<<"enter your name:"<<endl;
cin.get(name,50);
cout<<"enter your address:"<<endl;
cin.get(add,100);
}
void person::show_info()
{
cout<<"id:"<<id<<endl;
cout<<"name:"<<name<<endl;
cout<<"address"<<add<<endl;
}
person::person()
{
id=0;
name[0]='\0';
add[0]='\0';
}
class student:public person
{
private:
int rno;
int marks;
public:
void get_infos();
void show_infos();
student();
};
student::student()
{

rno=0;
marks=0;
}
void student::get_infos()
{
cout<<"enter your roll no:"<<endl;
cin>>rno;
cout<<"enter your marks:"<<endl;
cin>>marks;
}
void student::show_infos()
{
cout<<"roll no:"<<rno<<endl;
cout<<"marks:"<<marks<<endl;
}
int main()
{
student s;
s.get_info();
s.get_infos();
s.show_info();
s.show_infos();
return 0;
}
it is not getting inputs for the object
Ok.

Put cin.ignore() after the cin, like below and let me know if it works

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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include<iostream>
using namespace std;
class person
{
protected:
int id;
char name[50];
char add[100];
public:
void get_info();
void show_info();
person();
};
void person::get_info()
{
cout<<"enter your ID:"<<endl;
cin>>id;
cin.ignore(); //<-
cout<<"enter your name:"<<endl;
cin.get(name,50);
cin.ignore();//<-
cout<<"enter your address:"<<endl;
cin.get(add,100);
cin.ignore();//<-
}
void person::show_info()
{
cout<<"id:"<<id<<endl;
cout<<"name:"<<name<<endl;
cout<<"address"<<add<<endl;
}
person::person()
{
id=0;
name[0]='\0';
add[0]='\0';
}
class student:public person
{
private:
int rno;
int marks;
public:
void get_infos();
void show_infos();
student();
};
student::student()
{

rno=0;
marks=0;
}
void student::get_infos()
{
cout<<"enter your roll no:"<<endl;
cin>>rno;
cin.ignore(); //<-
cout<<"enter your marks:"<<endl;
cin>>marks;
cin.ignore();//<-
}
void student::show_infos()
{
cout<<"roll no:"<<rno<<endl;
cout<<"marks:"<<marks<<endl;
}
int main()
{
student s;
s.get_info();
s.get_infos();
s.show_info();
s.show_infos();
return 0;
}

thnx
Don't forget to put cin.ignore() after line 30
and after line 66 if you want to display the results.
Otherwise your console will exit without displaying the input...
Topic archived. No new replies allowed.