473,395 Members | 1,915 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,395 software developers and data experts.

Zero Fill Function C++

I am trying to write a function that passes in a int and if int is <10 puts a 0 infront of the int to print out. I can't use the manipulator.

Instead of 3, it prints 03.

Does anyone have any ideas?
Sep 24 '06 #1
5 13320
I have to pass mutilple numbers into this function. So I don't think it can be in the out statement. I would think that it would have to be assigned to a variable.
Sep 24 '06 #2
dush
27
Hi

If your function doesnt have to return string composed of numbers you passed to it, in another words If you just need to print out the numbers to screen I would recommend you:

in C use printf :

int i = 5, j = 15;
printf("%02d", i);
printf("%d", j);

in C++ you can use cout in your function:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. void print(const int& x) {
  5.   cout << (x < 10 ? cout << 0, x : x);
  6. }
  7.  
  8. int main() {
  9.  
  10.   print(5);    
  11.   cout << endl;  
  12.   print(15);
  13.  
  14.   return 0;    
  15. }
Sep 24 '06 #3
D_C
293 100+
For A number, say A, with N decimal digits, we can relate them by N = (int)(log A), where the logarithm is base 10.

If you want all numbers to have 30 digits, then print a string with (30-N) "0"s, followed by your number A.
Sep 24 '06 #4
dush
27
thanks for good idea D_C

I was actually thinkin how to get count of digits of integer number.
(int)log10 works fine, so I upgraded my function:

void print(const int& x, int n) {
while(--n > (int)log10(x)) cout << 0;
cout << x;
}
Sep 25 '06 #5
etaham
1
I found this post on google while looking for a solution to the same problem.
Here's what I got,

cout.fill('0');
cout<<std::setw(N)<<your number<<endl;

Where N is the width of the string you wish to print, and your number is your int.
The fill function tells the stream what to fillt he extra spaces with.
Jul 30 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Steve | last post by:
Is there a way in PHP to zero fill an integer to a specified length? For instance, if I have a two digit number, and I want to zero fill it to four digits, is there a PHP function I can use? Or do...
13
by: simondex | last post by:
Hi, Everyone! Does anyone know how to initialize an int array with a non-zero number? Thank You Very Much. Truly Yours, Simon Dexter
3
by: John Baker | last post by:
Hi: I have a report that has currency fields which I wish to have as blank if they are zero, but to show as fill currency if they have data in them: 0 = Blank 100= $ 100.00 ..01 = $ .01 ...
2
by: Steve | last post by:
I have a database where I want to use an autonumber field to make SKUs for a new product when it is entered. Is there a way I can make the field zero fill to a certain length? For instance, if I...
53
by: Zhiqiang Ye | last post by:
Hi, All I am reading FAQ of this group. I have a question about this: http://www.eskimo.com/~scs/C-faq/q7.31.html It says: " p = malloc(m * n); memset(p, 0, m * n); The zero fill is...
10
by: Lyle Fairfield | last post by:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbaac11/html/acfctNZ_HV05186465.asp "If the value of the variant argument is Null, the Nz function returns the number zero or a...
3
by: Haleigh | last post by:
I just started a few days ago, so I'm very new to this. I'm trying to update a gridview using a store procedure. When I am in the update subroutine, newvalues and oldvalues are empty, plus count is...
3
by: Bob | last post by:
Hi all, I have some additions that I need to do on some fields in some of my queries but it seems that my fields don't always have a value in them... So for example, I want the result of ColA...
10
by: tshad | last post by:
Is there a way to zero fill a numeric into a string that is a fixed lengh of 10 chars? In Sql I would do something like: Right("0000000000" + Convert(varchar,amt),10). Is there an easy way to...
4
by: lawrence k | last post by:
I've got a function that starts off like this: function alphabeticalListOfAlbumsByLetter($whichLetter="a") { } I pass in the letters (a, b, c, d, etc) and the numbers (0, 1, 2, 3, 4,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...

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.