Tuesday, July 28, 2009

C++ time problem...?

This is the code I am using to output the number of years since 1-1-1970.


Problem is the output is 361 years! What's wrong?





#include %26lt;time.h%26gt;


#include %26lt;iostream%26gt;


#define NL '\n';


using namespace std;





int main(){


time_t seconds;


seconds=time(NULL);


printf ("%d1 years since January 1, 1970", seconds/31556926);cout%26lt;%26lt;NL; //31556926seconds in a year





system("PAUSE"); // Pause so user can see the output


return 0;


}

C++ time problem...?
Well, the correct answer is 36, isn't it. And you printed 361. So you're close, but where did the 1 come from? Answer: %d1. You're telling printf to print a 1 after the decimal number.


No comments:

Post a Comment