i get infinite loop errors and i’m not sure why

My main issue was a improperly implemented do-while loop, although the while loop fix posted was also an issue. Thank you to everyone who helped. Here is my fixed code.

int main() {
ifstream inFS;
ofstream ofFS;
int x1, y1, x2, y2;
string fileName;
string fileCheck;
string fileName2;
do {
    while (!inFS.is_open()) {
        cout << "Input the file name: ";
        cin >> fileName;
        inFS.open(fileName);
        if (!inFS.fail()) {
            cout << "Reading file ... ";
            cout << "Done" << endl;
        }
    }
} while (!inFS.is_open());
int length = fileName.length();
length = length - 4;
fileCheck = fileName.substr(fileName.size() - 4, 4);
if (fileCheck == (".txt")) {
    fileName2 = fileName.substr(0, length) + ".log";
    ofFS.open(fileName2);
}
else if (fileCheck != (".txt")) {
    fileName2 = fileName + ".log";
    ofFS.open(fileName2);
}
if (inFS.is_open() && ofFS.is_open()) {
    while (inFS >> x1 >> y1 >> x2 >> y2) {
        double distance = sqrt(pow((x2 - x1), 2) + pow((y2 - y1), 2));
        ofFS << "(" << x1 << ", " << y1 << ")" << " : " <<
            "(" << x2 << ", " << y2 << ")" << " -> " << fixed <<
            setprecision(3) << distance << endl;
    }
    inFS.close();
    ofFS.close();
}
return 0;

}

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top