tags:

views:

128

answers:

2

So I'm trying to read this file. Everything looks like it should work, but during runtime the program times out and stops working, and I have to close it. What is going on? I suspect that the oef() test is never returning true and it keeps looking for more in the file. I have no dragging empty lines in the text file. I've tried debugging this like crazy. I can't find anything wrong but it still refuses to work.

Pet** petArray;

ifstream textFile2;
textFile2.open("pets.txt");

int i = 0;
string temp;
int tmpNum = 0;

if (textFile2.is_open())
{
    while (!textFile2.eof())
    {

        getline(textFile2, temp);

        petArray = new Pet*[arraySize];

        if (temp == "Dogs" || temp == "Cats" || temp == "Iguanas" || temp == "Pigs")
        {
            if (temp == "Dogs") tmpNum = 0;
            if (temp == "Cats") tmpNum = 1;
            if (temp == "Iguanas") tmpNum = 2;
            if (temp == "Pigs") tmpNum = 3;
            temp == "";
        }
        else
        {
            if (tmpNum == 0)
            {
                petArray[i] = new Dog(temp);
                cout << "Dog " << temp << " added" << endl;
            }
            if (tmpNum == 1)
            {
                petArray[i] = new Cat(temp);
                cout << "Cat " << temp << " added" << endl;
            }
            if (tmpNum == 2)
            {
                petArray[i] = new Iguana(temp);
                cout << "Iguana " << temp << " added" << endl;
            }
            if (tmpNum == 3)
            {
                petArray[i] = new Pig(temp);
                cout << "Pig " << temp << " added" << endl;
            }
            arraySize++;
        }

        i++;
    }
}

Here is the format of the text file:

Dogs
d1
d2
Cats
c1
c2
Iguanas
i1
i2
Pigs
p1
p2

Any suggestions?

+1  A: 

eof returns true after you tried to read something and the operation failed. So put it after getline.

EDIT: try this code:

vector<Pet*> petArray;
ifstream textFile2("pets.txt");

string temp;
int tmpNum = 0;

while (getline(textFile2, temp))
{
    if (temp == "Dogs") tmpNum = 0;
    else if (temp == "Cats") tmpNum = 1;
    else if (temp == "Iguanas") tmpNum = 2;
    else if (temp == "Pigs") tmpNum = 3;
    else
    {
        if (tmpNum == 0)
        {
            petArray.push_back(new Dog(temp));
            cout << "Dog " << temp << " added" << endl;
        }
        if (tmpNum == 1)
        {
            petArray.push_back(new Cat(temp));
            cout << "Cat " << temp << " added" << endl;
        }
        if (tmpNum == 2)
        {
            petArray.push_back(new Iguana(temp));
            cout << "Iguana " << temp << " added" << endl;
        }
        if (tmpNum == 3)
        {
            petArray.push_back(new Pig(temp));
            cout << "Pig " << temp << " added" << endl;
        }
    }
}
ybungalobill
Yeah, I know it won't probably solve the problem but there is no enough info to analyze. Definition of temp? If you break in debugger, where it stops?
ybungalobill
could I instead put: while(getline(textFile2, temp) ? Also: I added additional missing content
NateTheGreatt
Yes you can. Btw your program halts on my computer. How have you decided that it never returns from the loop?
ybungalobill
No, I figured that if getline(textFile2, temp) didn't get anything that it would jump out of the while loop. I guess I'm wrong to assume that? Also: I added the format of the text file to the main post.
NateTheGreatt
@Nate: You're right that this your code should exit the loop anyway, so I suspect that either the stream gets into `bad` state before it gets to `eof` or it actually exists and you your program blocks on some other code. Also I guessed the format based on your code ;)
ybungalobill
That works WAY better. Unfortunately I needed to get it to work using dynamic arrays. Question though: how would I go about deleting the vector after it's been used?
NateTheGreatt
Since you store there elements allocated with new you need to write a `for(int i = 0; i < (int)petarr.size(); i++) delete petarr[i];`. The memory used by vector itself is freed automatically when it goes out of scope.
ybungalobill
A: 

what do u mean that it is not working? The way this is written, this will try to read one line more,than you expect. This is because,when the last row is read, getline is not hit 'eof' yet,but when try to read the line,after the last,then will hit 'eof'. so,this may be your problem

Kiril Kirov