func HELP !!

OK, OK I know... it's obvious, but HELP!
This code doesn't do anything, although it compiles.
It's very simple, it is supposed to sort a user entered string in alpha order.

#include
#include
using namespace std;

void alpha (string &s);

int main()
{
string text;
cout<<"Please enter a string: ";
cin >> text;

alpha (text);

cout <<"Alphabetized: " ;
return 0;
}

void alpha (string &s)
{
string text;
int str_length = text.size();

getline(cin, text);
for (int i = 0; i<str_length; i++)
{
for (int j=i+1;j<str_length ;j++ )
{
if (s[i] != ' ' || s[j] != ' ')
{
for (int k = j; k < str_length; k++)
{
if (s[k] < s[j])
cout << s[j];
else
cout << s[k];
}
}
}
}
}

Comments

  • IT WILL NOW WORK BUT might not give u the required output
    check your algorithm
    Khan

    : OK, OK I know... it's obvious, but HELP!
    : This code doesn't do anything, although it compiles.
    : It's very simple, it is supposed to sort a user entered string in alpha order.
    :
    : #include
    : #include
    : using namespace std;
    :
    : void alpha (string &s);
    :
    : int main()
    : {
    : string text;
    : cout<<"Please enter a string: ";
    : cin >> text;
    :
    : alpha (text);
    :
    : cout <<"Alphabetized: " ;
    : return 0;
    : }
    :
    : void alpha (string &s)
    : {
    : string text;
    : [red]int str_length = text.size();[/red]
    : int str_length = s.size();
    : getline(cin, text);
    : for (int i = 0; i<str_length; i++)
    : {
    : for (int j=i+1;j<str_length ;j++ )
    : {
    : if (s[i] != ' ' || s[j] != ' ')
    : {
    : for (int k = j; k < str_length; k++)
    : {
    : if (s[k] < s[j])
    : cout << s[j];
    : else
    : cout << s[k];
    : }
    : }
    : }
    : }
    : }
    :

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion