•
•
•
•
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
![]() |
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;
}
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,192
Reputation:
Rep Power: 38
Solved Threads: 931
o << num1.read_list(i);
function read_list() returns void, so the above line will not compile.
![]() |
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- sorting an array of string (C)
- help by sorting a simply linked list (C)
- sorting file (C)
- sorting 2d arrays (C)
- re: Sorting Algorithms (C++)
Other Threads in the C++ Forum
- Previous Thread: checking for a space bar input
- Next Thread: Maps in VC++ 6.0



Linear Mode