Phonebook Project that writes and reads from text files using arrays

Hi I'm new here and not so adept at C++ myself. My problem is when I try to show the contents of my text files after I add a contact it wont show me anything. Can anyone point out the problem?


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
#include <iostream>
#include <conio.h>
#include <string>
#include <windows.h>
#include <fstream>
#include <cstdlib>
using namespace std;


//prototypes
void printline(char, int);



fstream file;


class contact
{
    string name;
    string mob;
    string line;

    public:


        // Shows all contacts
        bool show()
        {
            while(getline(file, line))
            {
                 cout << line + "\n";
            }
        }


        //The contact object is initialized by valid values
        bool add(string new_name, string new_mob)
        {
            if(name=="")
            {
                name = new_name;
                mob = new_mob;
                file << name + "   ";
                file << mob + "\n";
                return 1;
            }

        }


};

int main()
{

    contact person[100];
    file.open("MyContacts.txt");
    int choice, i, counter;
    bool flag;
    bool cancel_flag;
    string temp_name, temp_mob;

    cout << "**** PHONEBOOK ******" << endl;


    do
    {
        cout << "\n\n\n";
        printline('-', 20);
        cout << "0. Show contacts" << endl
        << "1. Add Contact" << endl
        << "2. Quit" << endl << endl
        << "Your choice...";
        cin >> choice;

        system("cls");
        printline('-', 20);
        cancel_flag = 0;

        switch(choice)
        {
            case 0:
                cout << "Showing Contacts" << endl;
                printline('-', 20);

                for(i=0; i<100; i++)
                    if(person[i].show())
                        flag = 1;


                //if(!flag)
                    //cout << "No contacts found!" << endl;
                break;

            case 1:
                cout << "Add New Contact\t\t\t\tpress $ to cancel" << endl;
                printline('-', 20);
                counter = 0;




                    cout << "Name: "; cin >> temp_name;

                    //Cancel operation
                    if(temp_name=="$")
                    {
                        cancel_flag = 1;
                        break;
                    }
                    cout << "Mobile No.: "; cin >> temp_mob;

                    //Cancel operation
                    if(temp_mob=="$")
                    {
                        cancel_flag = 1;
                        break;
                    }


                if(cancel_flag)
                {
                    system("cls");
                    break;
                }


                //This code adds the contact to phonebook
                for(i=0; i<100; i++)
                    if(person[i].add(temp_name, temp_mob))
                    {
                        cout << "Contact added successfully!" << endl;
                        flag = 1;
                        break;
                    }

                if(!flag)
                    cout << "Memory full! Delete some contacts first."
					<< endl;
                break;

            case 2:
                return 0;
                break;

        }
    } while(1);

    getch();
    file.close();
    return 0;
}

//prints a line
void printline(char ch, int size)
{
    for(int i=0; i<size; i++)
        cout << ch;
    cout << "\n";
}

Why is file a global variable?

Are you sure your file opens correctly? You should be checking to insure it opens properly.

Also since you never write anything to the file, what do you expect the file to actually contain?


Jim

It's global so I can use it in my bool show() function. After I add a contact and try to show them after, nothing shows. And I'm pretty sure it opens correctly since I have no problem adding contacts in succession, only when showing them.
Last edited on
Where are you writing this "added" contact to the file?

It's global so I can use it in my bool show() function.

That's not a very good reason. You should pass this variable to the function as a parameter.
Last edited on
Topic archived. No new replies allowed.