I'm writing a basic program that will read a list of integers from a text file and output to the screen the smallest integer and largest integer in the file. I made sure that the text file is in the same folder as the source code file, and the name of the file is the same as what I call it in the code. The program cannot open the file, no matter what. How can I fix this?
This is my program:
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
int main()
{
ifstream inStream;
inStream.open("infile.txt");
if (inStream.fail())
{
cout<<"Input file opening failed.\n";
system("pause");
exit(1);
}
int arr[100], i = 0;
while(!inStream.eof())
{
inStream>>arr[i++];
}
int min = arr[0];
for (int index = 1; index <= i; index++)
{
if (arr[index] < min)
{
min = arr[index];
}
}
int max = arr[0];
for (int index = 1; index <= i; index++)
{
if (arr[index] > max)
{
max = arr[index];
}
}
cout<<"The smallest number is "<<min<<endl;
cout<<"The largest number is "<<max<<endl;
inStream.close();
system("pause");
return 0;
}