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
#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";
开发者_StackOverflow中文版 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
Your emp
string array in the main function is declared with the initial size of the file and never resized when you add more entries, hence the crash when it tries to read a bigger file into the old size of array.
精彩评论