Please review this C++ code and tell me how to fix it. For some reason it is not
ID: 639677 • Letter: P
Question
Please review this C++ code and tell me how to fix it. For some reason it is not compiling for me. It says that the errors are in the stream operators.
#include
using namespace std;
//EmailListClass declaration
class EmailListClass
{
private:
//Declaration of a structure for the list
struct ContactNode
{
string name; // the name value in this node
string emailAddress; // the email adress value in this node
struct ContactNode *next; //Struct ContactNode pointer to the next node
};
ContactNode *head; //Pointer to the head of the ContactList head
public:
//Constructor initializes the head pointer to NULL in order to establish an empty linked list.
EmailListClass()
{head = NULL;}
//Linked list operations
void appendNode(string, string );
void displayList() const;
};
void EmailListClass::appendNode(string m, string n)
{
ContactNode *newNode; //It points to a new node
ContactNode *nodePtr; //To move through the list
//Allocate new nodes, store information, and the next pointer points to NULL
newNode = new ContactNode;
newNode->name = m;
newNode->emailAddress=n;
newNode->next = NULL;
if (!head)
head = newNode;
else
{
nodePtr=head;
while (nodePtr->next)
nodePtr=nodePtr->next;
nodePtr->next = newNode;
}
}
void EmailListClass::displayList() const
{
ContactNode *nodePtr; // To move though the list
nodePtr = head;
while(nodePtr)
{
cout << nodePtr->name<< endl;
cout << nodePtr->emailAddress<
nodePtr = nodePtr->next;
}
}
//Function main
int main()
{
//Define a EmailListClass object and other variables
EmailListClass list;
string name, email;
char input;
//While loop
while(true){
cout << "Do you want to enter anything else?(y or n)?";
cin >> input;
if( input == 'n' || input == 'N')
{
break;
}
else
{
cout << "Enter Name: ";
cin >> name;
cout << "Enter email address:";
cin >> email;
}
//Append information to the list
list.appendNode(name, email);
//Display the information in the list
list.displayList();
return 0;
}
Explanation / Answer
#include <iostream>
using namespace std;
//EmailListClass declaration
class EmailListClass
{
private:
//Declaration of a structure for the list
struct ContactNode
{
string name; // the name value in this node
string emailAddress; // the email adress value in this node
struct ContactNode *next; //Struct ContactNode pointer to the next node
};
ContactNode *head; //Pointer to the head of the ContactList head
public:
//Constructor initializes the head pointer to NULL in order to establish an empty linked list.
EmailListClass()
{head = NULL;}
//Linked list operations
void appendNode(string, string );
void displayList() const;
};
void EmailListClass::appendNode(string m, string n)
{
ContactNode *newNode; //It points to a new node
ContactNode *nodePtr; //To move through the list
//Allocate new nodes, store information, and the next pointer points to NULL
newNode = new ContactNode;
newNode->name = m;
newNode->emailAddress=n;
newNode->next = NULL;
if (!head)
head = newNode;
else
{
nodePtr=head;
while (nodePtr->next)
nodePtr=nodePtr->next;
nodePtr->next = newNode;
}
}
void EmailListClass::displayList() const
{
ContactNode *nodePtr; // To move though the list
nodePtr = head;
while(nodePtr)
{
cout << nodePtr->name<< endl;
cout << nodePtr->emailAddress<<endl;
nodePtr = nodePtr->next;
}
}
//Function main
int main()
{
//Define a EmailListClass object and other variables
EmailListClass list;
string name, email;
char input;
//While loop
while(true){
cout << "Do you want to enter anything else?(y or n)?";
cin >> input;
if( input == 'n' || input == 'N')
{
break;
}
else
{
cout << "Enter Name: ";
cin >> name;
cout << "Enter email address:";
cin >> email;
}
//Append information to the list
list.appendNode(name, email);
//Display the information in the list
list.displayList();
return 0;
}
Compilation error
stdin
Standard input is empty
compilation info
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.