Whilst running example code from a textbook, I receive errors from the compiler in relation to using getline(). I would like to extract the string from user input and use it within a data structure.
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>
usingnamespace std;
constint MAXRECS = 3; // maximum number of records
struct TeleType
{
char name;
char phoneNo;
TeleType *nextaddr;
};
void populate(TeleType *); // function prototype needed by main()
void display(TeleType *); // function prototype needed by main()
int main()
{
int i;
TeleType *list, *current; // two pointers to structures of
// type TeleType
// get a pointer to the first structure in the list
list = new TeleType;
current = list;
// populate the current structure and create the remaining
// structures
for (i = 0; i < MAXRECS - 1; i++)
{
populate(current);
current->nextaddr = new TeleType;
current = current->nextaddr;
}
populate(current); // populate the last structure
current->nextaddr = NULL; // set the last address to a NULL address
cout << "\nThe list consists of the following records:\n";
display(list); // display the structures
return 0;
}
// input a name and phone number
void populate(TeleType *record) // record is a pointer to a
{ // structure of type TeleType
cout << "Enter a name: ";
getline(cin, record->name);
cout << "Enter the phone number: ";
getline(cin, record->phoneNo);
return;
}
void display(TeleType *contents) // contents is a pointer to a
{ // structure of type TeleType
while (contents != NULL) // display until end of linked list
{
cout << endl << setiosflags(ios::left)
<< setw(30) << contents->name
<< setw(20) << contents->phoneNo;
contents = contents->nextaddr;
}
cout << endl;
return;
}
Example of one of the errors.
Severity Code Description Project File Line Suppression State
Error (active) E0304 no instance of overloaded function "getline" matches the argument list Example13.11 c:\Users\Kish\Documents\Visual Studio 2017\Projects\Example13.11\Example13.11\Example13.11.cpp 53