473,597 Members | 2,342 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ios::uppercase does not work

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
8 4305
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

"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::basef ield flag and the 2 arguement setf.
Try:

#include <iostream>

int main()
{
int num = 45;
std::cout.setf( std::ios::hex, std::ios::basef ield);
std::cout.setf( std::ios::showb ase );
std::cout.setf( std::ios::upper case );
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
On Sun, 27 Jun 2004 00:02:13 -0400, SaltPeter <Sa*******@Jupi ter.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::basef ield flag and the 2 arguement setf.
Try:

#include <iostream>

int main()
{
int num = 45;
std::cout.setf( std::ios::hex, std::ios::basef ield);
std::cout.setf( std::ios::showb ase );
std::cout.setf( std::ios::upper case );
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::upper case << test; // want NUMBER, but don't get it

Markus

Jul 22 '05 #4
>
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::upper case << 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
"John Harrison" <jo************ *@hotmail.com> wrote in message
std::string test = "number";
std::cout << std::ios::upper case << 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

"Siemel Naran" <Si*********@RE MOVE.att.net> wrote in message
news:0h******** *************@b gtnsc04-news.ops.worldn et.att.net...
"John Harrison" <jo************ *@hotmail.com> wrote in message
std::string test = "number";
std::cout << std::ios::upper case << 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 ToUpperStreamBu f : public streambuf
{
public:
ToUpperStreamBu f(streambuf* wrapped) : _wrapped(wrappe d)
{
}

protected:
int_type overflow(int_ty pe ch)
{
if (traits_type::n ot_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();
ToUpperStreamBu f toupper_buf(old _buf);
cout.rdbuf(&tou pper_buf);

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

// restore cout stream buffer
cout.rdbuf(old_ buf);
}

john
Jul 22 '05 #7
"John Harrison" <jo************ *@hotmail.com> wrote in message
"Siemel Naran" <Si*********@RE MOVE.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 ToUpperStreamBu f : public streambuf
{
public:
ToUpperStreamBu f(streambuf* wrapped) : _wrapped(wrappe d)
{
}
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_ty pe ch)
{
if (traits_type::n ot_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 ToUpperStreamBu f : public Streambuf {
public:
typedef typename Streambuf::int_ type int_type;
ToUpperStreamBu f();
protected:
int_type overflow(int_ty pe 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();
ToUpperStreamBu f toupper_buf(old _buf);
cout.rdbuf(&tou pper_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::s entry { ... };
virtual /*override*/
mysentry getsentry() const;
};

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

"Siemel Naran" <Si*********@RE MOVE.att.net> wrote in message
news:re******** *************@b gtnsc04-news.ops.worldn et.att.net...
"John Harrison" <jo************ *@hotmail.com> wrote in message
"Siemel Naran" <Si*********@RE MOVE.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 ToUpperStreamBu f : public streambuf
{
public:
ToUpperStreamBu f(streambuf* wrapped) : _wrapped(wrappe d)
{
}


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_ty pe ch)
{
if (traits_type::n ot_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 ToUpperStreamBu f : public Streambuf {
public:
typedef typename Streambuf::int_ type int_type;
ToUpperStreamBu f();
protected:
int_type overflow(int_ty pe 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();
ToUpperStreamBu f toupper_buf(old _buf);
cout.rdbuf(&tou pper_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::s entry { ... };
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
5803
by: Paul Schwann | last post by:
Hi everybody, I have a problem with the following code: #include <iostream> int main(void) { std::cout << std::hex << std::uppercase << 31 <<std::endl; return 0;
1
1671
by: sachin bond | last post by:
The following code(in c++) is supposed to divide a file into 'n' different files. suppose i'm having a file called "input.zip". The execution of the following code should divide "input.zip" into 'n'(user specified) different files. However the code currently..though makes 'n' different files... the divided contents are stored only in the first of the 'n' files.
5
1600
by: Mark Bruno | last post by:
I thought system("PAUSE"); was part of stdlib.h and cstdlib. Then why does this work? : #include <iostream> using namespace std; int main() { cout << "hello, world" << endl; system("PAUSE"); return 0;
6
7280
by: Luther Baker | last post by:
Hi, This doesn't do what I expect. int t = 27; std::cout << "myHex: '" << std::ios::hex << t << "'" << std::endl; std::cout.setf(std::ios::hex); std::cout << "moreHex: '" << t << "'" << std::endl; VC++ 7.1 says:
5
6411
by: Dennis | last post by:
if you have the headers and main in below code snippet, the below ios::noreplace creates an error: error C2039: 'noreplace' : is not a member of 'basic_ios<char,struct std::char_traits<char> >' error C2065: 'noreplace' : undeclared identifier The ios::noreplace allows the open to fail if the file already exists. What does the new std library use to fail an open if the file already exists?
103
48577
by: Steven T. Hatton | last post by:
§27.4.2.1.4 Type ios_base::openmode Says this about the std::ios::binary openmode flag: *binary*: perform input and output in binary mode (as opposed to text mode) And that is basically _all_ it says about it. What the heck does the binary flag mean? -- If our hypothesis is about anything and not about some one or more particular things, then our deductions constitute mathematics. Thus mathematics may be defined as the subject in...
10
2368
by: Gunnar G | last post by:
I'm having problem reading from the beginning of a file. Here is the code (more or less) ifstream codefin; ofstream codefout; while (not_annoyed)
9
5281
by: Someonekicked | last post by:
In my program, I need to open multiple files, and I wont know till after the program execution how many of them (user will enter that value). So I am using a vector of fstream. I am using fstream since I will need to write and read from files, and I am using those files as binary files. I made a sample of what's going on (below). Without using vector, everything works fine, but with using vectors, something is going wrong somewhere!?? ...
3
16352
by: Joseph Lu | last post by:
Hi,all I have code like the following lines, but when I compile this program I got a: error C2563 : 'ios' : is not a class or namespace name! //--Code starts here #include <iostream> #include <fstream> #include <cstdlib> #include <iomanip>
0
7969
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8272
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8381
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8035
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6688
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5847
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5431
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2404
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1494
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.