Wrox Programmer Forums
|
All Other Wrox Books Do you have a question about a Wrox book that isn't listed anywhere on p2p.wrox.com or where the forum is locked? Here's a forum to post questions about any other Wrox book so that other readers or one of the authors can help you with your questions. IF YOU ARE LOOKING FOR CODE DO NOT ASK "Where can I find the code for this book?" That question is answered here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the All Other Wrox Books section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old June 4th, 2003, 11:43 AM
Registered User
 
Join Date: Jun 2003
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default Ivor Horton's Beginning C++

Hi all,
Program 7.5 - Page 244
it compiles but it returns undesirable results.


Typos checked a million times and it has none.
Anybody experienced this problem before?

Thanks in advance.
  #2 (permalink)  
Old June 21st, 2003, 11:07 PM
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 :)
  #3 (permalink)  
Old June 28th, 2003, 05:48 PM
Registered User
 
Join Date: Jun 2003
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the reply...
After moving on I decided to go back and try it again...
voila.. it worked.. You're right, I had missed few things...
Now I back on the ball...

Thanks again:)

Odie





Similar Threads
Thread Thread Starter Forum Replies Last Post
Ivor Horton's Beginning C++ Firedrill C++ Programming 18 December 2nd, 2008 06:32 PM
Answers for Ivor Horton's Beginning C++ 7stud All Other Wrox Books 4 December 15th, 2006 10:59 AM
Where"Ivor Horton's Beginning Java 2" execiseAnswe yuanhuajie BOOK: Beginning Java 2, JDK 5 Edition 2 June 11th, 2006 10:34 PM
Where"Ivor Horton's Beginning Java 2" execiseAnswe yuanhuajie Java Basics 0 June 11th, 2006 09:47 PM
Errata for Ivor Horton's Beginning C++? 7stud C++ Programming 3 August 28th, 2003 05:51 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.