花>太阳
我想打印出所有带有" children ' sbook '; "在末尾键入
例如:
bookname : tower
booktype : drama
bookname : flower
booktype : childrensbook
bookname : sun
booktype : childrensbook
...
...
...
儿童读物是:
#include <iostream>
#include <string.h>
using namespace std;
class Books
{
public:
string name;
string type;
void getdetails() {
cout <<"enter book's name : ";
cin >> name;
cout <<"enter book's type : ";
cin >> type;
cout << endl;
}
};
int main() {
string name2[19];
string type2[19];
string test = "childrensbook";
Books book1;
for (int i=0;i<5;i++)
{
cout << "Book " << (i+1) << " info : " << endl;
book1.getdetails();
name2[i] == book1.name;
type2[i] == book1.type;
if(test == book1.type)
{
/*HERE I WANT TO SAVE THE BOOKS THAT ACTUALLY HAVE
"childrensbook" TYPE AND PRINT THEM OUT */
}
}
return 0;
}
试试这个:
#include <iostream>
#include <string>
using namespace std;
class Book
{
public:
string name;
string type;
void getdetails() {
cout << "enter book's name : ";
getline(cin, name);
cout << "enter book's type : ";
getline(cin, type);
}
};
int main() {
string test = "childrensbook";
Book books[5];
for (int i = 0; i < 5; i++)
{
cout << "Book " << (i+1) << " info : " << endl;
books[i].getdetails();
}
for (int i = 0; i < 5; i++)
{
if (books[i].type == test)
{
cout << "bookname : " << books[i].name << endl;
}
}
return 0;
}
在线演示