CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Junky CStrings

  1. #1
    Join Date
    Mar 2005
    Posts
    6

    Junky CStrings

    Hi - do you guys mind helping me again (I really tried my best to figure this out)

    I've got the following code:

    CString s;
    s = " Hello ";
    cout << s ; // junk value like 5f4dob14
    char * b = "bob";
    cout << b; // prints 'bob'
    s=b;
    cout << s; // new junk value like 002f2191c

    I've been looking at my texbook; I can't see what I'm doing differently that would lead to these errors. I've also tried changing the project settings from MFC in a shared dll to MFC in a static library (in both debug and not debug versions) and the junk persists.

    (note: for a little while I got some liking errors but I did something (like opening and closing the project) and haven't gotten those errors since. They said something about dlls and symbols not found. Sorry for not having more specific info on that.)

    ps: thanks for the explanations about <iostream>. I'm using Visual Studio 6, but I'll stick with the new standards.

  2. #2
    Join Date
    Sep 2002
    Location
    14° 39'19.65"N / 121° 1'44.34"E
    Posts
    9,815

    Re: Junky CStrings

    Quote Originally Posted by Layla Nahar
    CString s;
    s = " Hello ";
    cout << s ; // junk value like 5f4dob14
    That's because iostream doesn't know CString. You have to cast the CString to a type known by the standard library, for example, const char* (or LPCTSTR):
    Code:
    CString s = "Hello";
    cout << (LPCTSTR)s;
    Note: This works since CString has an LPCTSTR conversion operator which is invoked by the cast. It gives read-only access to CString's internal character buffer.

  3. #3
    Join Date
    Mar 2005
    Location
    Germany
    Posts
    29

    Re: Junky CStrings

    probably cout only works with plain strings.

    try
    cout << (LPCTSTR) s;

    the cast makes CString to expose its buffer as a zero-terminated string.

  4. #4
    Join Date
    Mar 2005
    Posts
    6

    Re: Junky CStrings

    thank you. that works.

    I have another question, though, as a result. How can I avoid typing (LPCTSTR) before every CString in a cout statement? Is there something I can put in the header, or some kind of macro I can make?

    Also, how do I read the "CODE" sections of posts here? I see a long horizontal box below the word CODE - but I can't select/highlight it ...

  5. #5
    Join Date
    Nov 2001
    Posts
    323

    Re: Junky CStrings

    Instead of using MFC's CString class
    you can use the STL string class. Just #include <string>

    and you do something like this

    Code:
    string x;
    x = " Hi there! ";
    cout << x.c_str();
    Best Regards,

    --Zim
    If you find this post useful, please rate it.
    _________________________________
    "Have you the brain worms?!?!?"

  6. #6
    Join Date
    Sep 2002
    Location
    14° 39'19.65"N / 121° 1'44.34"E
    Posts
    9,815

    Re: Junky CStrings

    Quote Originally Posted by Layla Nahar
    I have another question, though, as a result. How can I avoid typing (LPCTSTR) before every CString in a cout statement? Is there something I can put in the header, or some kind of macro I can make?
    Not really... the problem is that you are mixing two class libraries: The C++ standard library, which has its own data type (std::string), and MFC, where CString belongs to.
    Since you are using cout, you seem to be writing a console application - so I don't see why you would be using CString instead of std::string. MFC is an an application framework for Windows applications with a GUI, where you wouldn't use cout anyway.

    Quote Originally Posted by Layla Nahar
    Also, how do I read the "CODE" sections of posts here? I see a long horizontal box below the word CODE - but I can't select/highlight it ...
    Not sure what you mean by "read"... that box contains text formatted as code (that is, with a fixed space font). You can select the text inside that box and copy it.

  7. #7
    Join Date
    Mar 2005
    Posts
    6

    Re: Junky CStrings

    Hi Gsterken,

    Thanks for the isight about mixing libraries. I'm using a textbook that starts off with just the console app - after a few chapters I won't be doing that anymore. I guess when the guy wrote it, he didn't need to do that (LPCTSTR) hack ...

    on the topic of CODE tags - on my browser I don't see ANY text in the box below the word "Code:" - I thought maybe it was a link and I had to do something to enable the link ...

    If I select the quote button I can read what's in the tags...

    ps: This is really cool - If I use Safari, I can see the text in the text boxes, but I can't see the "Edit" button on my own posts! :P
    Last edited by Layla Nahar; March 3rd, 2005 at 02:04 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured