Printing String Issues

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • - Steve -

    Printing String Issues

    I have a class that has a private member char *buf which is basically a
    pointer to a string.

    I have a member function called print(); which consists of

    void String::print()
    {
    cout << name;
    cout << ":\t\"";
    cout << buf;
    cout << "\"\t" << length << " characters"<< endl;
    }

    But the cout << buf prints out maybe half of the string. When gives a weird
    symbol (usually a Spades symbol), and then continues on it's merry way. But
    if I use the debugger (Visual Studio 2002) it prints correctly.


  • Alf P. Steinbach

    #2
    Re: Printing String Issues

    On Fri, 01 Aug 2003 00:23:26 GMT, "- Steve -" <sevans@foundat ion.sdsu.edu> wrote:
    [color=blue]
    >I have a class that has a private member char *buf which is basically a
    >pointer to a string.
    >
    >I have a member function called print(); which consists of
    >
    >void String::print()
    >{
    > cout << name;
    > cout << ":\t\"";
    > cout << buf;
    > cout << "\"\t" << length << " characters"<< endl;
    >}[/color]

    Presumably a debug helper function.

    [color=blue]
    >But the cout << buf prints out maybe half of the string. When gives a weird
    >symbol (usually a Spades symbol), and then continues on it's merry way. But
    >if I use the debugger (Visual Studio 2002) it prints correctly.[/color]

    It's not zero terminated.

    Use

    cout << std::string( buf, buf+length )

    or something like that -- check the std::string constructors.


    Comment

    • John Harrison

      #3
      Re: Printing String Issues


      "- Steve -" <sevans@foundat ion.sdsu.edu> wrote in message
      news:e0c9311ba0 108c4728925eeee 677261b@free.te ranews.com...[color=blue]
      > I have a class that has a private member char *buf which is basically a
      > pointer to a string.
      >
      > I have a member function called print(); which consists of
      >
      > void String::print()
      > {
      > cout << name;
      > cout << ":\t\"";
      > cout << buf;
      > cout << "\"\t" << length << " characters"<< endl;
      > }
      >
      > But the cout << buf prints out maybe half of the string. When gives a[/color]
      weird[color=blue]
      > symbol (usually a Spades symbol), and then continues on it's merry way.[/color]
      But[color=blue]
      > if I use the debugger (Visual Studio 2002) it prints correctly.
      >[/color]

      Well that means you have a bug in your program. Unfortunately is not
      possible to tell what that is from the above code. Bugs which only show up
      in release versions are the bane of many programmers lives. Like bugs which
      only show up on customers machines, or bugs which only show when there is a
      full moon, there all still bugs.


      john


      Comment

      Working...