User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 426,877 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,310 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 1949 | Replies: 2
Reply
Join Date: Jun 2005
Location: California
Posts: 92
Reputation: djbsabkcb is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
djbsabkcb's Avatar
djbsabkcb djbsabkcb is offline Offline
Junior Poster in Training

Help C++ ques on sorting

  #1  
Sep 16th, 2005
What am I doing wrong I am not getting these values from the input file to even display correctly or sort in order. Give me some direction?




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

using namespace std;
class CommandLineException
{	
	public:
	CommandLineException(int max, int actual)
	{	
		cout << " Too many command line arguments. " << endl;
	}
};


class FileException
{
	public:
	FileException(char* fn)
	{
		cout << " File " << fn << " could not be opened. " << endl;
	}
};
class Sort 
{
	public:
		Sort();
		void read_list(ifstream& i );
		void selection_sort();
		void swap(int& x, int& y);
		void display(ofstream& o);

	private:
		
		vector<int> list;
};  

Sort::Sort()
{
	vector<int> list(1);
}

void Sort::read_list(ifstream& i)
{
	int num = 0;
	while(true)
	{
		i >> num;
		
		if( i.eof())
		{
			break;
		}
		list.push_back(num);
	}
}		

void Sort::selection_sort()
{
	int size = list.size();
	int min=0;
	int temp = 0;
	int i = 0;
	for( temp = 0; temp < size -1; temp++)
	{
		min = temp;
		
			for( i = temp + 1; i < size; i++)
			
				if ( i < min) 
					min = i;
				
			swap(temp,min);
	}
}

void Sort::swap(int& x, int& y)
{
	int temp = 0;
	temp = x;
	x = y;
	y = temp;
}

void Sort::display(ofstream &o)
{
	
	
	o << endl;
	o << "Unsorted list:  " ;
	o << endl;
	o << " Sorted list:" ;
	
}
	

int main(int argc, char * argv[]) 
{

	try
	{
	char infile[100];   // input file
	char outfile[100];  // output file

	
		if (argc == 1)
		{
			cout << " Enter the input file name. " << endl;
			cin >> infile;
			cout << " Enter the output file name. " << endl;
			cin >> outfile;
			cout << endl;
		}
		
		
	        else if (argc == 2)
		{
			strcpy(infile, argv[1]);
			cout << " Enter the output file name. " << endl;
			cin >> outfile;
			cout << endl;
			
		}
		else if ( argc == 3)
		{
			strcpy(infile, argv[1]);
			strcpy(outfile, argv[2]);
		}
		else
		{
			throw CommandLineException(2,argc -1);
		}		
	
	ifstream i(infile);
		if(!i)
			throw FileException(infile);

	ofstream o(outfile);
		if(!o)
			throw FileException(outfile);
	
		Sort num1;
		o << num1.read_list(i);

		
		

		
		
		
       	o.close();
		i.close();
	} catch(...)
		{
		cout <<  " Program Terminated. " << endl;
		exit(EXIT_FAILURE);
		}
	

	
	return 0;
}

AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,192
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 38
Solved Threads: 931
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: C++ ques on sorting

  #2  
Sep 16th, 2005
		o << num1.read_list(i);

function read_list() returns void, so the above line will not compile.
Reply With Quote  
Join Date: Jun 2005
Location: California
Posts: 92
Reputation: djbsabkcb is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
djbsabkcb's Avatar
djbsabkcb djbsabkcb is offline Offline
Junior Poster in Training

Re: C++ ques on sorting

  #3  
Sep 19th, 2005
Yeah, that was one of the errors. that command may work on linux but not visual C. Also, had a problem with my sort function. so it works now. thanks for the inputs everyone.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 10:22 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC