Reputation: 23
I'm working on this code and it works with cin>>partname;
instead of using getline(cin, partname);
in the showpart function but for only names without blank space. But with using getline(cin, partname);
it produces an error
error: no matching function for call to ‘getline(std::istream&, const string&)’
#include<iostream>
#include<cstring>
using namespace std;
class Inventory
{
private:
int partno;
string partname;
float cost;
void getpart()
{
cout<<"Enter the part number"<<endl;
cin>>partno;
cout<<"Enter the part name"<<endl;
cin>>partname;
cout<<"Enter the cost"<<endl;
cin>>cost;
}
public:
Inventory()
{
partno = 0;
partname = " ";
cost = 0.0;
}
Inventory(int pn,string pname,float c)
{
partno = pn;
partname = pname;
cost = c;
}
void setpart()
{
getpart();
}
void showpart() const
{
cout<<"Inventory details"<<endl;
cout<<"Part Number: "<<partno<<endl;
cout<<"Part Name: ";
getline(cin, partname);
cout<<"\nCost: "<<cost<<endl;
}
};
int main()
{
Inventory I1(1,"Resistor", 25.0), I2;
I2.setpart();
I1.showpart();
I2.showpart();
}
I've looked through similar errors but they seem not to be helpful.Thank you for looking through this.
Upvotes: 2
Views: 93
Reputation: 117851
#include <string>
instead of <cstring>
.
Also:
void showpart() const
can't be const
since you update partname
(which is an odd activity for a function called show
-something).
I suspect that you want to show the partname
, not to update it:
void showpart() const
{
cout<<"Inventory details"<<endl;
cout<<"Part Number: "<<partno<<endl;
cout<<"Part Name: "<<partname<<endl;
cout<<"Cost: "<<cost<<endl;
}
Upvotes: 4