#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;

int main ()
{
    ifstream fin("program4.txt");
    ofstream fout("output4.txt");
    string lastName;
    string firstName;
    string fournumbers;

    if(fin.fail())
    {
        cerr << "Unable to open input file\n";
        exit(2);
    }

    if(fout.fail())
    {
        cerr << "Unable to open output file\n";
        exit(3);
    }
    while (fin >> firstName >> lastName >> fournumbers)
    {
        fout << setw(10) << firstName << setw(10) << endl;
        fout << setw(10) << lastName << setw(10) << endl;
        fout << setw(10) << fournumbers << setw(10) << endl;
    }

    cout << "Now go check the output4.txt file\n" << endl;

    fin.close();
    fout.close();
    return 0;
}

Hey guys, I'm supposed to write a program that will take the largest of four numbers of an input file (such as 816 423 12 15) and display the TWO largest numbers next to the name the original four were sitting by. Example (this isn't code, its the input file):

JAMES SMITH 828 786 584 24

Then after the program uses them, it displays the following in an output file:

JAMES SMITH 828 24

So far, I've gotten the output file to show everything in a single colum, and it does not sort the two largest numbers. Why is that?

Don't keep the numbers as a string, but as four integers. You could enter the values in the array in sorted order but that might be a bit more difficult. int numbers[4] = {0}; After they are entered then code a sort algorithm to sort them. Or you

Don't keep the numbers as a string, but as four integers. You could enter the values in the array in sorted order but that might be a bit more difficult. int numbers[4] = {0}; After they are entered then code a sort algorithm to sort them. Or you

You may as well have spoken in Greek :( . I understand putting the numbers in an int, but when I put the names in an "int" the output file just goes blank.

What do you mean "After they are entered then code a sort algorithm to sort them"? How do I sort them? What's the algorithm?

Also, how can I get the output file to display information in four colums instead of one?

google for sort algorithms -- the code is all over the net. The easiest to code is the bubble sort.

Your names are fine as strings. Just the numbers can be ints for ease of use

string name;
  int     numbers[2];

 .... 
  ifile >> name >> numbers[0] >> numbers[1] ;
....

Although I don't understand this

JAMES SMITH 828 24

shouldn't your two largest numbers be 824 and 786 based on the example ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.