I am trying to write a program that reads in a file of numbers, writes to a file and displays the numbers in correct order. I don't fully understand how to read the file and pass that information into my class declarations in order to output before and after the swap. Below is my code so far: Help me understand this please?

#include <iostream>
#include <string>
#include <cmath>
#include <iomanip>
#include <fstream>


using namespace std;


class Sort
{
	private:
		int size;
		int *prt;
		
	public:
		Sort();
		void Print();
		void Selection_Sort( int arr[], int size);
		void Swap ( int &x, int &y);
		
}

Sort::Sort
{
	int size = 0;
	int *ptr = 0;

}  // end of constructor

void Sort::Selection_Sort( int arr[],int size)
{
	int min =0;
	int check = 0;
	int i = 0;

		for ( check = 0; check < size - 1; check++)
		{
			min = check;
			for ( i = 0; i < size; i++)
				if ( arr[i] < arr[min])
					min = i;
		Swap(arr[check], arr[min]);  // call Swap function
		}   // end of for loop
	
}   // end of Selection Sort

void Sort::Swap(int &x, int &y)
{
	int temp;
	temp = x;
	x = y;
	y = temp;
	
} // end of Swap 

void Sort::Print(ostream &o, char * list)
{
		o << endl;
		o << list.Sort;
}


int main() 
{

	ifstream infile("abc.txt", ios::in)   // input file
	
		if (!infile)
		{
			cout << " File could not be opened. " << endl;
			exit(1);
		}
		
	ostream outfile("xyz.txt", ios::out)   // output file
		
		if (!outfile)
		{
			cout << " File could not be opened. " << endl;
			exit(1);
		}
		
	
	return 0;

}
Attachments
#include <iostream>
#include <string>
#include <cmath>
#include <iomanip>
#include <fstream>


using namespace std;


class Sort
{
	private:
		int size;
		int *prt;
		
	public:
		Sort();
		void Print();
		void Selection_Sort( int arr[], int size);
		void Swap ( int &x, int &y);
		
}

Sort::Sort
{
	int size = 0;
	int *ptr = 0;

}  // end of constructor

void Sort::Selection_Sort( int arr[],int size)
{
	int min =0;
	int check = 0;
	int i = 0;

		for ( check = 0; check < size - 1; check++)
		{
			min = check;
			for ( i = 0; i < size; i++)
				if ( arr[i] < arr[min])
					min = i;
		Swap(arr[check], arr[min]);  // call Swap function
		}   // end of for loop
	
}   // end of Selection Sort

void Sort::Swap(int &x, int &y)
{
	int temp;
	temp = x;
	x = y;
	y = temp;
	
} // end of Swap 

void Sort::Print(ostream &o, char * list)
{
		o << endl;
		o << list.Sort;
}


int main() 
{

	ifstream infile("abc.txt", ios::in)   // input file
	
		if (!infile)
		{
			cout << " File could not be opened. " << endl;
			exit(1);
		}
		
	ostream outfile("xyz.txt", ios::out)   // output file
		
		if (!outfile)
		{
			cout << " File could not be opened. " << endl;
			exit(1);
		}
		
	
	return 0;

}

either decide whether you are going to use a class and let selection sort sort the held array pointed at by the int* member, in which case you will need a different constructor than a default one and you will also need to think about copying operations as the int* will point to dynamically allocated memory. Or alternatively keep everything as it is but instead use free functions wrapped inside a namespace. There is no point in using a class if theres no data held. Classes consisting of static functions and no data are pretty pointless most of the time.

I definitely have to use classes. I am trying to re-write it as we speak. When you say copy, are you referring to strcopy the array.

Thanks for the input.

its not a string. I mean you will need a constructor that can create enough memory to hold the array passed to your class and somehow copy the array to it. a constructor taking an int* and an int seems to fit the bill perfectly. an int member size and an int* member ptr kinda suggests that your teach wants you to use dynamic allocation and make your member funcs act on the object itself and not on passed in params. Remember when dealing with dynamic allocations it is important to consider copying operations carefully and also destruction.

This article has been dead for over six months. Start a new discussion instead.