cant get sort program to work


Im working on this program for class. I have an idea on were the problem lies, but i dont know how to begin fixing it. i beleave the problem is in the void functions at the bottom. it needs to be changed around, but i dont know were or what to change. does anyone have any suggestons? Here is the code i have so far. the program origionaly sorted numbers into numeric order. no i want it set up to put words in alphabetical order. this program is set up to take four names (all the stooges) and put them in alphabetical order.


#include <iostream>
#include <string.h>
#include <ctype.h>
using namespace std;


void sort(string n); // change int to string
void swap(string*p1, string*p2); // change int to string
string a[4]; // change int to string

int main ()
{
int i; // i is a counting varable
for (i = 0; i < 4; i++)
{
cout << "Enter stooge names" << i << ":";
getline(cin, a[i]); //change cin >> a[i]; to getline(cin, a[i]);
}

sort(4);
cout <<"\nHere are all the stooge names, sorted:\n" << endl;

for (i = 0; i < 4; i++)
cout << sort[i] << "";

cout <<"\n\n";
system("Pause");
return 0;
}

void sort(string n)
{
int i, j, low;
for (i = 0; i < n - 1; i++)
{
low = i;
for (j = i + 1; j < n; j++)
if (a[j] < a[low])
low = j;

if (i != low)
swap(&a[i], &a[low]);
}
}
void swap(string*p1, string*p2)
{
string temp = *p1;
*p1 = *p2;
*p2 = temp;
}
Last edited on
Well, to start with you are calling sort with an int when it's expecting a string:

1
2
3
void sort(string n);

sort(4);


Also what is this?

cout << sort[i] << "";

I assume you were trying to output the values in the array a?

There are other issues, but start there.
so turn sort (4) into int?

the cout << sort[i] << ""; was susposed to be a, guess i missed that. thanks man.
Are you using std::strings or char arrays. If std::strings, its #include <string> not #include <string.h>
i was told it would work with either or. im not sure what the difference is with string and string.h? is that just a library refference?
Topic archived. No new replies allowed.