473,322 Members | 1,398 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Problem in setting I/O format strings in DEV c/c++

The following code does not display the number 10 in hex format. The hex
flag doesn't seem to work.

Any ideas?

Compiler is DEV C++ 4.9.9.2 http://www.bloodshed.net/

#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
int main ()
{
int a=10;
cout.setf(ios::hex);
cout<<a;
cout<<setiosflags(ios::hex)<<a<<endl;

system("pause");
}
Nov 8 '07 #1
4 2548
"Nikos Hatzigiannakis" <yp**@aegean.grwrote in message
news:fg*********@mouse.otenet.gr...
The following code does not display the number 10 in hex format. The hex
flag doesn't seem to work.

Any ideas?

Compiler is DEV C++ 4.9.9.2 http://www.bloodshed.net/

#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
int main ()
{
int a=10;
cout.setf(ios::hex);
cout<<a;
cout<<setiosflags(ios::hex)<<a<<endl;

system("pause");
}
try
std::cout.setf(std::ios::hex, std::ios::basefield);
and no, I don't know what basefield is or why it works that way, I just
found that out by googling.
Nov 8 '07 #2
On 2007-11-08 13:22:10 -0500, "Jim Langston" <ta*******@rocketmail.comsaid:
>
try
std::cout.setf(std::ios::hex, std::ios::basefield);
and no, I don't know what basefield is or why it works that way, I just
found that out by googling.
basefield is the mask for the bits that determine the base to use for
I/O. If you just setf(ios::hex) you jam in whatever value ios::hex has,
and might end up with an invalid flag setting. When you use
setf(ios::hex, ios::basefield) you clear the bits represented by
basefield, then jam in the value of ios::hex.

In general, you need to do the analogous thing with all of the flags
that have more than two values.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Nov 8 '07 #3

Jim Langston wrote in message...
"Nikos Hatzigiannakis" wrote in message...
The following code does not display the number 10 in hex format. The hex
flag doesn't seem to work.
Any ideas?
Compiler is DEV C++ 4.9.9.2 http://www.bloodshed.net/

#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
int main (){
int a=10;
cout.setf(ios::hex);
cout<<a;
cout<<setiosflags(ios::hex)<<a<<endl;

system("pause");
}

try
std::cout.setf(std::ios::hex, std::ios::basefield);
and no, I don't know what basefield is or why it works that way, I just
found that out by googling.
"
Method: fmtflags ios::setf (fmtflags flag, fmtflags mask)
Clear the flag values indicated by mask, then set any of them that are also
in flag. (Flag values are described for `ios::flags ()'.) Return the
complete collection of flags previously in effect. (See ios::unsetf for
another way of clearing flags.)
"

So, it clears the 'basefield', then sets it to 'hex'.

OP:

int main (){
int a=10;

std::cout<<std::hex<<"0x"<<a<<std::endl;

system("pause");
}

--
Bob R
POVrookie
Nov 8 '07 #4
On Nov 8, 8:37 pm, Pete Becker <p...@versatilecoding.comwrote:
On 2007-11-08 13:22:10 -0500, "Jim Langston" <tazmas...@rocketmail.comsaid:
try
std::cout.setf(std::ios::hex, std::ios::basefield);
and no, I don't know what basefield is or why it works that way, I just
found that out by googling.
basefield is the mask for the bits that determine the base to use for
I/O. If you just setf(ios::hex) you jam in whatever value ios::hex has,
and might end up with an invalid flag setting. When you use
setf(ios::hex, ios::basefield) you clear the bits represented by
basefield, then jam in the value of ios::hex.
In general, you need to do the analogous thing with all of the flags
that have more than two values.
You mean, with all of the flags which aren't flags:-). And of
course, std::ios::hex only has one value, at least in a given
implementation.

The way I think of it is that fmtflags is conceptually a struct.
Some of the fields are boolean (flags), and can be set and reset
using the single argument versions of setf and unsetf. There is
no separate mask value (i.e. no ios::showposmask) for these,
since the set value is the mask (supposing you needed a mask).
Others, such as the base, the floating point representation, and
the alignment, are conceptually enum's: these have a distinct
mask value (field name), and can only be manipulated using the
two argument form of setf, with the name of the field as the
second argument.

Note that in the two argument form, you must specify all of the
affected fields in the second argument. Just saying:
setf( ios::showpos | ios::hex, ios::basefied )
isn't sufficient---you have to say:
setf( ios::showpos | ios::hex, ios::showpos | ios::basefield)
IMHO, however, it's clearer if you use two separate function
calls for this.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Nov 9 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Irepan | last post by:
does anybody know how to change the global IFormatProvider of my project so everycall to Convert.ToDouble on it uses this format provider instead of the one the one on the computer's regional...
3
by: xmlguy | last post by:
XmlTextReader myXmlReader = new XmlTextReader(args); string en = myXmlReader.Encoding.EncodingName; //Console.WriteLine(x); Error: Unhandled Exception: System.NullReferenceException: Object...
3
by: seegoon | last post by:
Hi to all. I have a small problem I hope someone can help me with. I am running a sql query to a csv file. The query searches for the total of a column between 2 dates. This is a copy of one of...
1
by: Li Pang | last post by:
Hi, I'd like to get the local language setting used to interpret the date string in Excel, anyone has an idea? Thanks in advance
5
by: Tsair | last post by:
I set the thread culture in MAIN() as below in order to show the date in format DAY/MONTH/YEAR, but the datagridview alway show the date in M/d/yyyy. How to set the default Date format from...
1
by: laredotornado | last post by:
Hi, I'm using PHP 4.4.4 on Apache 2 on Fedora Core 5. PHP was installed using Apache's apxs and the php library was installed to /usr/local/php. However, when I set my "error_reporting"...
8
by: Thunder | last post by:
Hi All, I'm using VB.Net 2005. I have for years used Format to format strings and dates, etc... Now when I try to use Format I get the following error? "'Format' is a type and can not be used...
5
by: snow | last post by:
Hi All, ToOADate can change a date value to a double value, but if I change the Regional and Lauguage setting in control panel, for example, change English(US) to English(Australia) , the same...
5
by: Rahul | last post by:
Friends, My Problem is related to writing a text file from a database using .Net. in a particular string format. I have a dataset with a table, let suppose in this table there are three fields....
1
by: Anup Gupta | last post by:
imsc_int32_t imsc_wsprintf( OUT imsc_wchar_t *s, IN imsc_wchar_t *format, ...) { va_list ap; imsc_int32_t dNumBytes; va_start(ap, format); dNumBytes = swprintf(s,format, ap);...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.