Aug 1, 2018 at 4:16pm Aug 1, 2018 at 4:16pm UTC
What this line string theSpare = itsName;
says is "assign the current value of itsName to theSpare". It does not make theSpare an alias for itsName. If you want to modify the value of itsName you need to std::getline() into it, on line 26.
Last edited on Aug 1, 2018 at 4:16pm Aug 1, 2018 at 4:16pm UTC
Aug 1, 2018 at 11:51pm Aug 1, 2018 at 11:51pm UTC
Please can you show me how you would alter itsName with cin or getlin(cin) without the defualt constructor?
That would be greatly appreciated. Many thanks.
Aug 2, 2018 at 12:02am Aug 2, 2018 at 12:02am UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14
class addressBook{
public :
void setItsName(const std::string &s){
this ->itsName = s;
}
//...
};
//...
std::string line;
std::getline(std::cin, line);
pageOne.setItsName(line);
Last edited on Aug 2, 2018 at 12:04am Aug 2, 2018 at 12:04am UTC
Aug 2, 2018 at 12:16pm Aug 2, 2018 at 12:16pm UTC
Without defining a default constructor, it is still done basically the same way...
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 38 39 40
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
class addressBook
{
public :
void Setname();
string Getname() { return itsName; }
private :
string itsName;
string itsStreetNumber;
string itsTown;
string itsCounty;
string itsPostcode;
};
void addressBook::Setname() {
cout << "Enter the name.\n" ;
getline(cin, itsName);
}
int main()
{
addressBook pageOne;
pageOne.Setname();
cout << pageOne.Getname();
cin.get();
return 0;
}
Last edited on Aug 2, 2018 at 12:17pm Aug 2, 2018 at 12:17pm UTC