我的循环是在C ++上打印最后一行数据

时间:2018-01-26 19:17:51

标签: c++ file output

所以这给了我想要的输出,除了最后一行打印两次(减去名字)。它是在小的excel表中不存在的最后重复数据。在我的代码中是否有任何明显的东西,我猜我的While循环,这将导致它这样做?谢谢!

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

using namespace std;

int main() {
    // declare variables
    ifstream inFile;
    string name, age, dec, ch, extra;

    // open file
    inFile.open("C:/Users/Joe/Downloads/input.csv");

    //create loop that stops when entire file is read
    while (!inFile.eof()) {


        //collects all data up to a ',' and then stores that data in the respective variable
        getline(inFile, name, '\,');
        getline(inFile, age, '\,');
        getline(inFile, dec, '\,');
        getline(inFile, ch, '\,');
        getline(inFile, extra);




        cout << left;
        cout << setw(8) << name
            << setw(8) << age
            << setw(10) << dec
            << setw(5) << ch
            << setw(5) << extra
            << endl;


    }


    // closes the file
    inFile.close();
    //pause and exit
    getchar();
    getchar();
    return 0;
}

0 个答案:

没有答案