Contact List Program: Can't output all contacts
Mar 15, 2015 at 2:53am UTC
I have a program below that is suppose to allow for a user to import an existing contact list if they have one or create a new one. This works however now I want the user to be able to have the option to see all of their contacts in their list however when I made the function to do so, it won't output anything for some reason.
Also, I have other options for the user to select however I have not made these yet so try and not comment on those bits because I have not created them yet.
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
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
void addContactList(string filename);
void createContactList(string name);
void options(fstream& filename);
void addContact(fstream& contactlist);
void listContacts(fstream& filename);
int main() {
fstream file;
string filename;
cout << "Enter name of file that you want to open: " << endl;
cin >> filename;
file.open(filename, std::fstream::in | std::fstream::out | std::fstream::app);
if (file.good()) {
options(file);
} else {
createContactList(filename);
options(file);
}
}
void addContact(fstream& contactlist) {
string name;
string email;
long int phone;
cout << "Enter a name: " << endl;
cin >> name;
contactlist << name;
cout << "Enter an email: " << endl;
cin >> email;
contactlist << email;
cout << "Enter a phone number: " << endl;
cin >> phone;
contactlist << phone;
contactlist << "\n" ;
}
void deleteContact() {
string email;
cout << "Enter email of who you want to delete: " << endl;
}
void searchContact() {
}
void listContacts(fstream& filename) {
string output;
getline(filename, output, '\n' );
cout << output;
}
void createContactList(string name) {
fstream newList;
newList.open(name, std::fstream:: in | std::fstream:: out | std::fstream:: app);
}
void options(fstream& filename) {
int selection;
cout << "Select an option (Enter number): " << endl;
cout << "1. Add a Contact" << endl;
cout << "2. Delete a Contact" << endl;
cout << "3. List all Contacts" << endl;
cout << "4. Search for Contact" << endl;
cout << "5. Exit" << endl;
cout << "\n" ;
cin >> selection;
if (selection == 1) {
addContact(filename);
} else if (selection == 2) {
} else if (selection == 3) {
listContacts(filename);
} else if (selection == 4) {
} else if (selection == 5) {
} else {
cout << "Please select a valid option" << endl;
}
}
Mar 15, 2015 at 4:56am UTC
Um? You never call the listContacts(file);
function in your main. How will it know what to do?
Last edited on Mar 15, 2015 at 4:56am UTC
Mar 15, 2015 at 4:17pm UTC
I call the options();
function in main which then has listContacts(filename);
in it.
Mar 15, 2015 at 4:35pm UTC
Works fine for me except that only the first line of the file is read.
This is because the function
listContacts
is implemented to read just the first line.
Made some small changes though
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
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
void addContactList(string filename);
void createContactList(string name);
void options(fstream& filename);
void addContact(fstream& contactlist);
void listContacts(fstream& filename);
int main() {
fstream file;
string filename;
cout << "Enter name of file that you want to open: " << endl;
getline(cin,filename);
file.open(filename.c_str());
if (file.good()) {
options(file);
} else {
createContactList(filename);
options(file);
}
}
void addContact(fstream& contactlist) {
string name;
string email;
long int phone;
cout << "Enter a name: " << endl;
cin >> name;
contactlist << name;
cout << "Enter an email: " << endl;
cin >> email;
contactlist << email;
cout << "Enter a phone number: " << endl;
cin >> phone;
contactlist << phone;
contactlist << "\n" ;
}
void deleteContact() {
string email;
cout << "Enter email of who you want to delete: " << endl;
}
void searchContact() {
}
void listContacts(fstream& filename) {
string output;
getline(filename, output, '\n' );
cout << output;
}
void createContactList(string name) {
fstream newList;
newList.open(name.c_str());
}
void options(fstream& filename) {
int selection;
cout << "Select an option (Enter number): " << endl;
cout << "1. Add a Contact" << endl;
cout << "2. Delete a Contact" << endl;
cout << "3. List all Contacts" << endl;
cout << "4. Search for Contact" << endl;
cout << "5. Exit" << endl;
cout << "\n" ;
cin >> selection;
if (selection == 1) {
addContact(filename);
} else if (selection == 2) {
} else if (selection == 3) {
listContacts(filename);
} else if (selection == 4) {
} else if (selection == 5) {
} else {
cout << "Please select a valid option" << endl;
}
}
numbers.txt
OUTPUT
Topic archived. No new replies allowed.