in my tutorial was a program to calculate the time delay to the mars.
the output is:
Time delay when talking to Mars: 182.796 seconds. This is 3.04659 minutes.
now i wanted to make to look like this:
Time delay when talking to Mars is 3 minutes and ?? seconds.
this is what i did:
but it didn't work out so how can i make it work?
edit: oops wrong title i mean mars. xD#include <iostream>
using namespace std;
int main ()
{
double distance, lightspeed, delay, delay_in_min, seconds;
int delay_in_min_2;
/* i added int delay_in_min_2; to calculate the seconds left after the whole minutes (minutes-minutes rounded)*60 */
distance = 34000000.0;
lightspeed = 186000;
delay = distance/lightspeed;
cout << "Time delay when talking to mars: " << delay << " seconds." << endl;
delay_in_min= delay/60.0;
cout << "This is " << delay_in_min << " minutes." <<endl;
seconds = (delay_in_min-delay_in_min_2)*60;
// [Warning ] converting to int from double. how to do it else?
delay_in_min_2= ((distance/lightspeed)/60);
cout << "Time delay when talking to mars: " << delay_in_min_2 <<" minutes and " << seconds <<endl;
system ("pause");
return 0;
}
Is this C#? Cause I tried working with it and I have a few undefined functions.
Last edited by rwkeith; 09-02-2009 at 02:54 PM.
Goals In Life:
[X] Become an Advanced Member
[X]Release a tut on mpgh
[0]Post 300 posts
[X]Make a working hack
[X] Learn c++
no, it is C++
this was the error.
[Warning ] converting to int from double.
i put it in the code too. hwo to do it otherwise
Ah, I see almost complete with a solution. By the way, did you get an error about your delay_in_min_2? I had to fix it because my compiler said unknown identifier.
Ok, simply when you want your answer to be a different type place the type you want it to be in parenthensies. Ex.:Code://myfirst.cpp--displays a msg #include <iostream> using namespace std; int main () { double distance; double lightspeed; double delay; double DelayInMin; double seconds; int DelayInMin2; // I smoothed things into my own style a little =) distance = 34000000.0; lightspeed = 186000; delay = distance/lightspeed; cout << "Time delay when talking to mars: " << delay << " seconds." << endl; DelayInMin = delay/60.0; cout << "This is " << (int)DelayInMin << " minutes." <<endl; DelayInMin2 = ((distance/lightspeed)/60);//You must declare the variable before using it =), or at least in my case =S seconds = (DelayInMin-DelayInMin2)*60; // [Warning ] converting to int from double. how to do it else? cout << "Time delay when talking to mars: " << (int)DelayInMin2 << " minutes and " << (int)seconds << " seconds."; cin.get(); return 0;
The result of this would be 10...Code://This is an example =P double test = 10.56567 cout << "I will reveal to you an integer: " << (int)test;
Last edited by rwkeith; 09-02-2009 at 03:17 PM.
Goals In Life:
[X] Become an Advanced Member
[X]Release a tut on mpgh
[0]Post 300 posts
[X]Make a working hack
[X] Learn c++
lalakijilp (09-06-2009),why06 (09-02-2009)
so you can change a double to an int by putting (int) before it?
Yes and ignore the (int) I put in this line of code, it isn't required
Goals In Life:
[X] Become an Advanced Member
[X]Release a tut on mpgh
[0]Post 300 posts
[X]Make a working hack
[X] Learn c++
Yes. it's called casting a variable. It forces a variable to another type. In Java compilers won't even allow a double value to be entered into a int without casting, but C++ allows a lot more freedom in this regard with it's automatic type conversion. The problem with automatic type conversion is that it can lead to ambiguity too.
Also here is how I would have coded your delay calculator:
I used and operator called mod "%" to only return the remainder when I divided the seconds by 60. right now I'm trying to work on a program that will automatically give minutes, hours, days, seconds, etc. depending on the amount of delay. The trouble is keeping all the "/60" and "%60" in my head. I think I will create a funtion to handle this that way only have to call that function and can keep the math out of the cout statements.Code:#include <iostream> using namespace std; int main() { double distance, delay; double SoL = 186000; //miles per second cout << "LightSpeed Delay Calculator\n" ; cout << "Enter distance in miles: \n"; cin >> distance; int seconds = (int)(distance/SoL); cout << "The time delay for a distance of " << distance << " miles is " << seconds/60 << " minutes and " << seconds%60 << " second(s)."; system("pause"); return 0; }
"Every gun that is made, every warship launched, every rocket fired signifies, in the final sense, a theft from those who hunger and are not fed, those who are cold and are not clothed. This world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children. The cost of one modern heavy bomber is this: a modern brick school in more than 30 cities. It is two electric power plants, each serving a town of 60,000 population. It is two fine, fully equipped hospitals. It is some fifty miles of concrete pavement. We pay for a single fighter plane with a half million bushels of wheat. We pay for a single destroyer with new homes that could have housed more than 8,000 people. This is, I repeat, the best way of life to be found on the road the world has been taking. This is not a way of life at all, in any true sense. Under the cloud of threatening war, it is humanity hanging from a cross of iron."- Dwight D. Eisenhower
Well it took long enough, and got kinda confusing at times, and I could have gone about it a different way, but here is my completed version that automatically calculates years, days, hours, minutes, and seconds.
Unfortunately it breaks if you inter a value bigger then an integer can hold. It also breaks if you enter something that isn't a number. But hey this is all the effort I'm gonna put into it since I will probably never use it :P
you can compile this code and it should run.Code:#include <iostream> using namespace std; void time(double seconds); int main() { double distance, delay; double SoL = 186000; //miles per second cout << "LightSpeed Delay Calculator\n" ; cout << "Enter distance in miles: \n"; cin >> distance; delay = distance/SoL; cout <<"The calculated time delay is: "; time(delay); cout<<endl ; system("pause"); return 0; } // a function that will take parameters in seconds and convert to cyclical time. void time(double seconds) { int minute = 60; int hour = 3600; int day = 3600*24; int year = 3600*24*365; int second = (int)seconds; int perfectseconds = second%minute; int perfectminutes = ((second/minute)%60); int perfecthours = ((second/hour)%24); int perfectdays = ((second/day)%365); int perfectyears = (second/year); if(seconds < year) { if(seconds < day) { if(seconds < hour) { if(seconds < minute) { cout << (int)seconds << " seconds"; } else cout << perfectminutes << "minutes and " << perfectseconds << " seconds"; } else cout << perfecthours << "hours " << perfectminutes << " minutes and " << perfectseconds << "seconds"; } else cout << perfectdays << "days " << perfecthours << " hours " << perfectminutes << " minutes " << perfectseconds << "seconds"; } else cout << perfectyears << "years " << perfectdays << " days" << perfecthours << " hours " << perfectminutes << " minutes " << perfectseconds << "seconds"; }
"Every gun that is made, every warship launched, every rocket fired signifies, in the final sense, a theft from those who hunger and are not fed, those who are cold and are not clothed. This world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children. The cost of one modern heavy bomber is this: a modern brick school in more than 30 cities. It is two electric power plants, each serving a town of 60,000 population. It is two fine, fully equipped hospitals. It is some fifty miles of concrete pavement. We pay for a single fighter plane with a half million bushels of wheat. We pay for a single destroyer with new homes that could have housed more than 8,000 people. This is, I repeat, the best way of life to be found on the road the world has been taking. This is not a way of life at all, in any true sense. Under the cloud of threatening war, it is humanity hanging from a cross of iron."- Dwight D. Eisenhower