GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 27-Nov-2005, 00:53
lian1238 lian1238 is offline
New Member
 
Join Date: Sep 2005
Location: Thailand
Posts: 28
lian1238 is on a distinguished road

display two digit values


Hi

Is there a way to display 2-digit values with cout?
  #2  
Old 27-Nov-2005, 01:22
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,258
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: display two digit values


Not easily. The simplest is
CPP / C++ / C Code:
if (num < 10)
{
    cout << '0';
}
cout << num;

cout is notoriously difficult to get formatted output. I find the iomanip flags and features to be tedious and limiting.
printf is much better but C++ snobs and zealots don't like using it for some reason.

For the hard way, check out this link
__________________

Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
  #3  
Old 27-Nov-2005, 08:39
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 927
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

Re: display two digit values


Quote:
Originally Posted by lian1238
Hi

Is there a way to display 2-digit values with cout?
Yes. cout itself prints any 2 digit value.

Please explain more clearly what you want.


Paramesh.

PS: Just a joke.
__________________

Don't walk in front of me, I may not follow.
Don't walk behind me, I may not lead.
Just walk beside me and be my friend.
  #4  
Old 27-Nov-2005, 11:16
lian1238 lian1238 is offline
New Member
 
Join Date: Sep 2005
Location: Thailand
Posts: 28
lian1238 is on a distinguished road

Re: display two digit values


WaltP, thx for the link.
Paramesh, I dun really get your joke but.. it's good to joke!
  #5  
Old 28-Nov-2005, 08:54
alcoholic's Avatar
alcoholic alcoholic is offline
Member
 
Join Date: Nov 2005
Posts: 170
alcoholic is on a distinguished road

Re: display two digit values


Quote:
Originally Posted by lian1238
Hi

Is there a way to display 2-digit values with cout?
Yes plz explain in detail what u want...it seems a very trivial question
  #6  
Old 28-Nov-2005, 10:46
lian1238 lian1238 is offline
New Member
 
Join Date: Sep 2005
Location: Thailand
Posts: 28
lian1238 is on a distinguished road

Re: display two digit values


first, sorry I wasn't very clear in the first place.
second, is it really a trivial question?
third, what I'm saying is that, if you had a number, say 2, and wanted to display "02", how would you do it with cout?
well, WaltP said you can do it like this:

CPP / C++ / C Code:
if (num < 10)
{
    cout << '0';
}
cout << num;

that wasn't very hard. but, what if you wanted to display 2 as "0000000002"?? (a ten-digit number).. that'd be more tedious, not necessarily harder.

I hope I've made myself more clear.
  #7  
Old 28-Nov-2005, 11:11
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 927
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

Re: display two digit values


Quote:
Originally Posted by lian
but, what if you wanted to display 2 as "0000000002"?? (a ten-digit number).. that'd be more tedious, not necessarily harder.
We can use the fill and width options in cout to do this.
No need to use iomanip!

For example, insert this code:
CPP / C++ / C Code:
    int i = 2;

    cout.fill('0');       // fill available spaces with 0  
    cout.width(10);  // set the width as 10

    cout<< i;           // print the value and see whats your output

Will print nine '0's before 2.
The output will be like:
Code:
0000000002

Regards,
Paramesh.
__________________

Don't walk in front of me, I may not follow.
Don't walk behind me, I may not lead.
Just walk beside me and be my friend.
  #8  
Old 28-Nov-2005, 13:07
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,258
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: display two digit values


Quote:
Originally Posted by Paramesh
We can use the fill and width options in cout to do this.
No need to use iomanip!
I was including these [kbd]cout[/jbd] methods as part of iomanip. My unfortunate lack of detailed knowledge. My dislike still holds -- these methods seem so awkward to me. Formatting cout is awkward, tedious, and limiting.

The printf() function is so much easier and straight forward...
__________________

Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
  #9  
Old 28-Nov-2005, 13:27
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,258
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

The printf/cout challenge


I have an ongoing challenge that noone's solved. The challenge is
Convert this printf() statement into readable C++, output must match exactly:
CPP / C++ / C Code:
int iVal = 2;
int hVal= 0x0C;
char *filename = "test.fil";
char *username = "juser";
float fValue23.8;
printf("%4d  0x%02X  Testfile: [%10s]  user: [%-10s]\nTrial %8.2f \n", 
        iVal, hVal, filename, username, fValue);
Output
CPP / C++ / C Code:
   2  0x0C  Testfile: [  test.fil]  user: [juser     ]
Trial    23.80

taken from a previous post
__________________

Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
  #10  
Old 28-Nov-2005, 21:35
Sokar Sokar is offline
Member
 
Join Date: May 2005
Posts: 243
Sokar has a spectacular aura aboutSokar has a spectacular aura about

Re: The printf/cout challenge


Quote:
Originally Posted by WaltP
output must match exactly:
I get this for output with your code.
walts_conjured_up_code.cpp(17) : error C2143: syntax error : missing ';' before 'constant'
Ok here you go,
CPP / C++ / C Code:
int main()
{
	float fValue23.8;
	return 0;
}
mycode.cpp(17) : error C2143: syntax error : missing ';' before 'constant'
I win. What do I win?
Quote:
Originally Posted by WaltP
taken from a previous post
Whenever you post that code it always has the same error. It seems like you would have fixed that by now.

Quote:
Originally Posted by WaltP
I have an ongoing challenge that noone's solved. The challenge is
Convert this printf() statement into readable C++, output must match exactly:

In all seriousness readable is subjective, as is convert, so here you go,
CPP / C++ / C Code:
#include <iostream>
#include <string>

using namespace std;

int main()
{
	int iVal = 2;
	int hVal= 0x0C;
	char *filename = "test.fil";
	char *username = "juser";
	float fValue = 23.8f;

	printf("%4d  0x%02X  Testfile: [%10s]  user: [%-10s]\nTrial %8.2f \n", 
		iVal, hVal, filename, username, fValue);

	cout << "   " << iVal << "  0x0";
	cout << hex << uppercase << hVal;

	string tfilename(filename);
	string tusername(username);
	cout << "  Testfile: [  " << tfilename << "]  user: ["
		<< tusername << "     ]\n";
	cout.precision(4);
	cout << "Trial    " << showpoint << fValue << endl;

	return 0;
}

I will not debate whether you have a valid point or not.
But most C++ user will use string not *char, so you have deliberately created more problems with your conjured up example than need be. So what you are trying to accomplish. Your gripe seems to be formating. So before you go and start talking about performance and code bulk with what I posted, I am not interested. You should ask the OP if what you posted is more readable. You know the answer will be no.
 
 

Recent GIDBlogUS Elections and the ?Voter?s Responsibility? by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Help With Stacks penance C Programming Language 9 10-Oct-2005 09:25
fedora display problem machinated Computer Software Forum - Linux 1 22-Aug-2005 22:03
problems with a text display in a tab maveganzones FLTK Forum 2 23-Mar-2005 03:26
My program can run,but warning were display on Vc++ fwongmc C Programming Language 5 08-Dec-2004 11:15

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 12:00.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.