View Single Post
  #2 (permalink)  
Old June 21st, 2003, 11:07 PM
Ammiel Ammiel is offline
Authorized User
 
Join Date: Jun 2003
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to Ammiel Send a message via AIM to Ammiel Send a message via MSN to Ammiel
Default

Hi odie,

I typed up the example on Microsoft Visual C++ 6.0 and it didn't give me any errors, and the example worked as shown. Perhaps your using a different compiler? Likeliness is your using a GCC relative such as MingW if your on Windows... I'll try compiling in Dev-C++ and post the results.

Yep, the code seems to be 100% portable, you probably missed a line of code or something.

Code:
#include <iostream>
#include <string>

using namespace std;

int main()
{
    string text;
    const string separators = " ,.\"\n";
    const int max_words = 1000;
    string words[max_words];
    string *pwords[max_words];

    cout <<  endl << "Enter a string terminated by #:" << endl;
    getline(cin, text, '#');

    int start = text.find_first_not_of(separators);
    int end = 0;
    int word_count = 0;
    while(start != string::npos && word_count < max_words)
    {
        end = text.find_first_of(separators, start + 1);
        if (end == string::npos)
            end = text.length();
        words[word_count] = text.substr(start, end - start);
        pwords[word_count] = &words[word_count];
        word_count++;

        start = text.find_first_not_of(separators, end + 1);
    }

    int lowest = 0;

    for(int j = 0; j < word_count - 1; j++)
    {
        lowest = j;

        for(int i = j + 1; i < word_count; i++)
            if(*pwords[i] < *pwords[lowest])
                lowest = i;

        if(lowest != j)
        {
            string* ptemp = pwords[j];
            pwords[j] = pwords[lowest];
            pwords[lowest] = ptemp;
        }
    }

    for(int i = 0; i < word_count ; i++)
        cout << endl << *pwords[i];

    cout << endl;
    return 0;
}
Of course....that's without comments :)