Hi, i need some help to make a prograsm that will read from a file and sort the numbers in lowes to highest order. For example the file will contain 5 4 3 2 1 and i want to to come out as 1 2 3 4 5 . How will i do this? I have some idea where to start from as i have made it so it loads the numbers but i dont know how to properly sort them in order. I know i need to make it so that the first number check with the second number and it flips accordingly and so on.

This si what i have so far.

//Program that demonstrates how to use input and output files
//Libraries used in the program

#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <conio.h>
using namespace std;



int main()
{
//Declaration of Variable and Arrays
	 int Index=0;
	 int NumData = 1;
	 vector<string> Array1(NumData);
	 vector<string> Array2(NumData);
		 
	 
//Specifies the Files that are used for Input and Output	 
	 ifstream InFile1("InData.txt");
	

	 
//Displays error message if File can't be opened
	 if (InFile1.fail())
		{cout << "File could not be opened";
		return(0);
		}

//Loads Array from Input file - READS ENTIRE LINE	 
	 while ( getline(InFile1, Array1[Array1.size()-1]) )
		{ 		 
		 Array1.resize(Array1.size()+1);
		}
		InFile1.close();

//Removes extra space at the end
		Array1.resize(Array1.size()-1);

//Displays array and outputs it to outfile
		ofstream OutFile ("OutData.txt");
		cout << "Array: ";
		for (Index = 0; Index < Array1.size();  Index++)
			{
			OutFile << Array1[Index]<< endl;

			cout << Array1[Index]<<endl;
			
			}
system("pause");
	 return(0);
}

Recommended Answers

All 5 Replies

You clearly know enough about sorting to mention one of the common (unfortunately) algorithms by name: bubble sort. I get the impression that you haven't even attempted to write the sort, so I'll simply link you to a tutorial.

Which compiler/IDE are you using? <conio.h> is a non-standard header file. Besides this discrepancy, there seems to be no need for it in your code... If you had compiled this on a new compiler, then this would surely have thrown an error, wouldn't it?

If you had compiled this on a new compiler, then this would surely have thrown an error, wouldn't it?

Nope, I can compile it using the newest MinGW compiler :)
(BTW, If there's need for a Borland-style conio implementation for MinGW, you can always find it here)

Regarding to the OP's question:
Check out Narue's page, it's plenty of useful information and code examples, you could also throw a look at this code snippet :)

>>Nope, I can compile it using the newest MinGW compiler
Wow! I am fully impressed, I will give you 500 points for this. But, being silent, knowingly that OP is moving toward non-portable code should not be practiced by (a portability fan like) you. The OP should be aware that conio is not part of the Standard C++ Library one should seldom use it.
I cannot say the rest of the code is standard though, but it is okay since at least it is compiling on g++(or any other standard compiler for that matter).

Nope, I can compile it using the newest MinGW compiler :)
(BTW, If there's need for a Borland-style conio implementation for MinGW, you can always find it here)

Now this is something new... Thanks for the info, tux4life... I thought <conio.h> was well shot of, but it seems that it isn't... :) When I first began coding in C++ (About a couple of years ago), I started on Turbo C++ 3 (No Flames, please), and couldn't live without <conio.h> ... That sure was a blast from the past!

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.