-1

I did some searching on this site and on google as well. But i couldnt understand a lot of code i seen and im hoping to find more direct help from here. Im only 2 semesters in for c++ and i have a side project id like to do for my boss.

He generates a csv file for call logs and i want to be able to retrieve certain lines from the log and be able to calculate and display data.

Im not sure of the exact questions i need to ask but heres my code where i tried to start getting data but ran into problems (my programming knowledge is fairly limited due to lack of time and experience :)

#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

int main()
{
//opens the csv file.
ifstream gwfile;
gwfile.open("log.csv");

if(!gwfile) { // file couldn't be opened
  cout << "FAILED: file could not be opened" << endl << "Press enter to close.";
  cin.get();
  return 0;
}else
    cout << "SUCCESSFULLY opened file!\n";

cout << "-------------------------------------------------------------------------------\n\n\n";

long int SIZE = 0;
char data[SIZE];
cout << "This is data SIZE:" << data[SIZE] << endl;

//in the csv im trying to only read lines that start with the voice as those are only valid data we need.
//also i would like to display the headings in teh very first line
while( !gwfile.eof() ){
    //This is where im trying to only accept the lines starting with "Voice"
    //if(data[SIZE] == "Voice"){
        for( int i=0; i!=","; i++){
            cout << "This is i: " << i << endl; //testing purposes.
        }
    //}
//        getline(gwfile, data, '');
//        cout << data[0];
}
return 0;
}
Nickoli
  • 31
  • 2
  • 8

1 Answers1

2

Let’s begin with the obvious

long int SIZE = 0;
char data[SIZE];
cout << "This is data SIZE:" << data[SIZE] << endl;

You are creating an array of size 0, then reaching for its first member: data[0]. This cannot work. Give your array a size that is large enough to handle the data you want to treat, or use a dynamicly resizable container (such as std::vector) to deal with it.

qdii
  • 12,505
  • 10
  • 59
  • 116
  • Not to mention `SIZE` is a variable and we don't have variable sized arrays in C++ (although we do in GNU extensions) – Shahbaz Mar 11 '12 at 01:09
  • I thought i could try to put them into array so i could add certain data. But i also used getline(gwfile, string data, ',') which i was able to capture a full line but i cant figure how you capture additional lines. – Nickoli Mar 11 '12 at 01:22