-1

I am trying to pull two columns of data from a CSV file and dump the rest.

The errors I am receiving are:

C2296: '>>': illegal, left operand has type 'std::basic_istream> &(__thiscall std::basic_istream>::* )(_Elem *,std::streamsize)'

C3867: 'std::basic_istream>::read': non-standard syntax; use '&' to create a pointer to member

The data is formatted as such:

1928,44.50%,.......

I want the 1928 assigned into data.year, and the 44.50% assigned into data.yield, but not including the percent sign.

bool ReadData(MyData &data)
{
//Variable for reading data into file stream
ifstream inFile;
string trash;
char junk;

cout << "\nReading file . . .";

//Open data file
inFile.open("Data.csv");

//Read the first 18 lines, and throw it away
for (int i = 0; i < 18; ++i)
{
    getline(inFile, trash);
}

//Read the necessary data into the arrays
for (int i = 0; i < SIZE; ++i)
{
     //===============================================================
     //This line is throwing 2 errors
     //Goal: read first column of a simple integer into data.year, discard the comma, then read the second column of a double into data.yield, discard the percentage sign. infile.ignore(); to clear cin stream, getline(inFile, trash) to discard remainder of the lines.

    inFile.read >> data.year[i] >> junk >> data.yield[i] >> junk >> trash >> endl;

     //===============================================================
    inFile.ignore();
    getline(inFile, trash);
}

//Return false if file could not be opened
if (!inFile)
{
    cout << "\n\nTechnical error! The file could not be read.";
    return false;
}
else
{
    cout << "\n\nFile opened successfully!";
    return true;
}
inFile.close();
}

struct MyData
{
int year[SIZE];
int yield[SIZE];
double minYield;
double maxYield;
double avgYield;
};

Where am I going wrong?

Andrew
  • 7
  • 5

2 Answers2

0

The very first problem is reading a file line by line constant number of times, however you never know the size of the file. So, you should add another check to your for loop. The second problem is that you say the yield is an int but it is a double in the file. The third problem is reading formatted data is not something like you did. The following piece of code can work for you, or you can play a bit with the code.

for (int i = 0; i < SIZE && std::getline(infile, line); ++i) {  
    std::stringstream linestream(line);
    std::string  year, yield;

    getline(linestream,year,',');
    getline(linestream,yield,',');

    yield.erase(std::remove(yield.begin(), yield.end(), '%'), yield.end()); // remove %

    myData.year[i] = std::stoi( year );  // string to int
    myData.yield[i] = std::stod( year ); // string to double

  }

PS: Don't forget to include sstream library.

eneski
  • 1,575
  • 17
  • 40
0
inFile.read >> data.year[i] >> junk >> data.yield[i] >> junk >> trash >> endl;

inFile.read is a function and has no operator >>, that's why you get the error. See https://en.cppreference.com/w/cpp/io/basic_istream/read

I'd suggest you try a different approach: Read the entire line and use a explode function to retrieve the individual elements. For example Is there an equivalent in C++ of PHP's explode() function?

Fubert
  • 80
  • 5