Sol Sol - 1 month ago 6
C++ Question

How can you use getline to input values to classes in C++?

I thought I have done everything correctly to make this work, but I get these errors:

..\CS115_IP2_SolJohnston.cpp:107:2: error: 'Customer1' was not declared in this scope
Customer1.setCustName("");
^

..\CS115_IP2_SolJohnston.cpp:152:29: error: cannot convert 'std::__cxx11::string {aka std::__cxx11::basic_string}' to 'Customer*' for argument '1' to 'void viewAddress(Customer*)'
viewAddress(name, address);
^

void displayMenu(string userName)
{

cout << userName << ", please select an action from the menu below" << endl;

cout<<"My Menu";
cout<<"========" << endl;
cout<<"0 - View Your Order Name and Address" << endl;

cout<<"X - Exit " <<endl<<endl;
}
class Customer
{
private:
string CustName;
string CustAddress;

public:
void setCustName(string);
string getCustName();

void setCustAddress(string);
string getCustAddress();

// Constructor
// create empty placeholders
Customer();
};

//definition of set/get member functions of Employee class
void Customer::setCustName(string name){CustName=name;}

void Customer::setCustAddress(string address){CustAddress=address;}

string Customer::getCustName() { return CustName; }

string Customer::getCustAddress() { return CustAddress; }


Customer::Customer()
{
CustName = "";
CustAddress = "";

}

void viewAddress(Customer *Cust)
{
cout << "Name: " << Cust->getCustName() << endl;
cout << "Address: " << Cust->getCustAddress() << endl;
}

int main(void)
{
Customer1.setCustName("");
Customer1.setCustAddress("");

string name = "";
string address = "";

cout << "Please enter your Address: street, city, state==> ";
getline(cin, address);
Customer1.setCustAddress(address);



cout << "Hello "+ name + " from " + address << endl;

do
{
// display menu
displayMenu(name);
// read user selection
cin>>selection;
switch(selection)
{
case '0':
cout<< "Your Address is" << endl;
viewAddress(name, address);
break;
case 'X' :
case 'x':
cout<<"Thank you!!!" << endl;
break;
default : cout<<"Invalid selection. Please try again";
// no break in the default case
}
cout<<endl<<endl;
} while (selection!= 'X' && selection != 'x');

return 0;
}

Answer

you are trying to use an undeclared object in main customer1. so declare an object of type Customer then use it.

int main()
{
     Customer customer1; // instantiate Customer class
    Customer1.setCustName("");
    Customer1.setCustAddress("");

}
Comments