> is printing as 62 and < is printing as 60

Discussion in 'C++' started by JustSomeGuy, Dec 5, 2004.

  1. JustSomeGuy

    JustSomeGuy Guest

    why would cout << '<' print 62
    and cout << '>' print 60
    Is there some flag that I have clobbered?
     
    JustSomeGuy, Dec 5, 2004
    #1
    1. Advertisements

  2. JustSomeGuy

    Sumit Rajan Guest


    Something like the following code gives you that output?


    #include <iostream>

    int main()
    {
    std::cout << '<';
    }
     
    Sumit Rajan, Dec 5, 2004
    #2
    1. Advertisements

  3. JustSomeGuy

    adbarnet Guest

    '<' is character code 62, and '>' is character code 60 - the stream operator
    just sees them as ints - cout << "<" would work.

    ad
     
    adbarnet, Dec 5, 2004
    #3
  4. JustSomeGuy wrote in in comp.lang.c++:
    It maybe that you are runing your compiler in some non-standard
    mode and the type of '<' is not char. There isn't AFAICT any
    flag you can set for std::eek:stream's that causes this to happen.

    #include <iostream>

    int main()
    {
    std::cout << '<' << (int)'<' << '\n';
    }

    The above outputs "<60\n" for me.

    Rob.
     
    Rob Williscroft, Dec 5, 2004
    #4
  5. JustSomeGuy

    JustSomeGuy Guest

    Sorry I obviously haven't posted all the code here..
    but when I try
    cout << "<"
    it prints out the address of the "<" string.
     
    JustSomeGuy, Dec 5, 2004
    #5
  6. JustSomeGuy wrote in in
    comp.lang.c++:
    Are you including <ostream> ?

    #include <iostream> /* for std::cout */
    #include <ostream> /* for the << operators & std::endl */

    int main()
    {
    std::cout << '<' << "<" << std::endl;
    }

    Rob.
     
    Rob Williscroft, Dec 5, 2004
    #6

  7. I"m new here and this may be an obvious statement, but what it looks like
    it's priniting is the ascii values for "<" and ">"
     
    Jeremy A. Smith, Dec 5, 2004
    #7
  8. JustSomeGuy

    JustSomeGuy Guest

    Yes exactly.. it is treating all 'x' or '<' or what ever is single quoated
    as a
    integer rather than a character... it is therefor printing the ascii code
    for the character.
     
    JustSomeGuy, Dec 5, 2004
    #8
  9. JustSomeGuy

    msalters Guest

    That's the obvious giveaway. You're getting the member
    operator<<(void*) not the non-member operator(char*) because
    your <iostream> does not include <ostream> (that's legal, but
    uncommon - hence many people forget <ostream> )
    Regards,
    Michiel Salters
     
    msalters, Dec 7, 2004
    #9
  10. JustSomeGuy

    msalters Guest

    That's the obvious giveaway. You're getting the member
    operator<<(void*) not the non-member operator(char*) because
    your <iostream> does not include <ostream> (that's legal, but
    uncommon - hence many people forget <ostream> )
    Regards,
    Michiel Salters
     
    msalters, Dec 7, 2004
    #10
  11. JustSomeGuy

    JustSomeGuy Guest

    Ok I tried including <ostream> that didn't help...
    however delving deeper into this class I found that it is inheriting from
    std::streambuf and has a overflow method... and I'm thinking that somehow
    to_char_type is not being called somehow.

    int_type overflow(int_type ch)
    {
    if (traits_type::not_eof(ch))
    {
    if (at_start)
    {
    for (int i = 0; i < level; ++i)
    {
    if (log_on)
    {
    buffer->sputc(traits_type::to_char_type('\t'));
    }
    }
    }
    if (log_on)
    buffer->sputc(traits_type::to_char_type(ch));
    if (traits_type::eq_int_type(ch, traits_type::to_int_type('\n')))
    {
    at_start = true;
    }
    else
    {
    at_start = false;
    }
    }
    return ch;
    }
     
    JustSomeGuy, Dec 8, 2004
    #11
  12. JustSomeGuy

    Arijit Guest

    You have to include ostream separately ?! I didn't know that. I have
    never had to do it even once. And I have used quite a few compilers.
    Plain lucky ?

    -Arijit
     
    Arijit, Dec 8, 2004
    #12
  13. Yes.


    Jonathan
     
    Jonathan Mcdougall, Dec 9, 2004
    #13
  14. JustSomeGuy

    msalters Guest

    The hard part of bugfixing is when you have more than one bug.
    Anyway, you must have mistracked somewhere. ostream doesn't have
    an IS-A relation with streambut, but a HAS-A. This is important,
    because you can replace that streambuf at runtime. std::cout
    will have a special one that streams to the screen.

    Anyway, the tricky part with the int_type is to deal with the number
    of possible results when trying to get a character. There are
    typically 257 results: either one of 256 characters, or EOF (-1).
    That won't fit in an char, so an int is used. Streams are templatized
    (work on wchar as well) and that's why you can't use int directly.
    Anyway, the mapping from int to char (and to wchar_t from whatever
    it maps from) is done by to_char_type. That is pretty much a no-op,
    and I can't imagine that failing.

    Regards,
    Michiel Salters
     
    msalters, Dec 9, 2004
    #14
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.