几乎一切都工作正常,直到您的书名是。如您所见,我提供了 cin
获取标题并显示它。问题是仅此部分不允许我按要求输入标题。如果有人可以帮助我,我将不胜感激。如果我不能很好地表达自己,请引起我的注意。
#include <iostream>
#include <string>
using namespace std;
class library {
public:
// data members
string title;
int price;
int accessionNumber;
// public member function for getting info
void getInfo()
{
cout << "\n";
cout << "Please Provide the Following: " <<endl;
cout << "Book Title: ";
getline (cin, title);
cout << "Book Price: ";
cin >> price;
cout << "Book Accession Number: ";
cin >> accessionNumber;
}
// public member function for displaying info
void showInfo()
{
cout << " \n";
cout <<"The Title of your Book is: " <<title <<endl;
cout <<"The Price is $" <<price <<endl;
cout <<"Use the Accession Number " <<accessionNumber << " to locate it"<<endl;
}
};
class books: public library {
public:
// data member
int pages;
void getPageNumber(){
cout << " \n";
cout << "What page can i find the Summary? ";
cin >> pages;
}
void displayPageNumber(){
cout <<"Check page "<< pages <<endl;
}
};
class media: public library {
public:
// data member
string audioName;
void getaudioName(){
cout << " \n";
cout << "Can you tell me the title of the Christian audio? " <<endl;
//getline (cin, audioName);
cin >> audioName;
}
void printaudioName(){
cout <<"It is the voice of Bishop Ansah. He titled it "<< audioName <<endl;
}
};
class CD: public library {
public:
// data member
double playTime;
void getPlayTime(){
cout << " \n";
cout << "The OOPs Video lessons is about how many hours? ";
cin >> playTime;
}
void showPlayTime(){
cout <<"It should be about "<< playTime << " hours in all" <<endl;
}
};
int main() {
// creating an object for the various class
books bk;
media md;
CD cd;
// calling member functions from books class
bk.getPageNumber();
bk.displayPageNumber();
// calling member functions from media class
md.getaudioName();
md.printaudioName();
cd.getPlayTime();
cd.showPlayTime();
// calling member functions from library class
cd.getInfo();
cd.showInfo();
return 0;
}
这是代码的输出。记下书名:在哪里。它应该允许我输入书名,但它似乎没有这样做。附加星号只是为了脱颖而出。
What page can i find the Summary? 77
Check page 77
Can you tell me the title of the Christian audio?
Resurrection
It is the voice of Bishop Ansah. He titled it Resurrection
The OOPs Video lessons is about how many hours? 8
It should be about 8 hours in all
Please Provide the Following:
Book Title: Book Price: 9
Book Accession Number: 8
**Book Title**:
The Price is $9
Use the Accession Number 8 to locate it
最佳答案
class CD: public library {
public:
// data member
double playTime;
void getPlayTime(){
cout << " \n";
cout << "The OOPs Video lessons is about how many hours? ";
cin >> playTime;
cin.ignore();
}
void showPlayTime(){
cout <<"It should be about "<< playTime << " hours in all" <<endl;
}
};
cd.getPlayTime();
cd.showPlayTime();
// calling member functions from library class
cd.getInfo();
在 playTime 输入后添加 cin.ignore(),因为在使用 getline() 之前必须清除缓冲区。
有关进一步说明,请参阅此链接:https://www.tutorialspoint.com/what-is-the-use-of-cin-ignore-in-cplusplus
关于c++ - 输入数据成员未按要求工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62618490/