Code:
// Program opens a file filled with integer values seperated by spaces and finds the
// largest value within the file. Integers must be three digits or less, and the final
// integer must be followed by a space to have it included. (all which can be modified
// easily within the code)
# include<fstream>
# include<iostream>
# include <stdio>
using namespace std;
int main ()
{
// Local variables
ifstream ins;
char yourfile[100], mynum[4];
char mychar;
int count, temp, final = 0;
cout << "What is the file path/name: ";
cin >> yourfile;
ins.open(yourfile);
if (ins.fail())
{
while (ins.fail())
{
cout << "Failure opening input file. Check file path." << endl << endl
<< "You entered " << yourfile << endl
<< "Enter path/name (or X to quit): ";
cin >> yourfile;
if ((yourfile[0]=='X')||(yourfile[0]=='x'))
return 1;
ins.open(yourfile);
} // end while
}// end if
count = 0;
while (!ins.eof()) // go until file ends
{
ins.get(mychar);
cout << mychar;
if ((isdigit(mychar))&&(mychar!=' '))
{
mynum[count] = mychar;
++count;
} // end if
if (mychar == ' ')
{
count = 0;
temp = atoi(mynum);
if (temp > final)
{
final = temp;
}
mynum[0] = '\0';
mynum[1] = '\0';
mynum[2] = '\0';
} // end if
}// end while
ins.close();
cout << endl << endl <<"The largest number in the file was "
<< final;
return 0;
}
That does it pretty much. I cannot help you anymore. you put the two together and try to figure it out. There are other ways as well....