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;
}
#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;
}

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!
C++ getline errors I am getting getline is undefined error messages. I would like the variables...
This is just a partial C++ code. I am having a problem with the getline(cin, variable) statement of my code. Please run the code for yourself and see that it doesn't let you enter the full name. It skips the prompt to enter the name and goes straight to the next cout statement. I can't use cin alone because I need to capture and store both the first name followed by space followed by last name. I was trying to...
i have problem where the address is not save to my file after i input a new address and exit it #include<string> #include<iostream> #include<fstream> #include<sstream> using namespace std; typedef struct date { int day; int month; int year; }Date; typedef struct add { string building; string street; string city; string state; string zip; }Address; typedef struct entry { string firstName; string lastName; Address address; string phNo; Date...
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 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 <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...
I am getting an error that the variable num was not declared in the scope on the line of code that has; while(num != 5). Do you know how to fixe this error? #include <iostream> #include <iomanip> #include <string> #include<fstream> #include <cstring> using namespace std; //Declare the structure struct Games { string visit_team; int home_score; int visit_score; }; // Function prototypes int readData(Games *&stats); int menu(); void pickGame(Games *&stats, int size); void inputValid (Games *&stats, int size); void homeTotal(Games *&stats,...
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...
The project I have is a link list that asks for a name and phone number and it allows the user to insert ,delete ,modify ,and find an element in the structure. The program has a menu in which the user can choose which function they want to use I need to use switch case to do this but it has not worked I need help with this. Thank you! #include <iostream> #include <string> #include <iomanip> using namespace std; const int...
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 ( 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. ===========================================================...