Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Incorrect Output

57 views
Skip to first unread message

MikeCopeland

unread,
Oct 26, 2015, 12:32:29 AM10/26/15
to
The following code doesn't work (as I want it to). Specifically, it
produces "20367c", whereas I want it to yield "203Cc". For some reason,
the "toupper(char(cz))" is yeilding 67, not C. What am I doing wrong?
Please advise. TIA

ostringstream ossz;
int nx1 = 2;
char cz = char(nx1+'a');
ossz.str(""), ossz << "2" << setfill('0') << setw(2)
<< (nx1+1) << toupper(char(cz))
<< char(cz);

---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus

MikeCopeland

unread,
Oct 26, 2015, 1:55:17 AM10/26/15
to
In article <C-201510...@ram.dialup.fu-berlin.de>, r...@zedat.fu-
berlin.de says...
> >the "toupper(char(cz))" is yeilding 67, not C. What am I doing
wrong?
>
> 67 /is/ 'C'! (Assuming ASCII)

Indeed it is, but I want the output to be "C", not "67".
Is my cast operation wrong?

Robert Wessel

unread,
Oct 26, 2015, 2:37:33 AM10/26/15
to
On Sun, 25 Oct 2015 22:54:59 -0700, MikeCopeland <mrc...@cox.net>
wrote:

>In article <C-201510...@ram.dialup.fu-berlin.de>, r...@zedat.fu-
>berlin.de says...
>> >the "toupper(char(cz))" is yeilding 67, not C. What am I doing
>wrong?
>>
>> 67 /is/ 'C'! (Assuming ASCII)
>
> Indeed it is, but I want the output to be "C", not "67".
> Is my cast operation wrong?
>
>ostringstream ossz;
>int nx1 = 2;
>char cz = char(nx1+'a');
>ossz.str(""), ossz << "2" << setfill('0') << setw(2)
> << (nx1+1) << toupper(char(cz))
> << char(cz);


toupper() returns an int. Feeding an int to a stream will result in a
number being emitted. Cast the output of toupper().

Paavo Helde

unread,
Oct 26, 2015, 2:43:48 AM10/26/15
to
MikeCopeland <mrc...@cox.net> wrote in news:MPG.30976e1acf2e5c9e9896b6
@news.eternal-september.org:

> In article <C-201510...@ram.dialup.fu-berlin.de>, r...@zedat.fu-
> berlin.de says...
>> >the "toupper(char(cz))" is yeilding 67, not C. What am I doing
> wrong?
>>
>> 67 /is/ 'C'! (Assuming ASCII)
>
> Indeed it is, but I want the output to be "C", not "67".
> Is my cast operation wrong?

Yes, cz is already a char, so there is no point to cast it to char again.
On the other hand, result of toupper() is an int and needs to be somehow
converted back to char. I.e. instead of

toupper(char(cz))

you need

char(toupper(cz)).


>
> ostringstream ossz;
> int nx1 = 2;
> char cz = char(nx1+'a');
> ossz.str(""), ossz << "2" << setfill('0') << setw(2)
> << (nx1+1) << toupper(char(cz))
> << char(cz);
>

PS. Note that toupper() behavior is undefined for most negative input
values (and char is signed in common implementations), so if you are not
100% sure your texts are ASCII-only one should better check for that or
at least cast cz to unsigned char before passing to toupper.

FredK

unread,
Oct 27, 2015, 10:47:52 AM10/27/15
to
Note the important disclaimer that MikeCopeland added: "Assuming ASCII".
There is no guarantee that the letters are in sequential order; there are many character sets where 'a'+2 might not be 'c'.

Richard

unread,
Oct 27, 2015, 1:37:58 PM10/27/15
to
[Please do not mail me a copy of your followup]

MikeCopeland <mrc...@cox.net> spake the secret code
<MPG.30975ab85...@news.eternal-september.org> thusly:

> The following code doesn't work (as I want it to). Specifically, it
>produces "20367c", whereas I want it to yield "203Cc". For some reason,
>the "toupper(char(cz))" is yeilding 67, not C. What am I doing wrong?

toupper returns an int, not a char.

<http://en.cppreference.com/w/cpp/string/byte/toupper>
--
"The Direct3D Graphics Pipeline" free book <http://tinyurl.com/d3d-pipeline>
The Computer Graphics Museum <http://computergraphicsmuseum.org>
The Terminals Wiki <http://terminals.classiccmp.org>
Legalize Adulthood! (my blog) <http://legalizeadulthood.wordpress.com>

Paavo Helde

unread,
Oct 27, 2015, 3:25:38 PM10/27/15
to
FredK <fred.l.kl...@gmail.com> wrote in
news:0563ef0d-aa48-4ef1...@googlegroups.com:
This was added by Stefan Ram.

> There is no guarantee that the letters are in sequential
> order; there are many character sets where 'a'+2 might not be 'c'.

I bet Mike Copeland will never get in touch with those character sets. On
the other hand, no common systems are using plain ASCII nowadays, they
are all extended ASCII-based charsets like iso-latin-1 or UTF-8. In this
case 'a'+2 will still equal 'c', but passing a char representing e.g. a-
umlaut to toupper() may easily crash the program.

Cheers
Paavo
0 new messages