2

I'm trying to read a text file containing 20 names into an array of strings, and then print each string to the screen.

string monsters[20];
ifstream inData;
inData.open("names.txt");
for (int i=0;i<monsters->size();i++){
    inData >> monsters[i];
    cout << monsters[i] << endl;
}inData.close();

However when I run this code the loop is executed but nothing is read into the array. Where have I gone wrong?

pjmil
  • 2,087
  • 8
  • 25
  • 41
  • how are the 20 names in the file separated? newline, space or a custom delimiter? – Evert May 01 '12 at 11:16
  • It does compile and the rest of the program runs fine. And sorry forgot to mention that, the names are separated by new lines. – pjmil May 01 '12 at 11:18
  • I don't know a lot about C++ but I would ask you to put a breakpoint inside the loop anc check. I suspect the "monsters->size()" statement. – maths-help-seeker May 01 '12 at 11:20

2 Answers2

6

Your for loop terminating condition is wrong:

i < monsters->size()

This will actually call size() on the first string in your array, since that is located at the first index. (monsters is equivalent to monsters[0]) Since it's empty by default, it returns 0, and the loop will never even run.

Remember, C++ does not have a size() operator for arrays. You should instead use the constant 20 for your terminating condition.

i < 20
Ashwin
  • 1,942
  • 4
  • 30
  • 59
penguinvasion
  • 156
  • 1
  • 8
  • 1
    And the constant 20 should be declared somewhere as `const uint8_t numberOfMonsters = 20;` – Vincenzo Pii May 01 '12 at 11:21
  • OK, I've applied this, but it still isn't reading anything from the file. Do I need to add something to iterate through each newline? – pjmil May 01 '12 at 11:32
  • @pjmil116, does the file definitely get opened? Are the reads succeeding? – hmjd May 01 '12 at 11:40
  • Ifstream's >> operator is separated by spaces by default. It will automatically iterate through "words". You can use getline(inData, monsters[i]) if you want to do it by line. But I can't see any other reason why this code isn't working. – penguinvasion May 01 '12 at 11:42
  • @hmjd I've applied all of the changes, it still only displays 20 blank lines in the cout though. – pjmil May 01 '12 at 11:51
  • Also, the file is saved as names.txt in the project's folder. – pjmil May 01 '12 at 11:54
  • Make sure your working directory is set to that project folder that names.txt is located in. You can use the is_open() function on inData to check whether there were any errors opening the file. – penguinvasion May 01 '12 at 11:58
  • 1
    @puller `uint8_t` is probably overkill here. `int` or `unsigned int` are perfectly fine if there is no additional requirement. – Quentin Pradet May 01 '12 at 17:29
3

monsters->size() is 0 at runtime. Change that line to for (int i=0;i<20;i++).

 string monsters[20];
    ifstream inData;
    inData.open("names.txt");
    for (int i=0;i<20;i++){
        inData >> monsters[i];
        cout << monsters[i] << endl;
    }inData.close();
Software_Designer
  • 8,490
  • 3
  • 24
  • 28