Need some help with a CC+ program I cannot seem out get the output printing corr
ID: 3812269 • Letter: N
Question
Need some help with a CC+ program I cannot seem out get the output printing correctly for what is need.
Here is my current error.
It just is not printing the output correctly
Here is my code.
Contacts.h
#ifndef Contact_H
#define Contact_H
#include<string>
using namespace std;
class ContactNode{
public:
ContactNode();
ContactNode(string name, string phone);
void InsertAfter(ContactNode*);
string GetName();
string GetPhoneNumber();
ContactNode* GetNext();
void PrintContactNode();
private:
string contactName;
string contactPhoneNum;
ContactNode* nextNodePtr;
};
#endif
Contacts.cpp
#include <iostream>
#include "Contacts.h"
using namespace std;
ContactNode::ContactNode(){
nextNodePtr=NULL;
}
ContactNode::ContactNode(string name, string phoneNum){
contactName=name;
contactPhoneNum=phoneNum;
nextNodePtr=NULL;
}
void ContactNode::InsertAfter(ContactNode *nextNode){
ContactNode *temp;
if(nextNodePtr==NULL)
nextNodePtr=nextNode;
else{
temp=nextNodePtr;
while(temp->nextNodePtr!=NULL){
temp=temp->GetNext();
}
temp->nextNodePtr=nextNode;
}
}
string ContactNode::GetName(){
return contactName;
}
string ContactNode::GetPhoneNumber(){
return contactPhoneNum;
}
ContactNode* ContactNode::GetNext(){
return nextNodePtr;
}
void ContactNode::PrintContactNode(){
ContactNode *temp;
if(nextNodePtr!=NULL) {
cout<<"Contact Name: "<<nextNodePtr->GetName()<<endl;
cout<<"Phone number: "<<nextNodePtr->GetPhoneNumber()<<endl<<endl;
GetNext()->PrintContactNode();
}
}
Main.cpp
#include <iostream>
#include "Contacts.h"
using namespace std;
int main()
{
ContactNode *List,*head;
string Name;
string PhoneNum;
List=new ContactNode;
for(int i=0;i<3;i++){
cout<<" Person " <<i+1<<endl;
cout<<"Enter name: "<<endl ;
cin>>Name;
cout<<"Enter phone number: "<<endl ;
cin>>PhoneNum ;
cout<<"You entered: "<<Name<<", "<< PhoneNum<<endl ;
List->InsertAfter(new ContactNode(Name,PhoneNum));
}
cout<<endl<<endl<<"CONTACT LIST"<<endl<<endl;
List->PrintContactNode();
system("pause");
return 0;
}
Here is the full assignment details if there is any futher questions
This program needs to be completed in C++.
(1) Create three files to submit.
Contacts.h - Class declaration
Contacts.cpp - Class definition
main.cpp - main() function
(2) Build the ContactNode class per the following specifications:
Parameterized constructor. Parameters are name followed by phone number.
Public member functions
InsertAfter() (2 pts)
GetName() - Accessor (1 pt)
GetPhoneNumber - Accessor (1 pt)
GetNext() - Accessor (1 pt)
PrintContactNode()
Private data members
string contactName
string contactPhoneNum
ContactNode* nextNodePtr
Ex. of PrintContactNode() output:
(3) In main(), prompt the user for three contacts and output the user's input. Create three ContactNodes and use the nodes to build a linked list. (2 pts)
Ex:
(4) Output the linked list. (2 pts)
Ex:
Explanation / Answer
/*Please update ur main.cpp others are okay.*/
#include <iostream>
#include "Contacts.h"
using namespace std;
int main()
{
ContactNode *List,*head;
string Name;
string PhoneNum;
List=new ContactNode;
for(int i=0;i<3;i++){
cout<<" Person " <<i+1<<endl;
cout<<"Enter name: "<<endl ;
std::getline (std::cin,Name);
cout<<"Enter phone number: "<<endl ;
std::getline (std::cin,PhoneNum);
cout<<"You entered: "<<Name<<", "<< PhoneNum<<endl ;
List->InsertAfter(new ContactNode(Name,PhoneNum));
}
cout<<endl<<endl<<"CONTACT LIST"<<endl<<endl;
List->PrintContactNode();
cout<<"pause";
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.