Join us at IRC!
Understanding is the answer, hatred is the problem, and hackers are the slaves abused and destroyed in the process of peace online - Deshouleres
Wednesday, May 23, 2012
Navigation
Members Online
Total Online: 34
Web Spiders: 13
Guests Online: 31
Members Online: 3

Registered Members: 70170
Newest Member: bahmx
Latest Articles
View Thread

HellBound Hackers | Computer General | Programming

Author

C++ Time in seconds problem

vegeta_man111
Member

Posts: 104
Location: Ohio
Joined: 20.07.05
Rank:
Apprentice
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";);
cout <<"The time is " << Hour << ":" << Minute << ":" << Second1 << endl;
cout <<"The time in seconds is " << SecondFinal << endl;
system("pause";);
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:
God
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.



Author

RE: C++ Time in seconds problem

t0xikc0mputer
Member



Posts: 112
Location: t0xik waste dump
Joined: 07.01.11
Rank:
Elite
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.


This sentence is false.

Hahaha no.



Edited by t0xikc0mputer on 29-05-11 9:36
http://t0xiksite.herobo.com/
Author

RE: C++ Time in seconds problem

j4m32
Member

Posts: 81
Location:
Joined: 01.05.10
Rank:
God
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! :) ]

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

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;
}
Guest
Username

Password

Remember Me


Bookmark This Page
Affiliates
Adverts

 

 

Links
By using, viewing or obtaining any information contained on this site, you agree to the disclaimer.

© HellBound Hackers 2008- 2009. Since 3rd December 2004.