1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
|
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: main.cpp
* Author: Josh
*
* Created on February 5, 2018, 5:20 PM
*/
#include <cstdlib>
#include <iostream>
#include <string>
#include <iomanip>
#include <string>
using namespace std;
// Function Predeclarations
void menu();
void inputInfo(int i, string fname[3], string lname[3], string phoneNumber[3]);
void sort(string fname[3], string lname[3], string phoneNumber[3], string ascending);
void search(int i, string searchUser, string fname[3], string phoneNumber[3]);
void print(int i, string lname[3], string fname[3], string phoneNumber[3]);
int main()
{
string fname[3];
string lname[3];
string phoneNumber[3];
string ascending;
int reply;
string searchUser;
int i;
do//this loop is set up to have the menu open after each function is completed
{
menu();
cin >> reply;
switch(reply)//set up menu with switch case using characters entered by user
{
case 'a':inputInfo(i, fname, lname, phoneNumber);
break;
case 'b':ascending = "y";sort(fname, lname, phoneNumber, ascending);
break;
case 'c':ascending = "n";sort(fname, lname, phoneNumber, ascending);//sorts arrays in descending order
break;
case 'd':print(i, fname, lname, phoneNumber);//prints arrays in their current state
break;
case 'e':search(i, searchUser, fname, phoneNumber);
break;
}
}
while (reply != 'f');
cout << "Goodbye" << endl;
return 0;
}
// Function Implementations
void menu()//prompts user to make a selection
{
cout << "Welcome to your Contact Manager. Please select an option" << endl;
cout << "a. Input Data" << endl;
cout << "b. Sort Data Ascending" << endl;
cout << "c. Sort Data Descending" << endl;
cout << "d. Print All Data" << endl;
cout << "e. Search for an individual in the data" << endl;
cout << "f. End Program" << endl;
}
void inputInfo(int i, string fname[3], string lname[3], string phoneNumber[3])
{
{
for (i = 0; i < 2; i++)
cout << "\nEnter Person's Name. Then press enter: ";
cin >> fname[i]; // store the input directly in the array
cout << "/nEnter Person's Last Name. Then press enter: ";
cin >> lname[i];
cout << "Enter Phone Number. Then press enter: "; // instead of using a dummy
cin >> phoneNumber[i];
}
}
void sort(string fname[3], string lname[3], string phoneNumber[3], string ascending)
{
string ptemp = "";
string fntemp = "";
string lntemp = "";
int pass;
int index;
if(ascending == "y")
{
for (pass = 0; pass < 3; pass++)//create outer loop to count the number of passes
{
for (index = 0; index < 3 - pass; index++)//inner loop to iterate 10 times to account for all values
{
if (phoneNumber[index + 1] < phoneNumber[index])//if any value is moved the other two will also move inside their arrays
{
ptemp = phoneNumber[index];
phoneNumber[index] = phoneNumber[index + 1];
phoneNumber[index + 1] = ptemp;
lntemp = lname[index];
lname[index] = lname[index + 1];
lname[index + 1] = lntemp;
fntemp = fname[index];
fname[index] = fname[index + 1];
fname[index + 1] = fntemp;
}
}
}
int index = 0;//prints out list after sorting
cout << left << setw(12) << "Phone Number" << setw(5) << " " << setw(20) << "Last Name" << right << setw(20) << "First Name" << endl;
for(index = 0; index < 3; index++)
{
cout << left << setw(12) << phoneNumber[index] << setw(5) << " " << setw(20) << fname[index] << right << setw(20) << lname[index] << endl;
}
}
// Write the code descending order below:
}
void search(int i, string searchUser, string fname[3], string phoneNumber[3])
{
cout << "\nPlease enter the first name of the person you want to search for"
<< " and then press enter:\n";
cin >> searchUser;
for(i = 0; i < 3; i++)
{
if (fname[i] == searchUser)
{
break;
}
}
if(i > 9)
{
cout << "\n" << searchUser << " was not found." << endl;
}
else
{
cout << "\n" << searchUser << " was found in the phone book! " << searchUser << "'s number is "
<< phoneNumber[i] << "." << endl;
}
}
void print(int i, string lname[3], string fname[3], string phoneNumber[3])
{
for (i=0; i<=2; i++)
{
cout << lname[i] << "'s number is: " << phoneNumber[i] <<endl;
}
cout << endl;
}
|