1

Our professor gave us this assignment, where we have a .txt file with the following format:

John 23
Mary 56
Kyle 99
Gary 100
...etc. etc.

What we have to do is read the file, and store the names and scores in parallel arrays.

This is turning out to be a bit more challenging to me than I anticipated. What is confusing me, when searching around stack, is all the different libraries people use to do this. Our Prof just wants us to use string, fstream, and sstream to do this.

Below is what I've come up with so far, it compiles perfectly, splits the scores from the names but stores them in the same array:

#include <string>
#include <sstream>
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
  const int SIZE = 50;
  string names[SIZE];
  int score [SIZE];
  short loop = 0;
  string line;

  ifstream inFile("winners.txt");

  if (inFile.is_open())
  {
    while(!inFile.eof())
    {
       istream& getline(inFile >> line);
       names[loop] = line;
       cout << names[loop] << endl;
       loop++;
    }
    inFile.close();
  }

  else cout << "Can't open the file" << endl;

  return 0;
}

I'm not looking for someone to solve my HW problem, I just want a push in the right direction!

Mike
  • 47,263
  • 29
  • 113
  • 177
rcorrie
  • 921
  • 8
  • 26

3 Answers3

1

If you want to read two things for each line of input, it seems reasonable to have two "read" statements:

std::string name;
inFile >> name;

int score;
inFile >> score;

std::cout << "Read score " << score << " for name " << name << '\n';

...then you can do that repeatedly until you've read the entire file.


Edit: After you get the basic logic worked out, you might want to think about error handling. For example, what is appropriate behavior for your program if the input file doesn't contain 50 pairs of (name, score)? How can you change your code to get that behavior?

Nate Kohl
  • 35,264
  • 10
  • 43
  • 55
  • I would personally insist that *every* unguarded input operation is a hard programming error. – Kerrek SB May 19 '12 at 19:45
  • @KerrekSB: That's valid, but given the context of the OPs question, I thought a simple answer would be more appropriate here. – Nate Kohl May 19 '12 at 19:48
  • 1
    Perhaps, but given how soul-destroyingly pervasive this misunderstanding of basic I/O is even into the so called "experienced" level of developers, I have come to adopt a zero-tolerance attitude on this subject, and I think you can't start teaching it early enough. – Kerrek SB May 19 '12 at 19:56
  • @KerrekSB I appreciate your perspective, could you elaborate a bit on this? – rcorrie May 19 '12 at 20:05
  • 2
    @RCorrie: It's quite simple: I/O, or at least *input*, always produces data that's *outside your control*, so it could be *anything*: expected data, unexpected data, or the input stream could even be closed altogether. Thus *every* input operation can (and will) fail at some point. It is thus mandatory that you check the return value of *every* input operation, and that you do so *before* you proceed to consume the alleged data produced by the operation. The data produced in an input operation must be considered invalid *unless* the result of the operation says it's OK. – Kerrek SB May 19 '12 at 20:09
  • @RCorrie: In short: Your program's correctness must not depend on external circumstances. You have to be able to stand up and guarantee that your program always behaves well (and deterministically). – Kerrek SB May 19 '12 at 20:11
  • @KerrekSB oh ok I understand, we've gone over this in class, and it is something that we are required to do. Thanks for your input – rcorrie May 19 '12 at 20:11
0

Each line in the file consists of a name and a score separated by whitespace. You're reading each line but not splitting it into its parts (the name and the score).

MRAB
  • 20,356
  • 6
  • 40
  • 33
0

Ideally you would use a vector for this, but since it seems that you were asked to use arrays we'll stick with arrays. What you have above looks good until you start reading entries. A more idiomatic way to accomplish this is to use std::getline, i.e.

ifstream inFile( "winners.txt" );
std::string line;

while( std::getline( inFile, line )) {
  // Do work here.
}

Inside the loop you need to split the line on the space. Without solving the problem for you, I suggest you take a look at the find and substr functions of the string class: here. They will give you everything you need to solve the problem.

Chris Hayden
  • 1,104
  • 6
  • 6