i have to write a code that prompts user to enter a file name (containing 2 columns of integer). The program then reads that file, prints out the numbers set.
My txt file looks like this (the numbers are separated by space):
2 3
5 6
7 8
8 9
5 7
I thought it would be easy, but now i stuck. When i run the program, this line appeared
"There're no number in the file."
I set number to "int", so why can't the compiler read the number? But when i change the type to "string", the numbers did appear O_O , since i later need to calculate the average of those num also, i cannot use string.
Here is my code, I appreciate any help TT___TT
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
ifstream in_file;
string filename;
int number;
int number1;
cout << "In order to process, please enter the file name: ";
cin >> filename;
in_file.open(filename.c_str());
if (!in_file.is_open())
{
cout << "Cannot open file" << endl;
}
else
{
in_file >> number;
if (in_file.fail())
{
cout << "There're no number in the file." << endl;
}
while (in_file >> number >> number1)
{
cout << number << " " << number1;
}
}
system("pause");
return 0;
}