> is printing as 62 and < is printing as 60

J

JustSomeGuy

why would cout << '<' print 62
and cout << '>' print 60
Is there some flag that I have clobbered?
 
S

Sumit Rajan

JustSomeGuy said:
why would cout << '<' print 62
and cout << '>' print 60
Is there some flag that I have clobbered?


Something like the following code gives you that output?


#include <iostream>

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

adbarnet

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

ad
 
R

Rob Williscroft

JustSomeGuy wrote in in comp.lang.c++:
why would cout << '<' print 62
and cout << '>' print 60
Is there some flag that I have clobbered?

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.
 
J

JustSomeGuy

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

ad

Sorry I obviously haven't posted all the code here..
but when I try
cout << "<"
it prints out the address of the "<" string.
 
R

Rob Williscroft

JustSomeGuy wrote in in
comp.lang.c++:
Sorry I obviously haven't posted all the code here..
but when I try
cout << "<"
it prints out the address of the "<" string.

Are you including <ostream> ?

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

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

Rob.
 
J

Jeremy A. Smith

JustSomeGuy said:
why would cout << '<' print 62
and cout << '>' print 60
Is there some flag that I have clobbered?


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 ">"
 
J

JustSomeGuy

Jeremy A. Smith said:
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 ">"

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.
 
M

msalters

JustSomeGuy said:
Sorry I obviously haven't posted all the code here..
but when I try
cout << "<"
it prints out the address of the "<" string.

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
 
M

msalters

JustSomeGuy said:
Sorry I obviously haven't posted all the code here..
but when I try
cout << "<"
it prints out the address of the "<" string.

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
 
J

JustSomeGuy

msalters said:
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

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;
}
 
A

Arijit

Rob said:
Are you including <ostream> ?

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

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
 
J

Jonathan Mcdougall

Arijit said:
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 ?

Yes.


Jonathan
 
M

msalters

JustSomeGuy said:
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.

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
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top