1

I have a txt file with this format:

<Year> <count>
1999   5.4
1989   5
1993   55.4

I have to get each year and count from the file, convert the year in to int and count in to double and store them in arrays.

Problem: it shows random huge negative numbers

stringstream line;
  string a;
  int year;
  double count;
  ifstream myfile(path);
  if (myfile.is_open())
  {
   getline(myfile, a); //getting rid of the first line.
   while (getline(myfile, a))
   {
    cout << a << '\n';
    line >> year >> count;
    cout << "year:" << year << endl;
    cout << "count:"<< count << endl;

   }
   myfile.close();

1 Answers1

0

You could just stream directly into your variables instead of using getline

Like so:

YourStream >> YearInt >> YourDouble;

Just using a getline at the beginning to get rid of the header.

HSchmale
  • 1,838
  • 2
  • 21
  • 48
  • Hey I updated my post with your advice. I used Stringstream but Now I get negative random numbers instead of the numbers in the file. – Veliko Kosev May 16 '17 at 21:06