Trying to run a bubble sort on a simple, small list of 7 names from a text file read in, stored as string values. It seems to have sorted the list correctly, but it is taking the first name in the list and printing as the last, side by side with the last name in the list. I cannot figure out why it is doing this, could anyone took a look at it.
HERE IS THE LIST OF NAMES FROM THE TEXT FILE
Mark
Adam
Rick
Teddy
Marcus
Jerry
Brian
Matthew
void ballClub::bubbleSort(string list[], int length)
{
string temp;
int iteration;
int index;
for (iteration = 1; iteration < length; iteration++)
{
for (index = 0; index < length - iteration; index++)
{
if (list[index] > list[index + 1])
{
temp = list[index];
list[index] = list[index + 1];
list[index + 1] = temp;
}
}
}
}
void ballClub::print(string list[], int length)
{
for (int i = 0; i < length; i++)
{
cout << list[i];
}
}
PROGRAM OUTPUT:
// Also is leaving a blank space above the name Adam
Adam
Brian
Jerry
Marcus
Matthew
Rick
Teddy Mark //not sure why it printing that side by side
Just looking at the code you have posted is not enough. With out all of your code I do not know where it is going wrong. Too many places that could be a problem. If it is not a problem post your whole code so I do not have t guess at what you are doing.
I put some code together to test your functions. I do not think what I have is anything like what you have written, but I did get the program I made to work.
Where I thought changing line 6 "iteration = 1" to "iteration = 0" did not work. I added a "\n" to the end of cout on line 23.
The program I have works, but I did not have the problems that you have described.
Notice that ballClub::print() doesn't print a new line, yet the output contains a new line after each name. That means that the names themselves contain a newline. I suspect that "Teddy" is the only one that doesn't.
Try changing line 23 to cout << list[i] << '\n'; and then print the list both before and after calling bubbleSort.
dhayden, I tried what you recommended, all that did was put a new line in between my output, same problem though. Handy Andy I will post all my code, I think the problem must be how I was reading in the characters from the file.
#include "ballClub.h"
#include <iostream>
#include <string>
#include <cstring>
#include <fstream>
usingnamespace std;
/***************************************************************************************/
void ballClub::getFile(string list[], int &length)
{
ifstream myFile;
string total;
int loc = 0;
char r;
bool done = true;
char fileName[100];
// OBTAIN THE LOCATION OF THE FILE AND MAKE SURE THAT IT OPENED CORRECTLY
do
{
cout << "Enter the file you would like to open: " << endl;
cin >> fileName;
myFile.open(fileName);
if (myFile.is_open())
cout << "File opened correctly " << endl;
else
cerr << "Error, please try again " << endl;
} while (!myFile);
// READ THE FILE AND ALL OF IT'S ENTRIES
while(done) //Use of a SENTINEL VALUE
{
if(!myFile.eof()) // If it has not reached the end of the file
{
myFile.get(r);
if(r == '\n' || myFile.eof())
{
list[loc] = total;
loc++;
length++;
total.clear();
}
total += r;
}
else
done = false;
}
/*****************************************************************************************/
Okay, the problem isn't that each name has a newline after it: it's that (almost) each name has a newline before it. The only exception is "Mark" because it's the first name in the list. The names are sorted correctly given this. To make it clearer, here are the sorted names with the newline printed as \n"
\nAdam
\nBrian
\nJerry
\nMarcus
\nMatthew
\nRick
\nTeddy
Mark
They have the newline because line 47 of your last post always adds the character that was read, even if it's a newline. To fix this, put that code inside an else: