| | |
Finding the largest four numbers of an input file.
![]() |
•
•
Join Date: Sep 2008
Posts: 25
Reputation:
Solved Threads: 0
C++ Syntax (Toggle Plain Text)
#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.
After they are entered then code a sort algorithm to sort them. Or you
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
-Pierre de Coubertin, The Olympic Creed Inspired by Bishop Ethelbert
•
•
Join Date: Sep 2008
Posts: 25
Reputation:
Solved Threads: 0
•
•
•
•
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
. 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.
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
-Pierre de Coubertin, The Olympic Creed Inspired by Bishop Ethelbert
•
•
Join Date: Oct 2007
Posts: 307
Reputation:
Solved Threads: 43
Your names are fine as strings. Just the numbers can be ints for ease of use
Although I don't understand this
JAMES SMITH 828 24
shouldn't your two largest numbers be 824 and 786 based on the example ?
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 ?
![]() |
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: Is it possible
- Next Thread: String function
Views: 965 | Replies: 4
| Thread Tools | Search this Thread |
Tag cloud for C++
algorithm api array arrays assignment basic beginner binary browser c++ c/c++ calculator char class classes code compile compiler constructor conversion convert count data delete desktop display dll dynamic encryption error file files form fstream function functions game givemetehcodez graph gui homework i/o iamthwee input int integer lazy library linker list loop loops map math matrix member memory network newbie news number object objects opengl output parameter pointer pointers problem program programming project random read recursion recursive reference sort sorting spoonfeeding string strings struct student studio template templates text time tree undefined variable vc++ vector video visual win32 window windows winsock






