-5
#include <iostream>
#include <fstream>

using namespace std;

void get_input (ifstream& ifile){

    std::string filename;
    cout << "Input filename:";
    cin >> filename;

    if (ifile.fail()){
        cout << "File is not found"<< endl;
    }

    int ID, score, count = 0;

    while (1){
        ifile >> ID >> score;
        if (ifile.eof()) break;
        ++count;
    }


    ifile.close();

    cout << ID << endl;
    cout << count << endl;
    cout << score << endl;


}

main:

int main(int argc, const char * argv[]) {

    ifstream file;

    get_input (file);

    return 0;
}

I changed it to std::string, but the counter still prints out 0. I am taking int data from a file that has 2 columns. I also need to count the number of lines in the file.

1 Answers1

2

You have more than one problem. As someone else mentioned, you're not actually opening any file.

if (ifile.fail()) <<this is failing & does not get entered

Instead try

if (!ifile.is_open())

And it will show that you're not opening anything. Once you open the file like so;

ifile.open(filename);

Your code should work, however I can't see where you're checking for line returns. I'll leave that as an exercise for you to follow up. Try searching for std::getline.

ReturnVoid
  • 971
  • 1
  • 8
  • 16