eagergrammer eagergrammer - 1 month ago 5
C++ Question

Can't convert string to int in return. No string variable declared whatsoever in int returning function getsocial()

#include<iostream>
#include<string>
using namespace std;
/*this program takes a bank customer's info with the use of a class*/
//class definition
class BankCustomer
{
public:
BankCustomer(); //constructor for BankCustomer class
void fullname(string, string);
string firstname();
string lastname();
bool setsocial(int s); //accept a arg. of int type
int getsocial();
private:
string fname, lname, SSNlength; //can't be changed by client; sensitive info should be made private
};

//class implementation
BankCustomer::BankCustomer(){}

void BankCustomer::fullname(string f, string l)
{
fname=f;
lname=l;
}

string BankCustomer::firstname()
{
return fname;
}

string BankCustomer::lastname()
{
return lname;
}

bool BankCustomer::setsocial(int s)//function verifies that entered SSN is 9 digits long by counting # of digits of entered SSN
{
int count, SSNlength;
while(s != 0)
{
s /=10;
++count;
if(count == 9)
{
cout <<"\nValid SSN Entered!" << endl;
SSNlength=s;
return true;
}
}
}

int BankCustomer::getsocial()
{
return SSNlength;
}

//client program
int main()
{
BankCustomer customer; //customer declared as object of BankCust class
string firstname, lastname;
int ssn, s;

//data assignment
cout <<"\n Enter First Name\n" << endl;
cin >> firstname;

cout<<"\n Enter Last Name\n"<< endl;
cin >> lastname;

customer.fullname(firstname,lastname);

do
{
cout<<"\nEnter 9-Digit SSN"<< endl;
cin >> ssn;
customer.setsocial(ssn);
}
while(!customer.setsocial(ssn)); //function will repeat as long as entered user ssn forces setsocial() to evaluate it as false

//data ouput
cout <<"\nFirst Name: "<<customer.firstname()<<"\n"<< endl;
cout <<"\nLast Name: "<<customer.lastname()<<"\n"<< endl;
cout <<"\n SSN is: "<<customer.getsocial()<<"\n" << endl;
}


The error is targeting BankCustomer::getsocial(). The variable SSNlength is declared as an int type and getsocial() has a return type of int. I don't see anywhere in my code where I intend to convert SSNlength to a string. Is it how I'm passing and handling data with the setsocial() function?

Answer

That is wrong:

int BankCustomer::getsocial()
{
return SSNlength;
}

since SSNlength is a member variable of the class BankCustomer of type string, visible from BankCustomer::getsocial, even if not declared in the method. That how C++ works.

So the compiler complains that it cannot convert a string to an int (what you want to return).

this is where you declare it:

private:
    string fname, lname, SSNlength; //can't be changed by client; sensitive info should be made private

Given the name of the variable, I suspect that you wanted to write:

private:
    string fname, lname;
    int SSNlength; //can't be changed by client; sensitive info should be made private

be careful, there's an auto variable called the same way in another method.

I suggest a naming rule for members to avoid all that. Example: prefix all members with m_:

private:
    string m_fname, m_lname
    int m_SSNlength; //can't be changed by client; sensitive info should be made private