0

So i have a file that has teams and scores, i need to read the team into a 2D array of chars and then their score following into a array of ints.

This is my current code for my function, how do i get this to stop reading after the team name is complete then store the score in a seperate array?

void getData (char array [][COLS], int rows, int scores [])
{
    ifstream inFile;     //Input file stream object

    //Open the file
    inFile.open ("scores.txt");

     if (!inFile)
    {
        cout << "Error opening data file!\n";
        exit(102);
    }

    for (int r = 0; r < rows; r++)
    {
        {
            //reads through columns
            for (int c = 0; c < COLS; c++)
            {
                inFile >> array[r][c];
            }
        }
    }

    for (int count = 0; count < ROWS; count++)
    {
        for(int i = 0; i < COLS; i++)
        {
            cout << array[count][i];
        }
    }

    inFile.close();
}

My input file is as follows:

Jaquars 23
Colts 23
49ers 13
Lions 13
Titans 7
Redskins 38
Cardinals 14
Buccaneers 36
Seahawks 30
Lions 24
Bears 28
Packers 23
Bears 14
Rams 22
Texans 6
Packers 34
john
  • 85,011
  • 4
  • 57
  • 81
  • 1
    What is the layout of the input file? If it is in form of lines having one team and its score per line then you are better off using getline(). Also it will be a good idea to use a vector of strings. Makes life easier. – NotAgain Oct 10 '13 at 22:39
  • an example of the line of input files would be Packers 21, then on the next line Browns 14 for example, and i have to use cstrings for this assignment – Jordan McPeek Oct 10 '13 at 22:45

3 Answers3

0

Maybe like this

for (int r = 0; r < rows; r++)
{
    int c = 0;
    // keep reading until we hit a space
    char ch = inFile.get();
    while (ch != ' ')
    {
        array[r][c] = ch;
        c++;
        ch = inFile.get();
    }
    // now read the score
    inFile >> scores[r];
    // now read until we hit the trailing newline
    ch = infile.get();
    while (ch != '\n')
    {
        ch = inFile.get();
    }
}
john
  • 85,011
  • 4
  • 57
  • 81
  • Jaquars 23 Colts 23 49ers 13 Lions 13 Titans 7 Redskins 38 Cardinals 14 Buccaneers 36 Seahawks 30 Lions 24 Bears 28 Packers 23 Bears 14 Rams 22 Texans 6 Packers 34 This is my file, all of which are on seperate lines – Jordan McPeek Oct 10 '13 at 22:49
  • OK well my answer above is completely wrong. I would put that information into the question above so everyone will see it (and try and get the formatting right). – john Oct 10 '13 at 22:52
  • The answer to your question is to loop until you read a space, when you read a space you know that the name has finished and you've got the read the score next. – john Oct 10 '13 at 22:53
  • So i need to use a getline() in the loop? – Jordan McPeek Oct 10 '13 at 22:56
  • You don't need to use it. You could but that would mean rewriting your code. Look at the code you have written `for (int c = 0; c < COLS; c++)`, that always reads exactly COLS characters. That's obviously incorrect because all your team names are different lengths. What I am saying is change that loop, so that instead of reading always the same number of characters, it reads only until you get to a space. Then you know you've read the team name (none of your team names have spaces in them). – john Oct 10 '13 at 22:59
  • so i will need to make a while loop? im sorry im confused – Jordan McPeek Oct 10 '13 at 23:40
  • @JordanMcPeek Yes a while loop, I edited my answer above to show what I was thinking of. There's other ways to do it, but I was trying to suggest the minimum changes to what you had already written. Whenever you want to write a loop, but you don't know *in advance* how many times you want to go round the loop you should think first of a while loop. That's exactly the situation you have here. – john Oct 11 '13 at 07:23
  • okay i am trying what you wrote for the code and when i look over it i can tell what it is trying to do however when i test it with my program it doesnt do anything, the compiler just flashes a blank line and doesnt make it to my cout declaration. Here is my current code with my cout declaaration – Jordan McPeek Oct 11 '13 at 16:13
  • @JordanMcPeek Maybe you're hitting an end of file problem (perhaps the last line in your file doesn't have a trailing newline). Try this `while (ch != '\n' && ch !=EOF)`. If that doesn't work then just put some `cout << ...` in the loops and see where it's getting stuck. – john Oct 11 '13 at 17:05
  • i edited the answer below and this is where the infinite loop is happening – Jordan McPeek Oct 11 '13 at 21:53
  • okay so i changed at the top of my function a constant int making my column size larger,anyways now it reads a whole bunch of numbers, however in between all these numbers there are the the chars from my file that i want to have read. How do i get rid of those numbers – Jordan McPeek Oct 11 '13 at 23:13
  • nevermind i believe i figured out that problem, the problem i was having had to do with my cout statements, i am using a for loop that goes to a set number so it reads junk numbers for the rest – Jordan McPeek Oct 12 '13 at 00:35
0

This should give you an idea how to go about it. I have not compiled it. But will serve well to illustrate the point. I will strongly suggest that you go for a Map based solution. Or use vectors.

#include <string>
#include <iostream>
#include <fstream>
#include<cstring>
#include<cstdlib>
using namespace std;

int main() 
{   
    ifstream in("DataFile.txt");
    string line;
    char * pch;

    while(getline(in, line))
    {
        pch = strtok (line.c_str()," ");
        while (pch != NULL)
        {
            //Put some logic here so that in first loop iteration
            //first segment(name) gets stored in your char arrays.
            //In second iteration, which will return the score, you store that
            //in the int array. Do not forget to use atoi() to convert to int before storing.
            pch = strtok (NULL," ");
        }
    }

    //There are more elegant ways to split a string in C++
    //http://stackoverflow.com/questions/236129/how-to-split-a-string-in-c
    return 1;
}
NotAgain
  • 1,927
  • 3
  • 26
  • 42
0

This is what i have for my code

void getData (char array [][COLS], int rows, int scores [])
{
     ifstream inFile;     //Input file stream object

    //Open the file
    inFile.open ("scores.txt");

     if (!inFile)
    {
        cout << "Error opening data file!\n";
        exit(102);
    }

for (int r = 0; r < rows; r++)
{
    int c = 0;
    char ch;
    // keep reading until we hit a space
    ch = inFile.get();
    while (ch != ' ')
    {
        array[r][c] = ch;
        c++;
        ch = inFile.get();
    }
    // now read the score
    inFile >> scores[r];
    // now read until we hit the trailing newline
    ch = inFile.get();
    while (ch != '\n' && ch != EOF)
    {
        ch = inFile.get();

    }
}

        for (int count = 0; count < rows; count++)
        {
            for(int i = 0; i < COLS; i++)
            {
                cout << array[count][i];
                for (int count2 = 0; count2 < rows; count2++)
                    cout << scores[count2];
            }
        }

    inFile.close();

    return;
}