By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,411 Members | 999 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,411 IT Pros & Developers. It's quick & easy.

ios::uppercase does not work

P: n/a
My ios::uppercase (and others) seems not to work.

#include <iostream>
using namespace std;
int main(){
int num = 45;
cout.setf(ios::hex | ios::showbase | ios::uppercase);
cout << "number " << num << endl;
return 0;
}

Shouldn't the output be
NUMBER 0X2D ???

Compiled with my g++ 3.3.1, the output is just a very boring
number 45

How can I get an uppercase output?

Thanks
Markus
Jul 22 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Markus Dehmann wrote:
My ios::uppercase (and others) seems not to work.
How do you know ios::uppercase doesn't work, if you're not outputting
letters?
#include <iostream>
You should also have

#include <ios>

For the ios flags, and

#include <ostream>

for endl.
using namespace std;
int main(){
int num = 45;
cout.setf(ios::hex | ios::showbase | ios::uppercase);


Setting the ios::hex flag doesn't unset the ios::dec flag, and if more than
one base flag is set, decimal is used as the base. You could add this line:

cout.unsetf(ios::dec);

Or you could use manipulators instead:

cout << hex << showbase << uppercase << number << num << endl;

--
Russell Hanneken
eu*******@cbobk.pbz
Use ROT13 to decode my email address.
Jul 22 '05 #2

P: n/a

"Markus Dehmann" <ma*******@gmx.de> wrote in message
news:op**************@news.cis.dfn.de...
My ios::uppercase (and others) seems not to work.

#include <iostream>
using namespace std;
int main(){
int num = 45;
cout.setf(ios::hex | ios::showbase | ios::uppercase);
cout << "number " << num << endl;
return 0;
}

Shouldn't the output be
NUMBER 0X2D ???
You need to toggle the std::ios bits as mentioned in Russell's post. Done
with std::ios::basefield flag and the 2 arguement setf.
Try:

#include <iostream>

int main()
{
int num = 45;
std::cout.setf( std::ios::hex, std::ios::basefield);
std::cout.setf( std::ios::showbase );
std::cout.setf( std::ios::uppercase );
std::cout << "number " << num << std::endl;
return 0;
}

number 0X2D

Compiled with my g++ 3.3.1, the output is just a very boring
number 45

How can I get an uppercase output?

Thanks
Markus

Jul 22 '05 #3

P: n/a
On Sun, 27 Jun 2004 00:02:13 -0400, SaltPeter <Sa*******@Jupiter.sys>
wrote:

"Markus Dehmann" <ma*******@gmx.de> wrote in message
news:op**************@news.cis.dfn.de...
My ios::uppercase (and others) seems not to work.

#include <iostream>
using namespace std;
int main(){
int num = 45;
cout.setf(ios::hex | ios::showbase | ios::uppercase);
cout << "number " << num << endl;
return 0;
}

Shouldn't the output be
NUMBER 0X2D ???


You need to toggle the std::ios bits as mentioned in Russell's post. Done
with std::ios::basefield flag and the 2 arguement setf.
Try:

#include <iostream>

int main()
{
int num = 45;
std::cout.setf( std::ios::hex, std::ios::basefield);
std::cout.setf( std::ios::showbase );
std::cout.setf( std::ios::uppercase );
std::cout << "number " << num << std::endl;
return 0;
}

number 0X2D


Thanks. I was actually most interested in a general uppercase output
("NUMBER"). So, how can I print a lowercase string uppercase?

std::string test = "number";
std::cout << std::ios::uppercase << test; // want NUMBER, but don't get it

Markus

Jul 22 '05 #4

P: n/a
>
Thanks. I was actually most interested in a general uppercase output
("NUMBER"). So, how can I print a lowercase string uppercase?

std::string test = "number";
std::cout << std::ios::uppercase << test; // want NUMBER, but don't get it


Convert your string to uppercase first, or write a streambuf wrapper that
does that for you.

There is no manipulator that will do that for you, presumably because
uppercase is a locale specific concept.

john
Jul 22 '05 #5

P: n/a
"John Harrison" <jo*************@hotmail.com> wrote in message
std::string test = "number";
std::cout << std::ios::uppercase << test; // want NUMBER, but don't get it


Convert your string to uppercase first, or write a streambuf wrapper that
does that for you.

There is no manipulator that will do that for you, presumably because
uppercase is a locale specific concept.


Isn't it a bug of the implementation that it does not respect the uppercase
flag in printing strings and const char arrays? Or maybe we have to imbue
some locale in the cout before printing?
Jul 22 '05 #6

P: n/a

"Siemel Naran" <Si*********@REMOVE.att.net> wrote in message
news:0h*********************@bgtnsc04-news.ops.worldnet.att.net...
"John Harrison" <jo*************@hotmail.com> wrote in message
std::string test = "number";
std::cout << std::ios::uppercase << test; // want NUMBER, but don't
get
it
Convert your string to uppercase first, or write a streambuf wrapper that does that for you.

There is no manipulator that will do that for you, presumably because
uppercase is a locale specific concept.


Isn't it a bug of the implementation that it does not respect the

uppercase flag in printing strings and const char arrays? Or maybe we have to imbue
some locale in the cout before printing?


ios::uppercase affects only integers printed in hexadecimal and the exponent
of floating point numbers printed in scientific notation.

I don't think locales are meant to be used for this purpose. A locale could
specify *how* to convert from uppercase to lowercase but I think it would be
stretching the meaning of a locale to make it actually do that conversion.

If I had to do this a lot I would write a streambuf wrapper. This is only
minimally tested, and probably needs fleshing out to be a complete
implementation.

#include <streambuf>
#include <iostream>
#include <cctype>
using namespace std;

class ToUpperStreamBuf : public streambuf
{
public:
ToUpperStreamBuf(streambuf* wrapped) : _wrapped(wrapped)
{
}

protected:
int_type overflow(int_type ch)
{
if (traits_type::not_eof(ch))
ch = _wrapped->sputc(toupper(traits_type::to_char_type(ch)));
return ch;
}
int sync()
{
return _wrapped->pubsync();
}

private:
streambuf* _wrapped;
};

int main()
{
// replace cout stream buffer
streambuf* old_buf = cout.rdbuf();
ToUpperStreamBuf toupper_buf(old_buf);
cout.rdbuf(&toupper_buf);

// now cout will print in uppercase
cout << "hello\n";

// restore cout stream buffer
cout.rdbuf(old_buf);
}

john
Jul 22 '05 #7

P: n/a
"John Harrison" <jo*************@hotmail.com> wrote in message
"Siemel Naran" <Si*********@REMOVE.att.net> wrote in message
Isn't it a bug of the implementation that it does not respect the
uppercase flag in printing strings and const char arrays?
Or maybe we have to imbue some locale in the cout before printing?


ios::uppercase affects only integers printed in hexadecimal and the

exponent of floating point numbers printed in scientific notation.
The standard says in the chapter on Input/output library: "replaces certain
lowercase letters with their uppercase equivalents in generated output".
Could they be any more vague? But the chapter on locales says the flag is
only used in num_get and num_put, that is for integers.
I don't think locales are meant to be used for this purpose. A locale could specify *how* to convert from uppercase to lowercase but I think it would be stretching the meaning of a locale to make it actually do that conversion.

If I had to do this a lot I would write a streambuf wrapper. This is only
minimally tested, and probably needs fleshing out to be a complete
implementation.

#include <streambuf>
#include <iostream>
#include <cctype>
using namespace std;

class ToUpperStreamBuf : public streambuf
{
public:
ToUpperStreamBuf(streambuf* wrapped) : _wrapped(wrapped)
{
}
What is variable 'wrapped' for? OK, I get it, so you can use this class
with a filebuf, iostreambuf, streambuf, etc.
protected:
int_type overflow(int_type ch)
{
if (traits_type::not_eof(ch))
ch = _wrapped->sputc(toupper(traits_type::to_char_type(ch)));
return ch;
}
Don't you have to uppercase all the chars in [eback(), egptr()) too? I'd
then call _wrapped->overflow(ch). Also, the streambuf has a locale in it so
one could call getloc(), then toupper(c, loc).
int sync()
{
return _wrapped->pubsync();
}

private:
streambuf* _wrapped;
};
One should probably forward all virtual functions to _wrapped. An
alternative design pattern I've used for this sort of thing is templates.

template <class Streambuf>
class ToUpperStreamBuf : public Streambuf {
public:
typedef typename Streambuf::int_type int_type;
ToUpperStreamBuf();
protected:
int_type overflow(int_type ch);
};

But wait! There's a problem because we don't know the type of cout.rdbuf(),
as it's implementation defined, and usually some special class derived from
streambuf.
int main()
{
// replace cout stream buffer
streambuf* old_buf = cout.rdbuf();
ToUpperStreamBuf toupper_buf(old_buf);
cout.rdbuf(&toupper_buf);

// now cout will print in uppercase
cout << "hello\n";

// restore cout stream buffer
cout.rdbuf(old_buf);
}


The stream method seems too big for most purposes, and forces uppercase all
the time. How about writing a class-style manipulator, which I've used in
the past?

class ToUpper {
public:
ToUpper(const char *);
private:
some data variable here;
};

std::ostream& operator<<(std::ostream&, ToUpper);

It would be nice if in istream and ostream they had a virtual function that
returned a sentry object, and the default behavior would be to return the
sentry as they currently have (that for istream ignores whitespace). Then
users of derived ostreams could change the virtual function to return a
sentry class derived from the base class one. Presumably it would do
additional processing. One minor problem: the sentry's copy constructer is
private.

class myostream : public std::ostream {
public:
class mysentry : public std::ostream::sentry { ... };
virtual /*override*/
mysentry getsentry() const;
};

What do you think of this design pattern in general?
Jul 22 '05 #8

P: n/a

"Siemel Naran" <Si*********@REMOVE.att.net> wrote in message
news:re*********************@bgtnsc04-news.ops.worldnet.att.net...
"John Harrison" <jo*************@hotmail.com> wrote in message
"Siemel Naran" <Si*********@REMOVE.att.net> wrote in message
Isn't it a bug of the implementation that it does not respect the
uppercase flag in printing strings and const char arrays?
Or maybe we have to imbue some locale in the cout before printing?


ios::uppercase affects only integers printed in hexadecimal and the

exponent
of floating point numbers printed in scientific notation.


I didn't look at the standard but my compilers documentation which derives
from dinkunware. Its normally pretty reliable.
The standard says in the chapter on Input/output library: "replaces certain lowercase letters with their uppercase equivalents in generated output".
Could they be any more vague? But the chapter on locales says the flag is
only used in num_get and num_put, that is for integers.
I don't think locales are meant to be used for this purpose. A locale could
specify *how* to convert from uppercase to lowercase but I think it would be
stretching the meaning of a locale to make it actually do that
conversion.
If I had to do this a lot I would write a streambuf wrapper. This is only minimally tested, and probably needs fleshing out to be a complete
implementation.

#include <streambuf>
#include <iostream>
#include <cctype>
using namespace std;

class ToUpperStreamBuf : public streambuf
{
public:
ToUpperStreamBuf(streambuf* wrapped) : _wrapped(wrapped)
{
}


What is variable 'wrapped' for? OK, I get it, so you can use this class
with a filebuf, iostreambuf, streambuf, etc.
protected:
int_type overflow(int_type ch)
{
if (traits_type::not_eof(ch))
ch = _wrapped->sputc(toupper(traits_type::to_char_type(ch)));
return ch;
}


Don't you have to uppercase all the chars in [eback(), egptr()) too? I'd
then call _wrapped->overflow(ch).


_wrapped->overflow is protected, so can't be called here.
Also, the streambuf has a locale in it so
one could call getloc(), then toupper(c, loc).
Agreed, although there are two streambuf. I think I would use the wrapped
streambuf and ignore the wrapper.
int sync()
{
return _wrapped->pubsync();
}

private:
streambuf* _wrapped;
};
One should probably forward all virtual functions to _wrapped.


Agreed, imbue -> pubimbue (use the locale in the wrapped streambuf),
seekoff -> pubseekoff, seekpos ->pubseekpos (support positioning if the
wrapped streambuf does) etc. etc.
An
alternative design pattern I've used for this sort of thing is templates.

template <class Streambuf>
class ToUpperStreamBuf : public Streambuf {
public:
typedef typename Streambuf::int_type int_type;
ToUpperStreamBuf();
protected:
int_type overflow(int_type ch);
};

But wait! There's a problem because we don't know the type of cout.rdbuf(), as it's implementation defined, and usually some special class derived from streambuf.
int main()
{
// replace cout stream buffer
streambuf* old_buf = cout.rdbuf();
ToUpperStreamBuf toupper_buf(old_buf);
cout.rdbuf(&toupper_buf);

// now cout will print in uppercase
cout << "hello\n";

// restore cout stream buffer
cout.rdbuf(old_buf);
}
The stream method seems too big for most purposes, and forces uppercase

all the time. How about writing a class-style manipulator, which I've used in
the past?

class ToUpper {
public:
ToUpper(const char *);
private:
some data variable here;
};

std::ostream& operator<<(std::ostream&, ToUpper);
Seems reasonable. I was assuming that the OP wanted to force a stream to
output everything in uppercase.

It would be nice if in istream and ostream they had a virtual function that returned a sentry object, and the default behavior would be to return the
sentry as they currently have (that for istream ignores whitespace). Then
users of derived ostreams could change the virtual function to return a
sentry class derived from the base class one. Presumably it would do
additional processing. One minor problem: the sentry's copy constructer is private.

class myostream : public std::ostream {
public:
class mysentry : public std::ostream::sentry { ... };
virtual /*override*/
mysentry getsentry() const;
};

What do you think of this design pattern in general?


Not sure, storing the sentry in the stream might have some implications.
What about multithreading for instance? Also there would still be a need for
a sentry object on the stack (to ensure exception safety). Presumably the
sentry on the stack would call methods in the sentry stored in the stream.
In fact I wouldn't use the term sentry for the object stored in the stream.
Perhaps a simpler method would be to have virtual methods on the stream
called prolog and epilog. The sentry can call these methods in its ctor and
dtor.

john
Jul 22 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.