Hi I need some help.
I am trying to write a program that will search a file for numbers of type int and write the largest and the smallest numbers to the screen. This is how far I got but when I it came to writing the code that would read the file and output the largest and smallest to the screen I kinda uh ...... yeah.
Could someone walk me through how I would do it?
#include <fstream>
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
ifstream fin;
ofstream fout;
fin.open("getfromthisfile.txt");
if(fin.fail())
{
cout << "Input file opening failed.\n";
exit(1);
}
fout.open("sendtothisfile.txt");
if(fin.fail())
{
cout << "Input file opening failed.\n";
exit(1);
}
how I think I should do it ... but most likely will not work...
int next;
int largest=0;
int smallest;
while(! fin.eof())
{
next++;
if(next>largest)
{
largest=next;
}
fin.get(next);
fout << "The largest number in getfromthisfile.txt is " << largest << endl;
if(next<smallest)
{
smallest=next;
}
fin.get(next);
fout << "The smallest number in getfromthisfile.txt is " << smallest << endl;
}
fin.close();
fout.close();
system("pause");
return 0;
}
yeah I am completely new to C++ ... ha :)