Thread: c_str() truncating string (randomly?)

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    4

    c_str() truncating string (randomly?)

    Hi, Im currently trying to write a program to model the "Weasel" evolution model, described here: Weasel program - Wikipedia, the free encyclopedia

    This basically consists of setting a target phrase, then, starting from a completely random phrase, "evolving" that random phrase towards the target phrase
    The program itself seem to work fine, but I'm having a bit of difficulty with my output. Im using FLTK for the GUI, which cant handle normal strings, but requires converting them via
    Code:
    exmplstr.c_str()
    Im outputting a new phrase, in the form of a c-string every 0.01 seconds by appending it to a buffer, which is then output in a text box.
    My problem is that, seemingly randomly, when I use c_str(), the original string will become truncated, but this only ever happens where there is a space (not always the first space either):

    Str: "1 2 3 4 5 6 7 8 9 0 { }...etc" <-multiple spaces in string
    C-str "1 2 3 4 5"
    Since this seems to happen seemingly randomly, I am at a loss as to what it might be, and google wasn't much help!

    The particular code that converts the strings and adds them to the buffer is here:

    Code:
    cout << "\nGen Num: " << gn;
    e1.setphrases(e1.breed()); //Generates new phrases string o = "\n Best from generation "; o.append(intstr(gn)); //Append Number of generations thus far o.append(" is: "); cout << e1.getHiScr(); //Get random phrase closest to target phrase o.append(e1.getHiScr()); //Append this to o cout << "\nNrm: " << o; //Testing difference cout << "\n Cstr: " << o.c_str(); //between original and converted progress->value(e1.getScr(e1.getHiScr())); //Update progress bar phrsbuff->append(o.c_str()); //Convert string o created above to cstr, append to text buffer outputbox->buffer(phrsbuff); //Put buffer in output box outputbox->redraw(); //Refresh box outputbox->insert_position(outputbox->buffer()->length()); //Scroll output outputbox->show_insert_position(); //box down Fl::add_timeout(0.01, generate_cb);
    Sorry to dump a big wall of text. Ill attach all the code, as its easier to see what I mean if you see the program running. Also, I'm using Quincy: Simple free C/C++ programming IDE for Windows as an editor and compiler
    Thanks

    Chris
    Last edited by chris2306; 01-09-2011 at 04:28 PM. Reason: Add quincy and attatchment

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  2. Please check my C++
    By csonx_p in forum C++ Programming
    Replies: 263
    Last Post: 07-24-2008, 09:20 AM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM