Question

C++ getline errors I am getting getline is undefined error messages. I would like the variables...

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 <iostream>

#include<string.h>

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 << "\nEnter 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<index;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<size;i++) {

if (cust[i].firstNm == firstName && cust[i].lastNm == lastName) {

showCustomer(cust[i]);

return;

}

}

cout << "Customer not found" << endl;

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>
#include<string.h>

using namespace std;

int index = 0; // variable to hold how many customers are entered

struct Address //Structure for the address.

{

string street;

string city;

string state;

string 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 << "\nEnter 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<index;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<size;i++) {

if (cust[i].firstNm == firstName && cust[i].lastNm == lastName) {

showCustomer(cust[i]);

return;

}

}

cout << "Customer not found" << endl;

}

D: Ctry main.exe 1 Enter new customer 2. Display all customers 3 Display a particular customer 4. Exit the program Enter choi

The error was int decleration of struct Address:

struct Address //Structure for the address.

{

string street; // you declared all these as int

string city;

string state;

string zipcode;

};

Please give this solution a thumbs up and comment if you have any doubts.
Thank You!

Add a comment
Know the answer?
Add Answer to:
C++ getline errors I am getting getline is undefined error messages. I would like the variables...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need help with this code. I'm using C++ and Myprogramming lab to execute it. 11.7:...

    I need help with this code. I'm using C++ and Myprogramming lab to execute it. 11.7: Customer Accounts Write a program that uses a structure to store the following data about a customer account:      Customer name      Customer address      City      State      ZIP code      Telephone      Account balance      Date of last payment The program should use an array of at least 20 structures. It should let the user enter data into the array, change the contents of any element, and display all the...

  • The following C++ code include 3 files: Patient.h, Patient.cpp and Main.cpp. The program basically creates and stores patient records. The original code has everything in a single .cpp file. I tried t...

    The following C++ code include 3 files: Patient.h, Patient.cpp and Main.cpp. The program basically creates and stores patient records. The original code has everything in a single .cpp file. I tried to divide the code in 3 parts (Patient.h, Patient.cpp and Main.cpp), but it is giving me errors. Patient.h #ifndef PATIENT_H #define PATIENT_H #include <string> #include "Patient.cpp" using namespace std; class Patient{ private : string firstname; string lastname; string location; static int cnt; int id; public : Patient(string, string, string);...

  • c++, I am having trouble getting my program to compile, any help would be appreciated. #include...

    c++, I am having trouble getting my program to compile, any help would be appreciated. #include <iostream> #include <string> #include <string.h> #include <fstream> #include <stdlib.h> using namespace std; struct record { char artist[50]; char title[50]; char year[50]; }; class CD { //private members declared private: string artist; //asks for string string title; // asks for string int yearReleased; //asks for integer //public members declared public: CD(); CD(string,string,int); void setArtist(string); void setTitle(string); void setYearReleased(int); string getArtist() const; string getTitle() const; int...

  • Fix my code, if I the song or the artist name is not on the vector,...

    Fix my code, if I the song or the artist name is not on the vector, I want to user re-enter the correct song or artist name in the list, so no bug found in the program #include <iostream> #include <vector> #include <string> #include <algorithm> using namespace std; class musicList{ private: vector<string> songName; vector<string> artistName; public: void addSong(string sName, string aName){ songName.push_back(sName); artistName.push_back(aName); } void deleteSongName(string sName){ vector<string>::iterator result = find(songName.begin(), songName.end(), sName); if (result == songName.end()){ cout << "The...

  • How could I separate the following code to where I have a gradebook.cpp and gradebook.h file?...

    How could I separate the following code to where I have a gradebook.cpp and gradebook.h file? #include <iostream> #include <stdio.h> #include <string> using namespace std; class Gradebook { public : int homework[5] = {-1, -1, -1, -1, -1}; int quiz[5] = {-1, -1, -1, -1, -1}; int exam[3] = {-1, -1, -1}; string name; int printMenu() { int selection; cout << "-=| MAIN MENU |=-" << endl; cout << "1. Add a student" << endl; cout << "2. Remove a...

  • 1. in this programe i want add some products to be shown after choosing choise (...

    1. in this programe i want add some products to be shown after choosing choise ( show all products ) before i add any products. let say 10 products have added to the program then when the user choose (show all products ) all the 10 products appear and the user going to choose one and that one must has its own name, price,and quantity after that the quantity will be reduce. 2. allow the user to buy products. ===========================================================...

  • "Function does not take 0 arguments". I keep getting this error for my last line of...

    "Function does not take 0 arguments". I keep getting this error for my last line of code: cout << "First Name: " << employee1.getfirstName() << endl; I do not understand why. I am trying to put the name "Mike" into the firstName string. Why is my constructor not working? In main it should be set to string firstName, string lastName, int salary. Yet nothing is being put into the string. #include<iostream> #include<string> using namespace std; class Employee {    int...

  • Rework this project to include a class. As explained in class, your project should have its...

    Rework this project to include a class. As explained in class, your project should have its functionalities moved to a class, and then create each course as an object to a class that inherits all the different functionalities of the class. You createclass function should be used as a constructor that takes in the name of the file containing the student list. (This way different objects are created with different class list files.) Here is the code I need you...

  • im writing a c++ code and getting stuck on two parts. The first part, when I...

    im writing a c++ code and getting stuck on two parts. The first part, when I go to write the customer order out to the file, it writes everything but the price out right. I'll get a weird number like 5.95828e-039 or nan. When I printout to the console it works fine so not sure what's wrong. Second After I write the order out to the file, I then have to go in and read the file and calculate the...

  • I am trying to run this program in Visual Studio 2017. I keep getting this build...

    I am trying to run this program in Visual Studio 2017. I keep getting this build error: error MSB8036: The Windows SDK version 8.1 was not found. Install the required version of Windows SDK or change the SDK version in the project property pages or by right-clicking the solution and selecting "Retarget solution". 1>Done building project "ConsoleApplication2.vcxproj" -- FAILED. #include <iostream> #include<cstdlib> #include<fstream> #include<string> using namespace std; void showChoices() { cout << "\nMAIN MENU" << endl; cout << "1: Addition...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT