It reads the numbers from a file but it is then supposed to find the lowest number. Right now it seems to read numbers from a file (txt file) but it seems to only be reading the first number and printing that. When it should be reading them all and finding the lowest number.
Not sure what I'm missing here.
#include<iostream>
#include<fstream>
#include<cstdlib>
#include <climits>
usingnamespace std;
int main() {
cout << "I will pull the numbers from test.txt. put them in an array, and then print the smallest number. \n";
ifstream instream;
int file_numbers[100]; // name of the array
instream.open("test.txt");
if (instream.fail()) {
cout << "Faild to open file! \n";
exit(1);
}
int cnt = 0; // this will be a counter into the array
while (instream >> file_numbers[cnt]) {
// the while loop is pretty short because it just reads into the array
cnt++;
}
int smallest = INT_MAX ; // if you start it at max int, you will get the smallest at the end
for ( int i=0; i < cnt; ++i ) { // go from 0 to cnt
if ( file_numbers[i] < smallest )
smallest = file_numbers[i] ;
}
cout << smallest << '\n' ;
return(0);
}
In the test.txt that I'm using to pull the numbers from whatever I change the first number to is the one it prints out. If the fist number is 78 that is the one that prints out.
I don't understand what you mean by "output the value of cnt that to see how many records were read?"
Is an element each number in the file? It's supposed to read them all and print the lowest one. Right now it only prints the first number.
I see now from printing the cnt that it is reading all the numbers, so my error must be in how it is sorting or supposed to be sorting the numbers.