Reading two integers from a file seperated by a whitespace

I need to figure out how to get two integers, that may be of any size, and are separated by a white space and store first one in a variable in one class, and the second one in a completely different class. Below is the code I have so far to get rest of the information into the first class.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
  Player::Player(char fileName[], char testFile[])
{
    ifstream inFile;
    inFile.open(fileName);
    if(!inFile)
    {
        cerr << "Dictionary file could not be located." << endl;
    }

    //Read the words in the dictionary file into a vector
    string word;
    inFile >> word;
    playerDictionary.push_back(word);
    while(inFile)
        {
            inFile >> word;
            playerDictionary.push_back(word);
        }

    ifstream testfile;
    testfile.open(testFile);
    if(!testfile)
    {
        for(int i = 0; i < 31; i++)
        {
            playerLetters[i] = ALPHABET.at(i);
        }
    }
    else
    {
        for(int i = 0; i < 31; i++)
        {
            playerLetters[i] = '0';
        }
        string line;
        char temp;
        int index = 0;
        for ( int lineno = 0; getline(testfile,line) && lineno < 8; lineno++)
        {
            if (lineno == 6)
            {
                for(unsigned int i = 0; i < line.length(); i++)
                {
                    temp = tolower(line.at(i));
                    if(temp >= 'a' && temp <= 'z')
                    {
                        playerLetters[index] = line.at(i);
                        index++;
                    }
                }
            }
        }
        for ( int lineno = 0; getline(testfile,line) && lineno < 10; lineno++)
        {
            if (lineno == 9)
            {
                
            }
        }
    }
}

Obviously the last for loop is incomplete. I just assumed that I could use a structure similar to where I read into my playerLetters array. The first 6 lines of the file contain information about a grid, and the 8th line is letters for another player's letter array, contained in a seperate class. The input file could look something like this:

.......
.......
...at..
..uoi..
ttttttt
sssssss
abcdefghijklmnopqrstuvwxy
a a b e e i i o o u
22 14


I just cant think of a good algorithm to put the information in the last line where I want it. I think, I need to find a way to read until I hit a whitespace, and then in the other class skip all integers until I hit a whitespace, and them read again when I have no more whitespace until I find integers again. This may be simple, I have been up for almost two days straight working on this program, so please forgive my simple-mindedness if it is.

This is for a homework assignment, so gentle prodding would be fine! Thank you for the help.
First off, you have a problem at lines 12-18. If either input operation fails, you still push word onto the vector. Consider the following instead:
1
2
3
 
  while (inFile >> word)
    playerDictionary.push_back(word);


I just cant think of a good algorithm to put the information in the last line where I want it.

I'm assuming you're referring to the 22 and 14 and want them to go into variables in separate classes.

I see several issues:

1) parsing the two numbers out of the line. That part is easy if you can use stringstream.
1
2
3
4
  getline(testfile,line);  // Assume we read line 9.  No error handling
  stringstream ss(line);  // create a stream from the line we just read
  int num1, num2; 
  ss >> num1 >> num2;  // No error handling here 


2) Line 53: Your loop starts with lineno = 0, but you haven't repositioned the file file back to the beginning. You probably want to move lines 55-58 after line 50 and get rid of the loop at line 53. Also adjust your termination condition at line 38.

3) Now that you've got num2, what are you going to do with it? How are you going to get addressibility to the other class where you want to store it. Nothing is apparent from what you've posted.






Last edited on
Anon,
Thank you for the reply. I fixed the first problem by checking for the file in the driver file instead, so that is good now. I will do some research on stringstream, I am not familiar with how to use that; and I will mce my for loop as you suggested. As for the third problem, I was just going to repeat the procedure in my other class, but use num2 instead of num 1 when assigning it where I need it to go. I know it isnt very elegant, but I still havent really figured out how to make classes work together nicely. Thanks again for your reply, I will go try out your suggestions.
One axiom I've always found to be true, is that if you're struggling with your class structure, then it's probably not right.

For example, why is your dictionary an attribute of a player? Does each player have a unique dictionary? Or is one dictionary common to all players? In either case, I would recommend creating a Dictionary class.
A dictionary is an object. The implementation of the Dictionary class should be invisible to a player. Make the Dictionary class responsible for loading itself. If all players share a dictionary, then you need only one instance. If each player has a unique dictionary, then each player can have their own Dictonary object. The same class works either way.

Not sure what you're loading from the testfile, but I would guess that's also an object and deserves it own class.

Last edited on
Thank you for the help. I have been struggling with classes for a while now, I know that I will get it though. Just got to keep working at it. Your solution to the problem worked great though, so I am going to mark as solved. Thank you again for the help!
Topic archived. No new replies allowed.