Question
C++ getline errors I am getting getline is undefined error messages. I would like the variables to remain as strings. Below is my code. #include
C++ getline errors I am getting getline is undefined error messages. I would like the variables to remain as strings. Below is my code.
#include
#include
using namespace std;
int index = 0; // variable to hold how many customers are entered
struct Address //Structure for the address.
{
int street;
int city;
int state;
int zipcode;
};
// Customer structure
struct Customer
{
string firstNm, lastNm;
Address busAddr, homeAddr;
};
// Functions
int displayMenu();
Customer getCustomer();
void showCustomer(Customer);
void allCustomers(Customer[]);
Address getAddress();
void findCust(Customer[], int);
int main()
{
// Declare array of customer struct
Customer cust[100];
// Show menu to user until they ask to exit.
while (true)
{
int choice = displayMenu();
switch (choice)
{
case 1:
cust[index] = getCustomer();
index++;
break;
case 2:
allCustomers(cust);
break;
case 3:
findCust(cust, index);
break;
case 4:
cout << "Exit program!!" << endl;
return 0;
break;
default:
cout << "Invalid selection!!" << endl;
}
cout << endl;
}
return 0;
}
int displayMenu()
{
// Display menu.
cout << "1. Enter new customer" << endl;
cout << "2. Display all customers" << endl;
cout << "3. Display a particular customer" << endl;
cout << "4. Exit the program" << endl;
int choice;
// User selects option they want.
cout << "Enter choice: ";
cin >> choice;
cin.ignore();
cout << endl;
// Return choice
return choice;
}
Address getAddress() {
// User enters requested information.
Address a;
cout << "Enter street: ";
getline(cin, a.street);
cout << "Enter city: ";
getline(cin, a.city);
cout << "Enter state: ";
getline(cin, a.state);
cout << "Enter zip code: ";
getline(cin, a.zipcode);
return a;
}
Customer getCustomer()
{
// Enter first name, last name and two addresses
// and return the customer
Customer c;
cout << "Enter first name: ";
getline(cin, c.firstNm);
cout << "Enter last name: ";
getline(cin, c.lastNm);
cout << "Enter business address - " << endl;
c.busAddr = getAddress();
cout << " Enter home address - " << endl;
c.homeAddr = getAddress();
cout << endl;
return c;
}
void showCustomer(Customer c) {
// Display customer details
cout << "First Name: " << c.firstNm << endl;
cout << "Last Name: " << c.lastNm << endl;
cout << "Business Address: " << c.busAddr.street << " , " << c.busAddr.city << " , " << c.busAddr.state << " , " << c.busAddr.zipcode << endl;
cout << "Home Address: " << c.homeAddr.street << " , " << c.homeAddr.city << " , " << c.homeAddr.state << " , " << c.homeAddr.zipcode << endl;
}
void allCustomers(Customer cust[]) {
for (int i = 0;i showCustomer(cust[i]); cout << endl; } } void findCust(Customer cust[], int size) { // Ask user to enter first and last name string firstName, lastName; cout << "Enter first name: "; getline(cin, firstName); cout << "Enter last name: "; getline(cin, lastName); cout << endl; // for (int i = 0;i if (cust[i].firstNm == firstName && cust[i].lastNm == lastName) { showCustomer(cust[i]); return; } } cout << "Customer not found" << endl; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started