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;
}
これはマルチレベル継承の実装です。 コンパイル中にエラーが発生しました。 しかし、ときに私は上記のコードスニペット、cin.getline(F_name,30,'\n');
から実行時の問題コードスニペットのランタイムスキップを解決する方法cin.getline of C++
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');
}
が動作していない直面していますプログラムを実行します。 毎回プログラムを実行してくださいcout<<endl<<" Enter the Mother's Name :"; cin.getline(M_name,30,'\n');
cin.getline(F_name,30,'\n');
をスキップして実行してください。ここで
のでハプニングとそのコードの代替とは何であるのはなぜ誰もが説明できます。
ありがとうございました。
あなたがcin>>marks;
を行う際に改行を空行としてそれを解釈getline
次の呼び出しのためのバッファに残っているAcadamic_details::input
で
どうすれば解決できますか? –
@KunalVermaどこでも 'getline'を使うのを止め、' >> '演算子だけを使用しますか?または、 'getline'を使って数字を取得し、文字列を数値に変換しますか?または、改行まで次の文字を[無視する](http://en.cppreference.com/w/cpp/io/basic_istream/ignore)ですか? –
解決策を見つけました。 ((cin >> marks).ignore();を使って) –