Phonebook trouble

Hello, I am a very big beginner and I am trying to make a phonebook that can add many entries, search through all the entries, and print all of the entries out. I know there is a lot wrong with my code. All it does as of now is make one entry in the phonebook and if I try to make a new one it will just overwrite the old one. The print out all contacts function is totally broken. I want to use getline some how but I have now idea how. I don't really know where to start on the search function. Any help would be appreciated, thanks.

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
 #include<vector>
#include<iostream>
#include<fstream>

using namespace std;

ifstream myfile;
ofstream mefile;

struct Person //makes a struct person
{
string name; //intializes the name
string address; //address
string email;//email
string number;//number
};

int i=0; //makes i start as 0


int choice;

char Keyword;

int main(){
	vector<Person> people; //makes a vector people
	 
	while(true){ //repeats the program
		cout << "Welcome to the phonebook" <<endl; //main screen choices
		cout << "1. Show contacts" << endl;
        cout << "2. Quit" << endl;
        cout << "3. Add Contact" << endl;
        //cout << "4. Search" << endl;
        cin >> choice;

if (choice == 1){//if choice is 1 then do this
		 myfile.open("Contacts.txt");//opens file in a reading format
			
//getline something
			myfile.close();
}

else if (choice == 2)
{
	return 0;//ends the program
}


else if (choice == 3)//if choice =3

	i = i + 1;//makes vector bigger
	
	people.push_back(Person()); //puts the people in a vector
	
	cout << endl << "Enter name";
 	cin >> people[i].name;
	 

	cout << endl << "Enter address";
	cin >> people[i].address;
	
	
	cout << endl << "Enter email";
	cin >> people[i].email;
	
	
	cout <<endl << "Enter number";
	cin >> people[i].number;


	
	mefile.open("Phonebook.txt");//opens file in a writing format
	if(mefile.is_open())
	{	
	mefile <<"Name: " << people[i].name <<endl;

	mefile << "Address: "<< people[i].address <<endl;

	mefile << "Email: " << people[i].email <<endl;

	mefile << "Number: " << people[i].number <<endl;

	mefile.close();

}
	

}	
}
For choice 3, you should declare a Person object first. Then fill up its fields using cin. After that, simply push it into the vector.

Line 18 and 51 are not needed. The vector size will automatically increase by one when you push onto it. No need to use the bracket operator [] in choice 3.

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
// PBachmann's help to USAHAL
// Phonebook
// 21 May 2016
#include <iostream>
#include <fstream>
#include <string>
#include <vector>

using namespace std;

struct Person{ // Makes a struct person
    string name; // Intializes the name
    string address; // Address
    string email; // email
    string number; // Number
};

/* Reads myinfile and copies the contents to people[].
 * This assumes contacs are written in the file like this:
 *
 *      Carl Snow, 182 High House Rd., carrSnow@gmail.com, 846-356-3548
 *      David Baker, 823 Elevator Cir., Baker221@yahoo.com, 655-789-1673
 *      Sapphire Peterson, 224 Apex Dr., SPeterson237@nc.rr.com, 901-779-9777
 *
 * etc. etc.
 */
void initializeVector(ifstream &myinfile, vector<Person> &people){
    Person person1; // Used to temporarily store contact info
    string space; // Used to store the extra space
    if (myinfile.is_open()){ // Opens file
        while (myinfile.good()){ // Checks for various things. For info look up "ios::good"
            getline(myinfile, person1.name, ','); // Reads until a comma
            getline(myinfile, space, ' '); // Reads the extra space and ignores it
            getline(myinfile, person1.address, ','); // Reads until the next comma
            getline(myinfile, space, ' '); // Reads the extra space and ignores it
            getline(myinfile, person1.email, ','); // Reads until the next comma
            getline(myinfile, space, ' '); // Reads the extra space and ignores it
            getline(myinfile, person1.number, '\n'); // Reads until the end of the line
            people.push_back(person1); // Adds the contact to people[]
        }
    }
    else{ // For when myinfile.is_open returns an error
        cout << "Unable to open.\n";
    }
}

// Main screen choices
void input(int &choice){
    cout << "Welcome to the phonebook." << endl;
    cout << "1. Show contacts" << endl;
    cout << "2. Add contact" << endl;
    cout << "3. Save contacts and quit" << endl;
    //cout << "4. Search" << endl;
    cin >> choice;
    cout << endl;
}

// Prints the contacts in a table
void showContacts(vector<Person> &people){
    cout << "   Name               Address                 email              Number" << endl;
    for(unsigned i = 0; i < people.size(); i++){
        cout << people[i].name << "    ";
        cout << people[i].address << "    ";
        cout << people[i].email << "    ";
        cout << people[i].number << endl;
    }
    cout << endl;
}

// Asks user to enter in a new contact
void addContact(ofstream &myoutfile, vector<Person> &people){
    Person person1; // Used to temporarily store contact info
    cin.ignore(); // Without this line, the user's menu choice number will be read as the contact name

    cout << "Enter name:" << endl;
    getline(cin, person1.name);
    // Using getline allows you to enter a first name and last name (space included)

    cout << "Enter address:" << endl;
    getline(cin, person1.address);


    cout << "Enter email:" << endl;
    getline(cin, person1.email);


    cout << "Enter number:" << endl;
    getline(cin, person1.number);

    people.push_back(person1); // Adds person1 to the end of people[]
    cout << endl;
}

// Saves current contacts in Phonebook.txt
void saveContacts(ofstream &myoutfile, vector<Person> &people){
    if (myoutfile.is_open()){ // Opens file
        for(unsigned i = 0; i < people.size(); i++){
            if (i > 0) myoutfile << endl; // Starts by writing a line return
            myoutfile << people[i].name << ", "; // Writes name then a comma
            myoutfile << people[i].address << ", "; // Writes address then a comma
            myoutfile << people[i].email << ", "; // Writes email then a commae
            myoutfile << people[i].number; // Writes number
        }
        cout << "Contacts saved.\n";
    }
    else{ // For when myoutfile.is_open returns an error
        cout << "Unable to open.\n";
    }
    cout << endl;
}

int main(){
    int choice; // The user's menu choice
    vector<Person> people; // Makes a Person-type vector called people
    ifstream myinfile("Contacts.txt"); // Input file
    ofstream myoutfile("Phonebook.txt"); // Outputfile

    initializeVector(myinfile, people); // Reads Contacts.txt and stores it in person[]

    do{ //repeats the loop until user inputs 3
        input(choice); // Menu
        switch(choice){ // switch is a version of if/then statements. Look up "switch" for more info.
            case 1: showContacts(people); break; // Show contacts
            case 2: addContact(myoutfile, people); break; // Adds new contact to people[]
            case 3: saveContacts(myoutfile, people); break; // Saves contacts to Phonebook.txt
            default: break;
        }
        if (choice < 1 || choice > 3){
            cout << "Invalid input.\n\n";
        }
    } while(choice != 3);

    return 0;
}
Last edited on
Topic archived. No new replies allowed.