Problem reading from csv file after writing to it in c++

Hello guys,
I'm quite new to C++, I'm trying to achieve a task whereby i could read from a csv file and write to that same file and vice versa depending on the user's selection. This is what i did:

Here's the data already on file
1,Mickey,23,090,Girne,TRNC,465
2,Charles,23,090,Girne,TRNC,465
3,Species,23,090,Girne,TRNC,465
4,Moody,23,090,Girne,TRNC,465
5,Kpokiyo,23,090,Girne,TRNC,465
6,Sualp,23,090,Girne,TRNC,465

Here's the code i wrote so far

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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include <iostream>
#include <sstream>
#include <fstream>
#include <vector>
#include <string>

    using namespace std; 
    
    bool checkIsFile();
    void addUser(string e[][7], int num);
    void readAllUsers(string x[][7]);
    void breakArray(string x[], string y[][7], int num);
    bool checkAccout(string x[], int y, string search);
    void findUser(string x[], string y[],string z[], string a[],string t[], 
    string c[], string d[]);
    int findRowNumber();
    
    int main()
    {
        int option;
        int acc;
        string emp[findRowNumber()][7];
        string account[findRowNumber()], age[findRowNumber()],name[findRowNumber()], 
        state[findRowNumber()], city[findRowNumber()], phone[findRowNumber()],
        zip[findRowNumber()];
        
      //infinite loop
          while(true){
          cout<<"-------------------------\n";
          cout<<"Menu Options\n";
          cout<<"1 Add account\n";
          cout<<"2 List Accounts\n";
          cout<<"3 Delete account\n";
          cout<<"4 Find user\n";
          cout<<"5 Exit program\n";
          cout<<"-------------------------\n";
          cin>>option;
          
              if(option==1)
              {
                 readAllUsers(emp);
                 addUser(emp, findRowNumber());
              }
              else if(option==2)
              {
                  readAllUsers(emp);
              }
              else if(option==3)
              {
                   //delete a user;
              }
              else if(option==4)
              {
                    //find user
                    readAllUsers(emp);
                    breakArray(account,emp,0);
                    breakArray(name,emp,1);
                    breakArray(age,emp,2);
                    breakArray(phone,emp,3);
                    breakArray(city,emp,4);
                    breakArray(state,emp,5);
                    breakArray(zip,emp,6);
                    
                    findUser(account,name,age,phone,city,state,zip);
              }
              else if(option==5)
              {
                    return 0;
              }
              else
              {
                    cout<<"Invalid option\n";
              }
          }
        }
    
    void addUser(string e[][7], int num)
    {
         string emp[num + 1][7];
         
         for(int a=0;a<num;a++)
         {
            for(int b=0;b<7;b++)
            {
                 emp[a][b] = e[a][b];
                 }
                 }
        //just to test if it works 
         emp[num][0] = "10";
         emp[num][1] = "10";
         emp[num][2] = "10";
         emp[num][3] = "10";
         emp[num][4] = "10";
         emp[num][5] = "10";
         emp[num][6] = "10";

         ofstream inFile;
         inFile.open("info.csv");
         
         for(int a=0;a<num + 1;a++)
         {
            for(int b=0;b<7;b++)
            {
                 cout<<emp[a][b];               
            }
        }
        
        for(int i=0;i<num +1; i++)
        {
             inFile << emp[i][0] + "," + emp[i][1] + "," + emp[i][2] + "," + emp[i][3] + "," + emp[i][4] + "," + emp[i][5] + "," + emp[i][6]<<endl;                 
        }
        
        inFile.close();
    }
    
    void readAllUsers(string x[][7])
    {
        ifstream inFile("info.csv");
        string line;
        int linenum = 0;
        while (getline (inFile, line))
        {
            istringstream linestream(line);
            string item;
            int itemnum = 0;
            while (getline (linestream, item, ','))
            {
                x[linenum][itemnum] = item;
                itemnum++;
            }
        linenum++;
        }
        inFile.close();
   }
   
   void breakArray(string x[], string y[][7], int num)
   {       
        for(int i=0;i<findRowNumber();i++)
        {
            for(int j=0;j<7;j++)
            {
                 x[i] = y[i][num];
            }
        }
   }
   
   bool checkAccout(string x[], int y, string search)
   {
        bool check = false;
        for(int i=0;i<y;i++)
        {
            if(x[i]==search)
            {
                check = true;
            }
        }
       return check;  
   }
      
   void findUser(string x[], string y[],string z[], string a[], 
    string t[], string c[], string d[])
    {
       string search;
       bool check = false;
       cout<<"Enter Account Number to Search: ";
       cin>>search;
       
       for(int i=0;i<findRowNumber();i++)
        {
            if(x[i]==search)
            {
                cout<<"Account Number: " + x[i]<<endl<<
                "Name:\t\t" + y[i]<<endl<<
                "Age:\t\t" + z[i]<<endl<<
                "Phone:\t\t" + a[i]<<endl<<
                "City:\t\t" + t[i]<<endl<<
                "State:\t\t" + c[i]<<endl<<
                "Zip:\t\t" + d[i]<<endl;
                check = true;
            }
        }
        
        if(!check)
            cout<<"User does not exist"<<endl;
    }
    
    int findRowNumber()
    {
        ifstream inFile("info.csv");
        string line;
        int linenum = 0;
        while (getline (inFile, line))
        {
            linenum++;
        }
        inFile.close();
        return linenum;
    }
    


It seems to work great when i choose to search for a user(selecting 4). However, the problem is when i try to write to file, it's works but when i try to do any other task like searching the user again or re-writing to file, the program crashes.
Please what i'm i doing wrong.

Thanks





Topic archived. No new replies allowed.