while 循环不会与 inFile 循环

时间:2021-03-13 06:42:32

标签: c++

我目前正在编写一个读取 .txt 文件的程序。该程序应该读取一个字符,输出一个错误,然后继续读取其余的行,然后照做。我不确定我哪里出错了。

#include <iostream>
#include <cmath>
#include <string>
#include <stream>

using namespace std;

int main(){

    double runningTotal;
    int c = 0;

    string fileName;
    ifstream infile;
    cout << "Enter a file name\n";
    cin >> fileName;

    infile.open(fileName.c_str());

    if (!infile.is_open()) {
        cout<<"Error! invalid entry please retry!\n";
    }
    else {
        string line;
    
        int a, b;
        while (!inflile.fail()) {
            infile >> a >> b;
            c = a + b;
            runningTotal = c;
            cout <<a<<" + "<<b<<" = " << runningTotal<<endl;             
        }
        getline(infile, line, '\n');
        if (infile.fail()){
            getline(infile, line, '\n');
            cout<<"stop being stupid\n";
        }
    }
}

0 个答案:

没有答案