Finding the largest four numbers of an input file.

Reply

Join Date: Sep 2008
Posts: 25
Reputation: PaladinHammer is an unknown quantity at this point 
Solved Threads: 0
PaladinHammer PaladinHammer is offline Offline
Light Poster

Finding the largest four numbers of an input file.

 
0
  #1
Oct 8th, 2008
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <iomanip>
  5. using namespace std;
  6.  
  7. int main ()
  8. {
  9. ifstream fin("program4.txt");
  10. ofstream fout("output4.txt");
  11. string lastName;
  12. string firstName;
  13. string fournumbers;
  14.  
  15. if(fin.fail())
  16. {
  17. cerr << "Unable to open input file\n";
  18. exit(2);
  19. }
  20.  
  21. if(fout.fail())
  22. {
  23. cerr << "Unable to open output file\n";
  24. exit(3);
  25. }
  26. while (fin >> firstName >> lastName >> fournumbers)
  27. {
  28. fout << setw(10) << firstName << setw(10) << endl;
  29. fout << setw(10) << lastName << setw(10) << endl;
  30. fout << setw(10) << fournumbers << setw(10) << endl;
  31. }
  32.  
  33. cout << "Now go check the output4.txt file\n" << endl;
  34.  
  35. fin.close();
  36. fout.close();
  37. return 0;
  38. }

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?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 16,628
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1615
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Finding the largest four numbers of an input file.

 
0
  #2
Oct 8th, 2008
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
The most important thing in the Olympic Games is not to win but to take part, just as the most important thing in life is not the triumph but the struggle. The essential thing is not to have conquered but to have fought well.
-Pierre de Coubertin, The Olympic Creed Inspired by Bishop Ethelbert
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 25
Reputation: PaladinHammer is an unknown quantity at this point 
Solved Threads: 0
PaladinHammer PaladinHammer is offline Offline
Light Poster

Re: Finding the largest four numbers of an input file.

 
0
  #3
Oct 8th, 2008
Originally Posted by Ancient Dragon View Post
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?
Last edited by PaladinHammer; Oct 8th, 2008 at 11:44 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 16,628
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1615
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Finding the largest four numbers of an input file.

 
0
  #4
Oct 8th, 2008
google for sort algorithms -- the code is all over the net. The easiest to code is the bubble sort.
The most important thing in the Olympic Games is not to win but to take part, just as the most important thing in life is not the triumph but the struggle. The essential thing is not to have conquered but to have fought well.
-Pierre de Coubertin, The Olympic Creed Inspired by Bishop Ethelbert
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 307
Reputation: stilllearning has a spectacular aura about stilllearning has a spectacular aura about 
Solved Threads: 43
stilllearning stilllearning is offline Offline
Posting Whiz

Re: Finding the largest four numbers of an input file.

 
0
  #5
Oct 9th, 2008
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 ?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum


Views: 965 | Replies: 4
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC