Jun 3, 2011 at 12:42pm UTC
hi... I was making a program in c++...the coding includes multiple inheritance and array of objects as its base.
I have made 3 objects...for the first object ,the program runs smoothly but when the second object is created(using the for loop) ...the compiler executes the second statement in the function get_info() without executing the first one i.e "enter name of the student".Here is the code, please help.
I have commented the statement which has a problem.
#include<iostream>
#include<conio.h>
using namespace std;
class student_info
{
char name[20];
char roll_no[20];
public:
void get_info();
void show_info();
};
void student_info::get_info()
{
cout<<endl<<"Enter name of student:";
gets(name); // <=*P R O B L E M*
cout<<endl;
cout<<"Enter student Roll number:";
cin>>roll_no;
}
void student_info::show_info()
{
cout<<endl<<"Student Name:";
cout<<name<<endl;
cout<<"Roll Number:";
cout<<roll_no<<endl;
}
class student_marks:public student_info
{
int math,eng,chem,phy,pdu,total;
float percent;
public:
void get_marks(int, int, int, int,int);
void show_marks();
};
void student_marks::get_marks(int m1, int m2, int m3, int m4,int m5)
{
math = m1;
eng = m2;
chem = m3;
phy = m4;
pdu = m5;
total = math + eng + chem + phy + pdu;
percent = float(((total*100)/500));
cout<<endl;
}
void student_marks::show_marks()
{
cout<<endl;
cout<<"Student Marks:"<<endl;
cout<<"MATH:"<<math<<endl;
cout<<"ENGLISH:"<<eng<<endl;
cout<<"CHEMISTRY:"<<chem<<endl;
cout<<"PHYSICS:"<<phy<<endl;
cout<<"GRAND TOTAL:"<<total<<endl;
cout<<"PERCENTAGE SECURED:"<<percent<<"%"<<endl;
}
class student_report: public student_marks
{
public:
void display();
};
void student_report::display()
{
cout<<endl<<"The student details are:"<<endl;
show_info();
show_marks();
}
int main()
{
class student_report mirror[3];
for(int i = 0; i<3; i++)
{
cout<<endl;
mirror[i].get_info();
mirror[i].get_marks(73+i,88,67+i,68+2*i,81-4*i);
mirror[i].display();
cout<<endl;
}
cout<<endl;
system("pause");
return 0;
}
can anyone tell me if I can use anything other than gets() to enter a string which has white space inbetween. thanks:)
Jun 3, 2011 at 4:50pm UTC
Use getline. http://www.cplusplus.com/reference/iostream/istream/getline/