I need help C++ program into C. Thank you for the help Please keep the classes t
ID: 3821667 • Letter: I
Question
I need help C++ program into C.
Thank you for the help
Please keep the classes the same and change the cpp classes into c
Contacts.cpp
#include <iostream>
#include "Contacts.h"
using namespace std;
ContactNode::ContactNode(){
nextNodePtr=NULL;
}
ContactNode::ContactNode(string name, string phoneNum){
contactName=name;
contactPhoneNum=phoneNum;
}
void ContactNode::InsertAfter(ContactNode *nextNode){
ContactNode *temp;
if(nextNodePtr==NULL)
nextNodePtr=nextNode;
else{
temp=nextNodePtr;
while(temp!=NULL){
temp=temp->GetNext();
}
temp=nextNode;
}
}
string ContactNode::GetName(){
return contactName;
}
string ContactNode::GetPhoneNumber(){
return contactPhoneNum;
}
ContactNode* ContactNode::GetNext(){
return nextNodePtr;
}
void ContactNode::PrintContactNode(){
ContactNode *temp;
cout<<"Contact Name: "<<GetName()<<endl;
cout<<"Phone number: "<<GetPhoneNumber()<<endl<<endl;
if(nextNodePtr!=NULL)
GetNext()->PrintContactNode();
}
Contacts.h
#ifndef Contact_H
#define Contact_H
#include<string>
using namespace std;
class ContactNode{
public:
ContactNode(); //constructor
ContactNode(string name, string phone);
void InsertAfter(ContactNode*);
string GetName();
string GetPhoneNumber();
ContactNode* GetNext();
void PrintContactNode();
private:
string contactName;
string contactPhoneNum;
ContactNode* nextNodePtr;
};
#endif
Main.cpp
#include <iostream>
#include "Contacts.cpp"
using namespace std;
int main(){
ContactNode contactList;
string name;
string phoneNum;
for(int i=0;i<3;i++){
cout<<"Enter name: ";
cin>>name;
cout<<"Enter phone number: ";
cin>>phoneNum;
contactList.InsertAfter(new ContactNode(name,phoneNum));
}
cout<<endl<<endl<<"CONTACT LIST"<<endl<<endl;
contactList.PrintContactNode();
return 0;
}
Explanation / Answer
The program requested is as follows and is completely matched the requirements and is noted in the same language as requested(C++). Don't forget to read the working of the program below the code. Hope it helps.
// All imports or inclusion of the header files required to run the code
#include <iostream.h>
#include <fstream.h>
#include <string.h>
#include <iomanip.h>
#include <conio.h>
class Contacts{
char name[20],phone_Num[15];
public:
void getInformat();
void showInformat();
char *getname(){ return name; }
char *getphone_Num(){ return phone_Num; }
void update(char *number,char *phn_Number){
strcpy(name,number);
strcpy(phone_Num,phn_Number);
}
};
void Contacts :: getInformat(){
cout<<" Enter Name : ";
cin>>name;
cout<<"Enter Phone No. : ";
cin>>phone_Num;
}
void Contacts :: showInformat(){
cout<<" ";
cout<<setw(20)<<name;
cout<<setw(15)<<phone_Num;
}
void main(){
Contacts user_Record;
fstream db_user_Record;
db_user_Record.open("E:\phone.dat", ios::ate | ios::in | ios::out | ios::binary); //Change as per your resources
char ch,number[20],phn_Number[6];
int ch,traced=0;
while(1){
clrscr();
cout<<" *****Phone Book***** ";
cout<<"1) Add New user_Recordord ";
cout<<"2) Display All user_Recordords ";
cout<<"3) Search Contact Number ";
cout<<"4) Search with Name ";
cout<<"5) Update Entries ";
cout<<"6) Exit ";
cout<<"Choose your ch : ";
cin>>ch;
switch(ch){
case 1 : //New user_Recordord
user_Record.getInformat();
cin.get(ch);
db_user_Record.write((char *) &user_Record, sizeof(user_Record));
break;
case 2 : //Display All user_Recordords
db_user_Record.seekg(0,ios::beg);
cout<<" user_Recordords in Phone Book ";
while(db_user_Record){
db_user_Record.read((char *) &user_Record, sizeof(user_Record));
if(!db_user_Record.eof())
user_Record.showInformat();
}
db_user_Record.clear();
getch();
break;
case 3 : //Search Phone Number with the person name known
cout<<" Enter Name : ";
cin>>number;
db_user_Record.seekg(0,ios::beg);
traced=0;
while(db_user_Record.read((char *) &user_Record, sizeof(user_Record)))
{
if(strcmp(number,user_Record.getname())==0)
{
traced=1;
user_Record.showInformat();
}
}
db_user_Record.clear();
if(traced==0)
cout<<" ---user_Recordord Not traced--- ";
getch();
break;
case 4 : //Search The name with the help of the Telephone number
cout<<" Enter Telephone No : ";
cin>>phn_Number;
db_user_Record.seekg(0,ios::beg);
traced=0;
while(db_user_Record.read((char *) &user_Record, sizeof(user_Record)))
{
if(strcmp(phn_Number,user_Record.getphone_Num())==0)
{
traced=1;
user_Record.showInformat();
}
}
db_user_Record.clear();
if(traced==0)
cout<<" ---user_Recordord Not traced--- ";
getch();
break;
case 5 : //Update Telephone Number.
cout<<" Enter Name : ";
cin>>number;
db_user_Record.seekg(0,ios::beg);
traced=0;
int cnt=0;
while(db_user_Record.read((char *) &user_Record, sizeof(user_Record)))
{
cnt++;
if(strcmp(number,user_Record.getname())==0)
{
traced=1;
break;
}
}
db_user_Record.clear();
if(traced==0)
cout<<" ---user_Recordord Not traced--- ";
else
{
int location = (cnt-1) * sizeof(user_Record);
cin.get(ch);
if(db_user_Record.eof())
db_user_Record.clear();
cout<<"Enter New Telephone No : ";
cin>>phn_Number;
db_user_Record.seekp(location);
user_Record.update(number,phn_Number);
db_user_Record.write((char *) &user_Record, sizeof(user_Record));
db_user_Record.flush();
}
break;
case 6 : gotoout;
}
}
out:
db_user_Record.close();
}
Working Of The Program
1) Inserts person information(Name and Number)
As you can see in the above post it requires the name of the person and the telephone number of the person to enter them in the file and it is the first choice in the given above program.
2) Displays all records from a saved File
The records are been displayed properly with the help of the iterative methods and the logic behind it is very simple and understandable.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.