I have just learned how to use fstream in my programming class and we are supposed to gather the information from a file we have created which I have named Section51.dat
The program is supposed to find the largest integer in the file. There is something wrong with my while loop and I can't work it out in my head. It keeps telling me the number of integers in the file instead of telling me the LARGEST integer in the file. Can someone show me how to design this loop in the correct way? I am having trouble seeing how it will compare one integer to the next in a loop .
Code:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream RED;
RED.open("Section51.dat");
int aNumber;
int highestNumber = 0;
while (RED >> aNumber)
{
if (aNumber >> highestNumber)
{
highestNumber = aNumber;
}
}
cout << highestNumber;
return 0;
}