Trying to set a class member string with getline
Aug 1, 2018 at 3:34pm UTC
Hi I am trying to build a class of strings so I can experiment with sorting it with std::sort. Why cant i get the setname function to work with getline so i can get the data into the instance. How do i make it work. Thanks.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
class addressBook
{
public :
string Setname(string name) {name = itsName;}
private :
string itsName;
string itsStreetNumber;
string itsTown;
string itsCounty;
string itsPostcode;
};
int main()
{
addressBook pageOne;
cout << "Enter the name.\n" ;
getline(cin,pageOne.Setname);
}
Aug 1, 2018 at 3:46pm UTC
getline(cin,pageOne.Setname);
The second parameter should be a string.
pageOne.Setname
is not a string. It's a function.
Aug 1, 2018 at 3:58pm UTC
Use a default constructor for this. Example...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
class addressBook
{
public :
addressBook(); //a default constructor
string Setname(string name) { name = itsName; }
string Getname() { return itsName; }
private :
string itsName;
string itsStreetNumber;
string itsTown;
string itsCounty;
string itsPostcode;
};
addressBook::addressBook() {
cout << "Enter the name.\n" ;
getline(cin, itsName);
}
int main()
{
addressBook pageOne;
cout << pageOne.Getname();
cin.get();
return 0;
}
Topic archived. No new replies allowed.