time delay to the moon

Posts 110 of 10 · Page 1 of 1
time delay to the moon
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?

#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;

}
edit: oops wrong title i mean mars. xD
Quote Originally Posted by lalakijilp View Post
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
Well one thing is when you first used delay_in_min_2, it wasn't given a value yet
Quote Originally Posted by zeco View Post
Well one thing is when you first used delay_in_min_2, it wasn't given a value yet
i changed it but it wont work yet
Is this C#? Cause I tried working with it and I have a few undefined functions.
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.
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;
Ok, simply when you want your answer to be a different type place the type you want it to be in parenthensies. Ex.:

Code:
//This is an example =P

double test = 10.56567
cout << "I will reveal to you an integer: " << (int)test;
The result of this would be 10...
so you can change a double to an int by putting (int) before it?
Quote Originally Posted by lalakijilp View Post
so you can change a double to an int by putting (int) before it?
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:

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;
}
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.
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

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";
}
you can compile this code and it should run.
Yes and ignore the (int) I put in this line of code, it isn't required
Posts 110 of 10 · Page 1 of 1
This thread is closed for replies.

Similar Threads

Tags for this Thread

Need help?