I am writing a program that would put all values not already in the array into the array. However, when I call array after all values have been inserted, the array does not cout any values.
What is possibly wrong?
Note: the program below extracts values from lines of data (values are separated by commas) and places all new nonnumerical values into arrays - each array for each nonnumerical column.
Code:
#include <string>
#include<iostream.h>
#include<fstream.h>
#include<ctype.h>
#include"lvp/vector.h"
int main()
{
const int BUFF_SIZE=100000;
int a;
ifstream infile;
ofstream outfile;
char buff[BUFF_SIZE];
vector<string>col1;
vector<string>col2;
vector<string>col3;
vector<string>col41;
// open the files
infile.open("/misc/tmp/kddcup.data_10_percent");
outfile.open("output.txt");
// make sure the files are open
if(!infile.is_open()){
cerr << "error opening input file";
return 1;
}
if(!outfile.is_open()){
cerr << "error opening output file";
return 1;
}
// loop until the end of the input file
for(a=0; !infile.eof();){
// read in one line
char *p;
char word[20] = "";
infile.getline(buff,200);
p = strtok(buff,",");
for(int i=0; i<42; i++)
{
if(i==1)
{
strcpy(word,p);
bool found = false;
for(int k=0; k<col1.length() && !found; k++)
{
if ( col1[k] == word ) found = true;
}
if ( !found )
{
int pos1 = col1.length();
col1.resize(pos1+1);
col1[pos1]=word;
}
}
if(i==2)
{
strcpy(word,p);
bool found = false;
for(int l=0; l<col2.length() && !found; l++)
{
if ( col2[l] == word ) found = true;
}
if ( !found )
{
int pos2 = col2.length();
col2.resize(pos2+1);
col2[pos2]=word;
}
}
if(i==3)
{
strcpy(word,p);
bool found = false;
for(int m=0; m<col3.length() && !found; m++)
{
if ( col3[m] == word ) found = true;
}
if ( !found )
{
int pos3 = col3.length();
col3.resize(pos3+1);
col3[pos3]=word;
}
}
if(i==41)
{
strcpy(word,p);
bool found = false;
for(int n=0; n<col41.length() && !found; n++)
{
if ( col41[n] == word ) found = true;
}
if ( !found )
{
int pos41 = col41.length();
col41.resize(pos41+1);
col41[pos41]=word;
}
}
p = strtok(NULL,",");
}
a++;
}
for(int w=0; w<col2.length(); w++)
cout<<col2.length()<<endl;
// close the files
infile.close();
outfile.close();
// pause so you can see the output on the screen
cout << " **** All done! ****\n";
cin.get();
return 0;
}
for(int w=0; w<col2.length(); w++)
cout<<col2.length()<<endl;
at the end of code does not return any values.
Instead there is the segmentation fault.
What should I do?
thanks