Code:
I have added in a while statement so the user can keep adding data until they want to stop. Sadly its a never ending loop. I added { to the if else statements because I seen they were missing. Still do not think it worked because now it just says "you did not enter a correct value" over and over until I quit the program.
Code:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
void userPrompt();
void accessPrompt(char);
void addContact();
void deleteContact();
void CountContact();
vector<string> contacts;
void userPrompt()
{
char prompt;
cout<<"Enter [a] to add a contact, [d] to delete a contact, [c] to count the contacts and to [e]Exit"<< endl;
cin >> prompt;
cin.ignore();
accessPrompt(prompt);
}
void accessPrompt(char user_request)
{
user_request = '\0';
bool running = true;
while (running == true) {
if (user_request == 'a' || user_request == 'A')
{
addContact();
}
else if (user_request == 'd'|| user_request == 'D')
{
deleteContact();
}
else if (user_request == 'c' || user_request == 'C')
{
CountContact();
}
else if (user_request == 'e' || user_request == 'E')
{
running = false;
}
else
cout<<"You did not enter a correct value"<<endl;
}
}
void addContact()
{
string contact;
cout<<"Add the name of your contact below"<<endl;
getline(cin, contact);
contacts.push_back(contact);
}
void deleteContact()
{
int position;
cout<<"Where is the contect located that you want to delete?"<<endl;
cin>>position;
cin.ignore();
contacts.erase(contacts.begin() + position); //contacts.begin=0 so position will find the contact
}
void CountContact()
{
cout<<"You have"<<contacts.size() << " contact(s)."<<endl;
for (size_t i = 0; i<contacts.size(); i++) {
cout<< i <<". "<< contacts.at(i) <<endl; //just another way to way to write contacts[i]
}
}
int main(int argc, const char * argv[])
{
userPrompt();
return 0;
}