0

I'm having issues with some code that I'm doing for an assignment. When I go and compile the file it sometimes works and sometimes it doesn't. The basic idea of the program is to read each line of text from a file and store it into an array (size of the array should be 100 and there should be 100 lines of text). Each string of text (each line) should be stored in it's own array address. Once all lines are stored the program is to pull each line from the array noting which line number it's from. When compiling it with Code::Blocks it runs with no problems, however, when I compile it with cygwin I go to run it and get an error message that says "terminate called after throwing an instance of 'std::bad_cast' what(): std::bad_cast Aborted (core dumped)"

Any help that you guys could give me would be greatly appreciated!

Here is the code that I've gotten so far:

#include <iostream>
#include <fstream>
#include <cstdlib>

using namespace std;

int main()
{
string aFile[100];
ifstream nFile("TMA1Question4 Text.txt");
string nText;

    if (nFile)
    {
        for (int nLineCounter=1; nLineCounter <=100; getline(nFile, nText))
        {
            aFile [nLineCounter] = nText;
            nLineCounter++;
        }
    }

for (int nLineReader=1; nLineReader<=100; nLineReader++)
{
    cout << "Line" << nLineReader << ": " << aFile[nLineReader] << endl;
}

return 0;
}

2 Answers2

1

First of all, arrays are indexed from 0. Your array indices range from 0 to 99, not from 1 to 100. Your for loops should look more like:

for (int nLineReader=0; nLineReader<100; nLineReader++)

Your attempt to use a for loop in a sneaky way is also a problem. The getline will only be called after each iteration. On the first iterations you are sticking the empty string nText into your array. Change it to:

for (int nLineCounter=0; nLineCounter<100; nLineCounter++)
{
    getline(nFile, nText);
    aFile [nLineCounter] = nText;
}

Of course, this depends on you being certain that there are 100 lines in the file. The safer way to read lines from a file is to use getline as the condition for a loop:

int nLineCounter = 0;
while (getline(nFile, nText))
{
    aFile[nLineCounter] = nText;
    nLineCounter++;
}

However, if you were to use a standard container (which you should), you could read lines from the file with no loops at all.

Community
  • 1
  • 1
Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324
  • WOW! Thanks so much for the quick response! I did have it the other way, using the While loop, however, in the coding using the For loop you're only asking it to read up to the 100th line in the file and then the For loop exits, so if there are additional lines it just doesn't read from them...am I correct in this assumption? Also, just as a note I think that changing it from 1-100 to 0-99 fixed it. I can't believe that I had that brain fart! Thanks so much for your help! – Mike Good Feb 09 '13 at 22:34
0

Indexing in C and C++ is zero-based, so start your counter at 0, and go to 1 less than max


That said, consider using a std::vector instead of a raw array. Then you can use the push_back method of std::vector. That way you can more easily store any number of lines, not just exactly 100.


There are also some other problems with the current code, but when you get the technical stuff fixed you’ll presumably spot and fix all that…;-)

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
  • There are probably more efficient ways of doing what I've done here, however, the course that I'm taking is introduction to C++ so I'm just using the stuff that I've been taught so far. I knew that the array started at 0, however I had a brain fart! haha – Mike Good Feb 09 '13 at 22:40