This is a follow up to this question. I have taken the suggestions given to me and implemented them in my code, as well as moving towards adding more features to the program. I am unfortunately required to use a linked list for storing contacts, so I cannot adapt to your feedback for that, but thank you all for helping me gain further knowledge of C++.
#include "stdafx.h"
#include <iostream>
#include <string>
class List
{
private:
struct node
{
std::string firstName;
std::string lastName;
std::string phoneNumber;
node *next;
};
node *head, *tail;
public:
List()
{
head = NULL;
tail = NULL;
}
void create_contact(std::string first, std::string last, std::string cellNumber) // Create node and add it onto the linked list
{
node *temp = new node;
temp->firstName = first;
temp->lastName = last;
temp->phoneNumber = cellNumber;
temp->next = NULL;
if (head == NULL)
{
head = temp;
tail = temp;
temp = NULL;
}
else
{
tail->next = temp;
tail = temp;
}
}
void display_all() const
{
int contactNum = 0;
for (node *curr = head; curr; curr = curr->next)
{
std::cout << "\nContact Number: " << ++contactNum << "\n";
std::cout << "First Name: " << curr->firstName << "\n";
std::cout << "Last Name: " << curr->lastName << "\n";
std::cout << "Phone Number: " << curr->phoneNumber << "\n";
}
}
void display_contact(int number) const
{
int contactNum = number;
for (node *curr = head; curr; curr = curr->next)
{
std::cout << "\nContact Number: " << contactNum << "\n";
std::cout << "First Name: " << curr->firstName << "\n";
std::cout << "Last Name: " << curr->lastName << "\n";
std::cout << "Phone Number: " << curr->phoneNumber << "\n";
}
}
void delete_position(int pos)
{
if (pos < 1 || head == nullptr)
return;
node *victim{ head };
if (pos == 1) { // deleting head
if (tail == victim) {
head = tail = nullptr;
}
else {
head = head->next;
}
}
else {
// deleting non-head node
node *prev{ head };
for (pos -= 2; pos; --pos) {
if (prev == nullptr)
return;
prev = prev->next;
}
victim = prev->next;
prev->next = victim->next;
if (tail == victim) {
tail = prev;
}
}
delete victim;
}
void pause() {
getchar();
}
};
int main()
{
List Contacts; // create a Contacts item for the List class
std::string position;
std::string firstName;
std::string lastName;
std::string phoneNumber;
std::string choice;
int x, converted_position;
while (1) {
std::cout << "\nWhat would you like to do?: " << "\n";
std::cout << "1. Show All Contacts" << "\n";
std::cout << "2. Add A Contact" << "\n";
std::cout << "3. Remove A Contact" << "\n";
std::cout << "4. Edit A Contact" << "\n";
std::cout << "5. Search Contacts (Coming Soon)" << "\n";
std::cout << "6. Exit The Program" << "\n\n";
std::cin >> choice;
try {
x = std::stoi(choice);
}
catch (...) {
x = 0;
}
switch (x)
{
case 1:
std::cout << "\n";
Contacts.display_all(); // display all contacts
std::cout << "\n";
break;
case 2:
std::cout << "\nEnter the contacts' first name: ";
std::cin >> firstName;
std::cout << "\nEnter the contacts' last name: ";
std::cin >> lastName;
std::cout << "\nEnter the contacts' phone number: ";
std::cin >> phoneNumber;
Contacts.create_contact(firstName, lastName, phoneNumber); // create the contact in the linked list
std::cout << "\n";
break;
case 3:
std::cout << "Enter the contact number of the contact you would like to remove: ";
std::cin >> position;
try {
converted_position = std::stoi(position);
}
catch (...) {
converted_position = 0;
}
Contacts.delete_position(converted_position); // delete contact from list
break;
case 4:
std::cout << "Enter the contact number of the contact you would like to edit: ";
std::cin >> position;
try {
converted_position = std::stoi(position);
}
catch (...) {
converted_position = 0;
}
Contacts.display_contact(converted_position);
std::cout << "\nEditing contacts is coming soon." << "\n";
break;
case 5:
std::cout << "\nSearching contacts is coming soon." << "\n";
break;
case 6:
exit(1); // exit the program with exit code 1
break;
default:
std::cout << "\n" << choice << " is not an option. Please select a valid option." << "\n";
break;
}
}
Contacts.pause();
return 0;
}