The project I have is a link list that asks for a name and phone number and it allows the user to insert ,delete ,modify ,and find an element in the structure. The program has a menu in which the user can choose which function they want to use I need to use switch case to do this but it has not worked I need help with this. Thank you!
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
const int maxrecs = 5;
//struct definition
struct Teletype
{
string name;
string phoneNo;
Teletype *nextaddr;
};
//function prototype
void display(Teletype *);
void populate(Teletype *);
void modify(Teletype *head, string name);
void insertAtMid(Teletype *, string, string);
void deleteAtMid(Teletype *, string );
int find(Teletype *, string);
bool is_phone_no(string);
//main function
int main()
{
Teletype *list, *current;
string name;
string phoneNo;
int menu;
//link nodes
list = new Teletype;//new reserves space and returns the adress to
the list
current = list;
for(int i=0; i<maxrecs -1; i++)
{
populate(current);
current->nextaddr = new Teletype;
current = current->nextaddr;
}
populate(current);
current->nextaddr=NULL;
cout<< "The contents of the linked list is:
"<<endl;
display(list);
cout<< "\nPlease select a number from the menu: " <<
endl;
cout<<"(1)Insert new Structure on the linked list."<<
endl;
cout<<"(2)Modify an existing structure in the Linked
list."<< endl;
cout<<"(3)Delete an existing structure in the Linked
list."<< endl;
cout<<"(4)Find an existing Structure from the Linked
list."<< endl;
cout<<"(5)Exit the program."<< endl;
cin>>menu;
switch(menu)
{
case '1':cout<< "Enter Name and number"<< endl;
cin>> name>>phoneNo;
insertAtMid(list, name , phoneNo);
cout<< "The contents of the link list after inserting:
"<< endl;
display(list);
break;
case'2': cout << "\nName of the person you need to modify:
";
cin >> name;
modify(list, name);
display(list);
break;
case'3':cout<< endl;
cout<< "Enter Name "<< endl;
cin>> name;
deleteAtMid(list, name);
cout<< endl;
cout<< "The contents of the linked list after deleting John
is: "<<endl;
display(list);
break;
case'4':cout<< endl<<endl;
cout<<"Enter a name (last, first): ";
getline(cin, name);// getline because of the namespace
if(!find(list, name))
{
cout<< "The name is not in the current phone list" <<
endl;
}
break;
case'5':break;
}
return 0;
}
void insertAtMid(Teletype *head, string name, string
phone)
{
Teletype *tmp = new Teletype(),*temp2;
tmp->name = name;
tmp->phoneNo = phone;
tmp->nextaddr = head->nextaddr;
temp2=head->nextaddr;
head->nextaddr = tmp;
tmp->nextaddr=temp2;
}
void deleteAtMid(Teletype *head, string name)
{
while(head->nextaddr != NULL)
{
if(head->nextaddr->name == name)
{
Teletype *del = head->nextaddr;
head->nextaddr = head->nextaddr->nextaddr;
delete del;
break;
}
head = head->nextaddr;
}
}
//implementaion
void display(Teletype *contents)
{
while(contents !=NULL)
{
cout<<endl<<setw(30)<< contents->name <<
setw(20) << contents-> phoneNo;
contents=contents->nextaddr; //get next addr
}
}
int find(Teletype *contents, string name)
{
int found = 0;
while(contents != NULL)
{
if(contents->name == name)
{
found = 1;
cout<< setw(30) <<contents->name
<<setw(20)<<contents->phoneNo;
break; //stop the iterations
}
else
{
contents = contents-> nextaddr;
}
}
cout<< endl;
return found;
}
bool is_phone_no(string phoneNo)
{
for (auto it = phoneNo.begin(); it != phoneNo.end(); it++)
{
if (*it > '9' || *it < '0')
return false;
}
return true;
}
void populate(Teletype *record)
{
cout<<"Enter a name: ";
getline(cin,record->name);
string phoneNo;
while (true)
{
cout << "Enter the phone number: ";
getline(cin, phoneNo);
try
{
if (!is_phone_no(phoneNo))
{
throw "Exception caught! Invalid phone number\n";
}
record->phoneNo = phoneNo;
break;
}
catch (const char *e)
{
cout << e;
}
}
return;
}
//do comment if any problem arises
//modified code has been highlighted
//you have not implemented modify so commented it
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
const int maxrecs = 5;
//struct definition
struct Teletype
{
string name;
string phoneNo;
Teletype *nextaddr;
};
//function prototype
void display(Teletype *);
void populate(Teletype *);
// void modify(Teletype *head, string name);
void insertAtMid(Teletype *, string, string);
void deleteAtMid(Teletype *, string);
int find(Teletype *, string);
bool is_phone_no(string);
//main function
int main()
{
Teletype *list, *current;
string name;
string phoneNo;
int menu;
//link nodes
list = new Teletype; //new reserves space and returns the adress to the list
current = list;
for (int i = 0; i < maxrecs - 1; i++)
{
populate(current);
current->nextaddr = new Teletype;
current = current->nextaddr;
}
populate(current);
current->nextaddr = NULL;
cout << "The contents of the linked list is: " << endl;
display(list);
cout << "\nPlease select a number from the menu: " << endl;
cout << "(1)Insert new Structure on the linked list." << endl;
cout << "(2)Modify an existing structure in the Linked list." << endl;
cout << "(3)Delete an existing structure in the Linked list." << endl;
cout << "(4)Find an existing Structure from the Linked list." << endl;
cout << "(5)Exit the program." << endl;
cin >> menu;
switch (menu)
{
case 1:
cout << "Enter Name and number" << endl;
cin >> name >> phoneNo;
insertAtMid(list, name, phoneNo);
cout << "The contents of the link list after inserting: " << endl;
display(list);
break;
case 2:
cout << "\nName of the person you need to modify: ";
cin >> name;
// modify(list, name);
display(list);
break;
case 3:
cout << endl;
cout << "Enter Name " << endl;
cin >> name;
deleteAtMid(list, name);
cout << endl;
cout << "The contents of the linked list after deleting John is: " << endl;
display(list);
break;
case 4:
cout << endl
<< endl;
cout << "Enter a name (last, first): ";
getline(cin, name); // getline because of the namespace
if (!find(list, name))
{
cout << "The name is not in the current phone list" << endl;
}
break;
case 5:
break;
}
return 0;
}
void insertAtMid(Teletype *head, string name, string phone)
{
Teletype *tmp = new Teletype(), *temp2;
tmp->name = name;
tmp->phoneNo = phone;
tmp->nextaddr = head->nextaddr;
temp2 = head->nextaddr;
head->nextaddr = tmp;
tmp->nextaddr = temp2;
}
void deleteAtMid(Teletype *head, string name)
{
while (head->nextaddr != NULL)
{
if (head->nextaddr->name == name)
{
Teletype *del = head->nextaddr;
head->nextaddr = head->nextaddr->nextaddr;
delete del;
break;
}
head = head->nextaddr;
}
}
//implementaion
void display(Teletype *contents)
{
while (contents != NULL)
{
cout << endl
<< setw(30) << contents->name << setw(20) << contents->phoneNo;
contents = contents->nextaddr; //get next addr
}
}
int find(Teletype *contents, string name)
{
int found = 0;
while (contents != NULL)
{
if (contents->name == name)
{
found = 1;
cout << setw(30) << contents->name << setw(20) << contents->phoneNo;
break; //stop the iterations
}
else
{
contents = contents->nextaddr;
}
}
cout << endl;
return found;
}
bool is_phone_no(string phoneNo)
{
for (auto it = phoneNo.begin(); it != phoneNo.end(); it++)
{
if (*it > '9' || *it < '0')
return false;
}
return true;
}
void populate(Teletype *record)
{
cout << "Enter a name: ";
getline(cin, record->name);
string phoneNo;
while (true)
{
cout << "Enter the phone number: ";
getline(cin, phoneNo);
try
{
if (!is_phone_no(phoneNo))
{
throw "Exception caught! Invalid phone number\n";
}
record->phoneNo = phoneNo;
break;
}
catch (const char *e)
{
cout << e;
}
}
return;
}
The project I have is a link list that asks for a name and phone number...
C++ getline errors I am getting getline is undefined error messages. I would like the variables to remain as strings. Below is my code. #include <iostream> #include<string.h> using namespace std; int index = 0; // variable to hold how many customers are entered struct Address //Structure for the address. { int street; int city; int state; int zipcode; }; // Customer structure struct Customer { string firstNm, lastNm; Address busAddr, homeAddr; }; // Functions int displayMenu(); Customer getCustomer(); void showCustomer(Customer);...
C++ - I have a doubly linked list, but I haven't been able to get the "reverse list" option in the code to work(It's option #in the menu in the program). I received this guidance for testing: Test 4 cases by entering (in this order) c,a,z,k,l,m This tests empty list, head of list, end of list and middle of list. Then delete (in this order) a,z,l. This tests beginning, end and middle deletes. This exhaustively tests for pointer errors. #include...
Fix my code, if I the song or the artist name is not on the vector, I want to user re-enter the correct song or artist name in the list, so no bug found in the program #include <iostream> #include <vector> #include <string> #include <algorithm> using namespace std; class musicList{ private: vector<string> songName; vector<string> artistName; public: void addSong(string sName, string aName){ songName.push_back(sName); artistName.push_back(aName); } void deleteSongName(string sName){ vector<string>::iterator result = find(songName.begin(), songName.end(), sName); if (result == songName.end()){ cout << "The...
Q) Modify the class Linked List below to make it a Doubly Linked List. Name your class DoublyLinkedList. Add a method addEnd to add an integer at the end of the list and a method displayInReverse to print the list backwards. void addEnd(int x): create this method to add x to the end of the list. void displayInReverse(): create this method to display the list elements from the last item to the first one. Create a main() function to test...
1. in this programe i want add some products to be shown after choosing choise ( show all products ) before i add any products. let say 10 products have added to the program then when the user choose (show all products ) all the 10 products appear and the user going to choose one and that one must has its own name, price,and quantity after that the quantity will be reduce. 2. allow the user to buy products. ===========================================================...
i have problem where the address is not save to my file after i input a new address and exit it #include<string> #include<iostream> #include<fstream> #include<sstream> using namespace std; typedef struct date { int day; int month; int year; }Date; typedef struct add { string building; string street; string city; string state; string zip; }Address; typedef struct entry { string firstName; string lastName; Address address; string phNo; Date...
The following is a sample inventory in C++, i want to ask the user to input a item number for removing from inventory. //CPP #include <iostream> #include <fstream> #include <cstdlib> #include <iomanip> #define MAX 1000 using namespace std; //Function to Add a new inventory item to the data into the array in memory void addItem(string desc[],string idNum[], float prices[], int qty[],int &num) { cout<<"Enter the names:"; cin>>desc[num]; cout<<"Enter the item number:"; cin>>idNum[num]; cout<<"Enter the price of item:"; cin>>prices[num]; cout<<"Enter the...
using the source code at the bottom of this page, use the following instructions to make the appropriate modifications to the source code. Serendipity Booksellers Software Development Project— Part 7: A Problem-Solving Exercise For this chapter’s assignment, you are to add a series of arrays to the program. For the time being, these arrays will be used to hold the data in the inventory database. The functions that allow the user to add, change, and delete books in the store’s...
below i added the code, but while running it, makefile.win error
showing. how i fix this?
1.)waitlist.h
#include<iostream>
using namespace std;
class guest{
public:
string name;
guest*next;
guest*prev;
guest(string);
};
class waitList{
int numberOfGuests;
guest* first;
guest* last;
public:
waitList();
void addPerson(string);
void deletePerson(string);
guest* getFirst();
guest* getLast();
};
2.)waitlist.cpp
#include<iostream>
#include"waitlist.h"
using namespace std;
guest::guest(string name){
this->name...
Hi I've a problem with this code. When I add more than 1 song to the list, it doesn't show the first one, only shows the latest one, when I called the function displaysong, let's say when you add 2 songs ID 1 then ID 2, it shows only ID 1. Can you fix it. Everything else is fine.. #include <iostream> #include<string> using namespace std; //class Song class Song{ private: int songID; string title; string artist; string album; int year; public:...