gpt4 book ai didi

c++ - 输入数据成员未按要求工作

转载 作者:行者123 更新时间:2023-12-01 14:47:27 25 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





Why does std::getline() skip input after a formatted extraction?

(4 个回答)


去年关闭。




几乎一切正常,直到您的书名出现。如您所见,我提供了 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com