Hi all,
I built a class type 'books' which includes the title and the price of a particular book. And I want to use vectors to store all the books information I have. This code should be able to get all the books' information and store them into the vector.
But it seems no possible to use xxx.push_back(yyy) or iterators to add new elements to vector<books>.... Could anyone show me the correct way to deal with this kind of problem?
Thanks in advance.
This is the code I used by mistake.
Code:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class books{
private:
string title;
double price;
public:
void getname(){
string temp;
cout<<"Please input the name of the book"<<endl;
cin>>temp;
title=temp;
}
void printname(){
cout<<"Title"<<title<<endl;
}
void getprice(){
double temp;
cout<<"Please input the price of the book"<<endl;
cin>>temp;
price=temp;
}
void printprice(){
cout<<"Price"<<price<<endl;
}
};
int main(){
char ch;
vector<books> bvec;
vector<books>::iterator iter=bvec.begin();
do{
*iter.getname();
*iter.getprice();
iter++;
cout<<"Another book info?"<<endl;
cin>>ch;
}while(ch=='y');
cout<<"The Books Stored:"<<endl;
for(iter=bvec.begin();iter!=bvec.end();++iter){
*iter.printname();
*iter.printprice();
}
return 0;
}