CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 22 of 22
  1. #16
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,923

    Re: C-String problem

    Quote Originally Posted by syzn View Post
    woah, thanks for all the info. I don't know some of the code both of you posted, but I'll figure it out. I haven't learned much yet, hence, the horrible code I made.
    How are you learning c++? You may like to look at these sites for more info
    http://www.learncpp.com/
    http://www.cplusplus.com/doc/tutorial/
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  2. #17
    Join Date
    Jun 2013
    Posts
    8

    Re: C-String problem

    I have a book called C++ Early Objects. I'll look into those links as well though, thank you.

  3. #18
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,923

    Re: C-String problem

    Quote Originally Posted by syzn View Post
    I have a book called C++ Early Objects. I'll look into those links as well though, thank you.
    Is that book the one by Gaddis, Walters & Muganda?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  4. #19
    Join Date
    Jun 2013
    Posts
    8

    Re: C-String problem

    Yes

  5. #20
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: C-String problem

    Quote Originally Posted by 2kaud View Post
    Code:
    void  lastChar(char const* str)
    {
    char const *const last = strchr(str, '\0');
    
    	(last > str) ? std::cout << *(last - 1) : std::cout << "null";
    }
    In addition to laserlight's post #10, I don't see any advantage in using the ternary operator here instead of a plain if-else. The result of the operation is discarded anyway.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  6. #21
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,923

    Re: C-String problem

    Quote Originally Posted by Eri523 View Post
    I don't see any advantage in using the ternary operator here instead of a plain if-else. The result of the operation is discarded anyway.
    Guilty as charged In mitigation I was trying to identify a polymorphism bug at the time and I originally wrote

    Code:
    //Don't try this at home
    std::cout << (last > str) ? *(last - 1) : "null";
    but this doesn't compile of course! (incompatible operand types) So I just quickly changed it to

    Code:
    (last > str) ? std::cout << *(last - 1) : std::cout << "null";
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  7. #22
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: C-String problem

    Quote Originally Posted by 2kaud View Post
    Code:
    //Don't try this at home
    std::cout << (last > str) ? *(last - 1) : "null";
    In fact I considered suggesting that very variation to make the ternary operator at least half-way useful here, but came to the conclusion that it wouldn't compile before finishing my post.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

Page 2 of 2 FirstFirst 12

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