1

I apologize if this has been answered already, I tried searching and couldn't figure out why this isn't working.

I am writing a program to read from a file, the file contains lines of one name followed by 5 integers. I am trying to read the name into one string array and then read the 5 integers into another array. When I run my code the first name and first 5 integers are read as expected, but then when the loop continues, nothing else is read into the arrays.

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

using namespace std;

int main()
{
    int row, col, average, students = 0;
    string storage;

    string names[15];
    double test[15][5];
    double grade[15];

    ifstream inFile;
    ofstream outFile;

    inFile.open("testScores.txt");
    outFile.open("averages.out");

    for (students; !inFile.eof(); students++)
    {
        //getline(inFile, storage, '\n');
        inFile >> names[students];
        for (col = 0; col <= 5; col++)
        {
            inFile >> test[students][col];
        }
        inFile.ignore('\n');

    }

I know that using namespace std is frowned upon, but that is how my teacher wants us to complete the code. I tried adding an ignore in to skip to the next line in the input, but that hasn't seemed to work. I also tried using a temporary storage string using getline, but wasn't sure that was the best way to go about it. Any help would be greatly appreciated. Thank you

input file-

    Johnson 85 83 77 91 76
    Aniston 80 90 95 93 48
    Cooper 78 81 11 90 73
    Gupta 92 83 30 69 87
    Blair 23 45 96 38 59
    Clark 60 85 45 39 67
    Kennedy 77 31 52 74 83
    Bronson 93 94 89 77 97
    Sunny 79 85 28 93 82
    Smith 85 72 49 75 63
Sam
  • 11
  • 2
  • Read this https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong Then show us what your input file looks like. This and the many other duplicates may hep as well. https://stackoverflow.com/questions/1744665/need-help-with-getline – Retired Ninja Jul 29 '17 at 17:29
  • 1
    Is it `col <= 5` or `col < 5`? – iBug Jul 29 '17 at 17:31
  • @iBug I'm guessing that's the problem too, but without the input file it's just a guess. But yeah, trying to read a letter into a number causes an error, and since there's no error checking it goes unnoticed and every other read fails. – Retired Ninja Jul 29 '17 at 17:33
  • Would the use of eof be causing the read error? or is that more of something to fix because it is bad practice? – Sam Jul 29 '17 at 17:34
  • 2
    The problem is you're testing for a specific condition in the wrong place. Test for errors when you read, not before you read so you don't process bad data, and if you must test for errors on the stream test for all of them, not just `eof`. Based on your input and your loop condition, you have 5 numbers to read (0-4) and you try to read 6 (0-5). – Retired Ninja Jul 29 '17 at 17:37
  • At least, you have error here: `for (col = 0; col <= 5; col++)`. If you want to read 5 numbers, code should looks like `for (col = 0; col < 5; col++)` – Bor Laze Jul 29 '17 at 17:42
  • Thank you guys, I feel pretty silly that I didn't catch that, but changing it to col < 5 and removing the ignore statement fixed my problem. – Sam Jul 29 '17 at 17:44
  • If you keep the `eof` you'll still likely try and fail to read the last line twice. – Retired Ninja Jul 29 '17 at 17:46
  • [ignore()](http://en.cppreference.com/w/cpp/io/basic_istream/ignore) takes a number (of characters to ignore) as first parameter. The second is the delimiter. – O'Neil Jul 30 '17 at 01:03

1 Answers1

0

I used the getline function and a stringstream

it's easier to handle with getline because you can write the in a string and modify or analyze this string

and stringstream is an cool way to extract data from a string

Here's how I gonna do it

you have to include sstream

string line{""};

if (inFile.is_open()) {
  while (getline(inFile,line)){
     names[students] = line.substr(0, line.find(" "));
     stringstream ss;
     ss << line.substr(line.find(" "));

     for(size_t i{0}; i < 5; ++i){
       ss >> dec >> test[students][i];
     }  
     ++students;
  }

  inFile.close();  
} else {
      cout << "could not read file";
}
lambda
  • 56
  • 5