Visit Jeremy's Blog.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 05-04-2011, 03:14 PM   #1
LQ Newbie
Registered: Aug 2010
Location: Clayton, CA
Distribution: Slackware
Posts: 2

Rep: Reputation: 0
C++ sorting problem

Hey everyone, this is my first post on LQ. I have been having some problems writing a program for my Computer Science class. The objective here is to input text from a file into an array based list and sort it in alphabetical order. This program input the text correctly and outputs it to the console correctly, but it will not sort it correctly. The function sortAlpha create a temporary array of strings based on the array passed to it an converts all of the strings to upper case, this works as I have output that array to confirm. The sorting code below it was taken out to our book. It should use the temporary array I created int the function to compare two elements and switch them if one is greater that the other (not sure exactly how this works with strings, but I got it out of a working program in our book). When I run the program it doesn't sort correctly. Can someone tell me what is wrong with this or tell me a better solution to sorting. Thank you.

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
#include <cctype>

void sortAlpha(string *array, int size);
void printList(string *array, int size);

int main()
const int MAX_NAMES = 8; //maximum list size
int nNames = 0; //list counter
string names[MAX_NAMES]; //list
string aName; //temp input variable
string filename;
ifstream fin; //file input stream

cout << "Enter name of input file: "; //get filename from console
getline(cin, filename);; //open file
if (!fin.good()) throw "I/O Error";

int i = 0;
while (i < MAX_NAMES) //read up to 8 names from file or eof which ever comes first
if (!fin.good()) break;
getline(fin, aName);
if (aName == "") continue; // if line is black skip it and try again without adding to list or incrementing counter
names[nNames++] = aName;

sortAlpha(names, nNames); //sort funcion
printList(names, nNames); // print function

fin.close(); //close file
return 0;

void sortAlpha(string *array, int size)
string *Arr = new string[size]; //temporary array to hold upper case version of origonal array
string temp; //temp string

int i;
int j;
for (i = 0; i < size; i++) //convert each string to upper case
temp = array[i];

for (j = 0; j < temp.size(); j++)
temp[j] = toupper(temp[j]);

Arr[i] = temp;

for (i = 0; i < size; i++) //sort based on converted temporary array, but modify original array
for (int j = i + 1; j < size; j++)
if (Arr[i] > Arr[j])
temp = array[i];
array[i] = array[j];
array[j] = temp;

delete [] Arr; //free memory

void printList(string *array, int size)
for (int i = 0; i < size; i++)
cout << array[i] << endl;
Old 05-04-2011, 03:37 PM   #2
Registered: Dec 2007
Location: Slightly left of center
Distribution: slackware
Posts: 276
Blog Entries: 2

Rep: Reputation: 44
You're only iterating once through the array to be sorted... that won't work.

IMO, a function to switch places of two elements, called recursively, is the best method (see the code for qsort for a great example). If your instructor allows the use of qsort rather than re-creating its functionality, by all means use it. Otherwise, for this simple example, a similar approach can be constructed rather easily.

This reply is intentionally general and somewhat vague -- it is your homework, after all
Old 05-04-2011, 03:50 PM   #3
LQ 5k Club
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Use [code] tags. What you posted is impossible to read the way it is.
Old 05-04-2011, 04:19 PM   #4
Senior Member
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,186

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
Oops. I was thinking C, not C++. Comment removed.

Last edited by PTrenholme; 05-04-2011 at 04:22 PM.
Old 05-04-2011, 04:46 PM   #5
LQ Newbie
Registered: Aug 2010
Location: Clayton, CA
Distribution: Slackware
Posts: 2

Original Poster
Rep: Reputation: 0
Problem solved. The sort loop used the temporary array Arr to compare and then sorted the original array, but it did not sort the temp Arr array as a result everything after the first sorting operation through the loop was off. That is why I got garbage output. Also I will look at qsort. I am a bit familiar with it as I have used it once or twice before, but I don't think I will modify this code now that it satisfies the requirements for the class. Thanks for your input everyone.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Chemistry problem- File matching and Sorting!!! robertselwyne Programming 9 07-12-2010 11:16 PM
ls sorting problem: lower and upper case folded? mcwasi Linux - General 6 10-24-2007 10:26 AM
Having problem on sorting my files to the correct folder New2Linux06 Linux - General 3 11-04-2006 05:22 AM
sorting problem ltordsen Programming 2 11-02-2005 04:25 PM
File sorting problem Kerridis Linux - General 3 03-08-2004 11:44 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:40 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration