473,320 Members | 2,112 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,320 software developers and data experts.

leading zero in C++

hey i am doing a program that involves time in 24 hour format and i want to put a leading zero before the hour and minute values that are between 0 and 9. That is; i want it to appear like 02:30am and not like 2:30am on the black screan or like 15:03pm and not like 15:3pm.Any ideas?
Oct 18 '08 #1
4 13816
boxfish
469 Expert 256MB
Take a look at this page on iomanip. setfill looks useful.
Expand|Select|Wrap|Line Numbers
  1. #include <iomanip>
  2. // Other stuff.
  3. cout << setw(2) << setfill('0') << minutes << endl;
  4.  
But you could only do that in C++.
You could to this with an if statement instead.
Expand|Select|Wrap|Line Numbers
  1. if (minutes < 10)
  2.     cout << 0;
  3. cout << minutes << endl;
  4.  
Hope this helps.
Oct 18 '08 #2
Ganon11
3,652 Expert 2GB
You probably don't want to say

cout << 0;

as this prints...umm, I'm not sure what character has the ASCII value of 0. I think it's an unprintable character. You want to do this:

cout << '0';

which prints out the character 0.
Oct 18 '08 #3
boxfish
469 Expert 256MB
cout can take almost anything. The << operator is overloaded for ints as well as chars, so 0 won't be casted to a char. cout << 0 will print 0.
Edit:
Oh yeah, the ASCII value of zero is '\0', the famous null terminator. Doesn't look like much when printed.
Oct 18 '08 #4
Take a look at this page on iomanip. setfill looks useful.
Expand|Select|Wrap|Line Numbers
  1. #include <iomanip>
  2. // Other stuff.
  3. cout << setw(2) << setfill('0') << minutes << endl;
  4.  
But you could only do that in C++.
You could to this with an if statement instead.
Expand|Select|Wrap|Line Numbers
  1. if (minutes < 10)
  2.     cout << 0;
  3. cout << minutes << endl;
  4.  
Hope this helps.
thanx alot boxfish it worked out well.
Oct 19 '08 #5

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

Similar topics

6
by: david | last post by:
Hi, I have an application as follows: MySQL database Back-Eend linked to MS Access Front-End and ASP Web Application. I require users to enter Serial Numbers such as: 0105123567 (10...
2
by: Jeff Lowry | last post by:
I'm pasing a zip code as a prameter to an Access stored procedure. In Access the parameter is a text data type. It works for non-leading zero zip codes but, apparently access (or ASP) is...
7
by: david | last post by:
Hi, I have 2 text boxes on an ASP form. A user enters a Serial Number in TB1 such as 0105123456, presses tab to move to TB2, TB2 then displays the value of TB1 after a calculation has been...
1
by: Joshua Ammann | last post by:
Hello, I'm trying to export a query containing contact information, including a field. Some zip codes have one or two leading zeros, for example, San Juan, PR (00927) and Springfield, MA...
5
by: OneDay | last post by:
I've got a field that has some old data with text in it, but all forward data will be a 3 digit number. But many of the numbers are still only 2 digits. I would like to force the leading zero in...
2
by: chris | last post by:
Hi, I have a simple ms access application that allows you to scan barcodes in to a form which stores them in the database. The barcodes are 6 digits in length e.g. 555666 but my handheld...
5
by: GarryJones | last post by:
I have code numbers in 2 fields from a table which correspond to month and date. (Month, Code number) Field name = ml_mna 1 2 3 etc up to 12 (Data is entered without a leading zero)
8
by: Andrew Poulos | last post by:
In my limited testing with FF 2, IE 6 and Opera 9 when I divided a positive integer, that is less than 100, by 100 I get a leading zero in front of the decimal point. For example 80/100 gives...
4
by: bobm2005 | last post by:
Whatever format I try in Printf, an 'E' format number nearly always has a leading non-zero:- 1.2345E7 -9.3456E8 etc. Is it possible to force it (printf) always to have leading zero? ...
6
by: JimmyKoolPantz | last post by:
Task: Customer wants a script of the data that was processed in a "CSV" file. Problem: Zip-Code leading zeros are dropped Basically we have a client that has requested a custom script for...
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
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...
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: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
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)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.