C++ program: Write an address book program that will accomplish the following: 1
ID: 3686734 • Letter: C
Question
C++ program:
Write an address book program that will accomplish the following:
1. Read name and address data from the user from the keyboard.
2. While reading the names and addresses, put the names into an appropriate data structure.
3. After reading names and addresses, allow user to search for names and change the names or addresses in the container data structure.
4. Allow user to write out the container data structure to a comma delimited file.
The input file will have the following form, 1 line for the name the next line for the address:
name
address
name
address
Use features of c++ such as classes, and dynamic data structures. You may not use the STL data structures. The program will also be evaluated regarding readability and structure (functions, and methods).
Explanation / Answer
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
//structure defined
struct Student {
string name;
string address;
Student *next;
};
Student* insertStudent(string name,string address,Student *start)
{
Student* temp=new Student;
temp->name=name;
temp->address=address;
temp->next=NULL;
if(start==NULL)
{
start=temp;
start->next=NULL;
return start;
}
else
{
Student *s = start;
while (s->next != NULL)
{
s = s->next;
}
s->next = temp;
}
cout << " Information has been saved successfully.";
}
void displayStudent(string name,Student * start)
{
Student *s = start;
while (s!= NULL)
{
if(name.compare(s->name)==0)
{
cout << " " << s->name;
cout << " " << s->address;
break;
}
s=s->next;
}
}
bool searchStudent(string name,Student * start)
{
bool flag=false;
Student *s = start;
while (s!= NULL)
{
if(name.compare(s->name)==0)
{
flag=true;
break;
}
s=s->next;
}
return flag;
}
int main() {
//declared array of structs
int choice;
Student * start=NULL;
while (true) {
cout<< " 1- Enter a new student. 2- search a student. 3-display student 4-QUIT ";
cin>>choice;
if(choice==1)
{
cout << " Please enter student information: ";
string name, address;
//reading data from user
cout << " Enter name: ";
cin >> name;
cout << " Address: ";
cin >> address;
//inserting student data in linked list
start=insertStudent(name, address,start);
} else if (choice == 2) {
string name;
cout << " Enter name: ";
cin >> name;
//search the linked list for student name
bool flag = searchStudent(name,start);
if(flag==true)
{//if student name available
cout<<" Your Details:";
displayStudent(name,start);
}
else
cout<< " Sorry, we cant find your name in the database, please check ";
cout<<" the spelling or contact the student affair in CoC.";
} else if (choice == 3) { exit(0);
} }
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.