Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 187
  • Last Modified:

easy question

I need to display double 00 to represent seconds minutes
for a clock program that I am writing
just inserting double 00 is wrong of course
declaring them as a char doesn't work.
thank you
0
strmtrpr
Asked:
strmtrpr
  • 4
  • 2
  • 2
  • +5
1 Solution
 
ozoCommented:
printf("%02d:%02d",minutes,seconds);
0
 
Answers2000Commented:
BTW ozo's answers assumes minutes & seconds are int variables
0
 
strmtrprAuthor Commented:
I have to display the time using cout.

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
thresher_sharkCommented:
Why must you display the time using cout?  printf would work equally well (I think).  Please tell us more about what you want to do.
0
 
strmtrprAuthor Commented:
I had to write a class that had threee inline functions .
1. to set the clock
2. to increment the clock.
3. to display the time. in standard or milatary . depending on the argument this function received.
I taking a C++ class , I have everything working except for this. we are not supposed to printf
I wish I could use printf I think it would be easier.
any help would be great.

0
 
bbarnetteCommented:
This is what I use to display date or time:

time_t ltime;
struct tm* ldate;

time(&ltime);
ldate = localtime(&ltime);

cout <<ldate->tm_min<<":"<<ldate->tm_sec<<endl;

0
 
thresher_sharkCommented:
If you already have the variables all straightened out, you can just:

cout << minutes << ":" << seconds;

Or perhaps:
cout << hours << minutes << seconds;
0
 
mlevCommented:
How about this:

char buf[6];
sprintf(buf, "%02d:%02d",minutes,seconds);
cout << buf << endl;

0
 
mlevCommented:
Btw, I don't know what compiler you're using, but in G++
you can do
cout.form("%02d:%02d",minutes,seconds);

0
 
strmtrprAuthor Commented:
thanks everyone for their help I think I am going to rethink the increment function

0
 
alexoCommented:
#include <iostream.h> // plain <iostream> on newer compilers
#include <iomanip.h>  // plain <iomanip> on newer compilers

int main()
{
    int hour = 11, min = 2, sec =3;
    cout << setfill('0');
    cout << setw(2) << hour << ':' << setw(2) << min << ':' << setw(2) << sec << endl;
   
    return 0;
}
0
 
GrailmanCommented:
Since a printf is 'compiled' at run-time, how much of an actual performance diffrence is there between printf & cout?
0
 
strmtrprAuthor Commented:
I am taking a c++ Class the teacher wanted us to get use to cout instead of printf
the c++ books I have seen, don't use printf they use cout.
I have had a C class and I am use to the printf,scanf etc  Trying to get use to cout cin  is taking a while.  

by the way .
someone last night commeted that I use under G++.
cout.form("%02d,%02d,%02d",hours,minutes,second);
so I booted into LINUX  2 minutes later I was up and running
thanks for the help.


0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 4
  • 2
  • 2
  • +5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now