Mike Good
Mike Good

Reputation: 1

Storing and Reading Strings From Arrays

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;
}

Upvotes: 0

Views: 101

Answers (2)

Joseph Mansfield
Joseph Mansfield

Reputation: 110778

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.

Upvotes: 1

Cheers and hth. - Alf
Cheers and hth. - Alf

Reputation: 145457

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…;-)

Upvotes: 0

Related Questions