0
#include<conio.h>
#include<iostream>
using namespace std;

class Student
{
private:
char name[20];
char rollno[20];

public:
void input();
void display(); 
};
class Acadamic_details:public Student
{
private:
    float marks,percentage;
public:
    void input();
    void display();
};
    class Guardian_details:public Acadamic_details
    {
    private:
        char F_name[20],M_name[20];
    public:
        void input();
        void display();
    };

    void Student::input()
    {
    cout<<endl<<" Enter the Name : ";
    cin.getline(name,30,'\n');
    cout<<endl<<" Enter the Roll No :";
    cin.getline(rollno,30,'\n');
    }
    void Acadamic_details::input()
    {
    Student::input();
    cout<<endl<<" Enter the Marks out of 500 :";
    cin>>marks;
    }
    void Guardian_details::input()
    {
        Acadamic_details::input();
        cout<<endl<<" Enter the Father's Name :";
        cin.getline(F_name,30,'\n');
        cout<<endl<<" Enter the Mother's Name :";
        cin.getline(M_name,30,'\n');
    }

    void Student::display()
    {
    cout<<endl<<" Name : "<<name<<endl;
    cout<<" Roll No : "<<rollno<<endl; 
    }   
    void Acadamic_details::display()
    {
        Student::display();
        percentage=(marks/500)*100;
        cout<<endl<<" Marks : "<<marks<<"/500"<<endl;
        cout<<" Percentage : "<<percentage<<"%"<<endl;
    }
            void Guardian_details::display()
            {
                Acadamic_details::display();
                cout<<endl<<" Father's Name : "<<F_name<<endl;
                cout<<" Mother's Name : "<<M_name<<endl;
            }

   int main()
   {
   Guardian_details g1;
   cout<<" Enter the Student's Details "<<endl;
   g1.input();
   cout<<" Student's Details Are : "<<endl;
   g1.display();
   getch();
   return 0;
   }

This is the Implementation of Multilevel Inheritance. There is no compilation Error encountered during Compilation. But When I executed the Program I am facing a run time Problem

void Guardian_details::input()
{
    Acadamic_details::input();
    cout<<endl<<" Enter the Father's Name :";
    cin.getline(F_name,30,'\n');
    cout<<endl<<" Enter the Mother's Name :";
    cin.getline(M_name,30,'\n');
}

from the above Code Snippet , cin.getline(F_name,30,'\n'); is not working. Everytime Run the Program It directly Execute the cout<<endl<<" Enter the Mother's Name :"; cin.getline(M_name,30,'\n');

skipping the cin.getline(F_name,30,'\n');.

Here is the Output Screen : Output Screen

Can anyone Explain Why is Happening so and What is the Alternative to that Code.

Thanks in Advance.

1 Answers1

2

In Acadamic_details::input when you do

cin>>marks;

the newline is left in the buffer for the next call to getline, which interprets it as an empty line.

Some programmer dude
  • 363,249
  • 31
  • 351
  • 550