Follow us on Twitter!
Your life is ending one minute at a time. If you were to die tomorrow, what would you do today?
Friday, April 25, 2014
Navigation
Home
HellBoundHackers Main:
HellBoundHackers Find:
HellBoundHackers Information:
Learn
Communicate
Submit
Shop
Challenges
HellBoundHackers Exploit:
HellBoundHackers Programming:
HellBoundHackers Think:
HellBoundHackers Track:
HellBoundHackers Patch:
HellBoundHackers Other:
HellBoundHackers Need Help?
Other
Members Online
Total Online: 16
Guests Online: 15
Members Online: 1

Registered Members: 82906
Newest Member: ilija
Latest Articles
View Thread

HellBound Hackers | Computer General | Programming

Author

C++ Time in seconds problem


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 22-01-11 23:32
#include <iostream>
using namespace std;


int main ()
{
int Hour;
int Minute;
int Second1;
int HourSeconds = Hour * 3600;
int MinuteSeconds = Minute * 60;
int SecondFinal = HourSeconds + MinuteSeconds + Second1;


cout <<"WHat hour is it?" << endl;
cin >> Hour;
cout <<"what minute is it?" << endl;
cin >> Minute;
cout <<"What second is it?" << endl;
cin >> Second1;
system("pause"Wink;
cout <<"The time is " << Hour << ":" << Minute << ":" << Second1 << endl;
cout <<"The time in seconds is " << SecondFinal << endl;
system("pause"Wink;
return 0;
}

I can not seem to get this to work. No matter what time, it still comes up with the same value of 887918626. please help.


Author

RE: C++ Time in seconds problem

tkearn5000
Member



Posts: 32
Location: US
Joined: 13.04.09
Rank:
Newbie
Posted on 22-01-11 23:47
Your setting the values of HourSeconds, MinuteSeconds, and Seconds1 before you get the correct information from the user. You have to do those calculations after you get the user's input.

In C++ the compiler allows you to operate on non initialized variables. The value in those variables is just whatever happens to be in memory when the variables are created. That's why you are getting an answer, but not the answer you're looking for.


img7.imageshack.us/img7/6588/1993.png
Author

RE: C++ Time in seconds problem

t0xikc0mputer
Member



Posts: 112
Location: t0xik waste dump
Joined: 07.01.11
Rank:
Newbie
Posted on 23-01-11 00:38
Dude, it's probably a good idea if you "disable smileys in this post." That way we can read all of your code.


Author

RE: C++ Time in seconds problem

j4m32
Member

Your avatar

Posts: 81
Location:
Joined: 01.05.10
Rank:
Newbie
Posted on 23-01-11 01:37
+1 to tkearn5000, hit it on the head.

The only other thing I would add to that...
Make sure your inputs are
a) the correct type and
b) the value does not fall otuside the limits of the type you are storing it in.

[ Just stick in some do-while loops in there with some ifs to prompt the user they're being an idiot! Smile ]

There are some preprocessor constants with some C and C++ compilers, burried away in the header files, for the upper and lower limits for all the types (unless unsigned).

Jim,
Author

RE: C++ Time in seconds problem

the_unwanted
Member

Your avatar

Posts: 11
Location:
Joined: 03.12.10
Rank:
Newbie
Posted on 26-01-11 03:50
#include <iostream>
using namespace std;


int main ()
{
int Hour;
int Minute;
int Second1;
int HourSeconds;
int MinuteSeconds;
int SecondFinal;


cout <<"WHat hour is it?" << endl;
cin >> Hour;
cout <<"what minute is it?" << endl;
cin >> Minute;
cout <<"What second is it?" << endl;
cin >> Second1;

HourSeconds=Hour*3600;
MinuteSeconds=Minute*60;
SecondFinal=HourSeconds+MinuteSeconds+Second1;

system("pause" );
cout <<"The time is " << Hour << ":" << Minute << ":" << Second1 << endl;
cout <<"The time in seconds is " << SecondFinal << endl;
system("pause" );
return 0;
}