Results 1 to 10 of 10
  1. #1
    lalakijilp's Avatar
    Join Date
    Jan 2008
    Gender
    male
    Posts
    310
    Reputation
    9
    Thanks
    53
    My Mood
    Blah

    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

  2. #2
    zeco's Avatar
    Join Date
    Jul 2009
    Gender
    male
    Location
    Canada
    Posts
    683
    Reputation
    12
    Thanks
    78
    My Mood
    Cynical
    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
    Last edited by zeco; 09-02-2009 at 12:02 PM.

  3. #3
    lalakijilp's Avatar
    Join Date
    Jan 2008
    Gender
    male
    Posts
    310
    Reputation
    9
    Thanks
    53
    My Mood
    Blah
    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

  4. #4
    rwkeith's Avatar
    Join Date
    Jul 2008
    Gender
    male
    Posts
    457
    Reputation
    11
    Thanks
    79
    My Mood
    Angelic
    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++

  5. #5
    lalakijilp's Avatar
    Join Date
    Jan 2008
    Gender
    male
    Posts
    310
    Reputation
    9
    Thanks
    53
    My Mood
    Blah
    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

  6. #6
    rwkeith's Avatar
    Join Date
    Jul 2008
    Gender
    male
    Posts
    457
    Reputation
    11
    Thanks
    79
    My Mood
    Angelic
    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...
    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++

  7. The Following 2 Users Say Thank You to rwkeith For This Useful Post:

    lalakijilp (09-06-2009),why06 (09-02-2009)

  8. #7
    lalakijilp's Avatar
    Join Date
    Jan 2008
    Gender
    male
    Posts
    310
    Reputation
    9
    Thanks
    53
    My Mood
    Blah
    so you can change a double to an int by putting (int) before it?

  9. #8
    rwkeith's Avatar
    Join Date
    Jul 2008
    Gender
    male
    Posts
    457
    Reputation
    11
    Thanks
    79
    My Mood
    Angelic
    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++

  10. #9
    why06's Avatar
    Join Date
    Jul 2009
    Gender
    male
    Location
    IBM
    Posts
    4,304
    Reputation
    170
    Thanks
    2,203
    My Mood
    Flirty
    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.

    "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

  11. #10
    why06's Avatar
    Join Date
    Jul 2009
    Gender
    male
    Location
    IBM
    Posts
    4,304
    Reputation
    170
    Thanks
    2,203
    My Mood
    Flirty
    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.

    "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

Similar Threads

  1. fuck the new york times
    By ace76543 in forum Flaming & Rage
    Replies: 13
    Last Post: 08-15-2008, 01:58 AM
  2. Who the fuck put there time into making this.
    By radnomguywfq3 in forum General
    Replies: 12
    Last Post: 06-28-2008, 12:54 PM
  3. Click The Link For The Time Of Your Life!
    By -TM- in forum Spammers Corner
    Replies: 0
    Last Post: 03-24-2008, 02:22 PM
  4. PB down for the time being
    By Hispiforce in forum WarRock - International Hacks
    Replies: 3
    Last Post: 03-04-2008, 03:27 PM
  5. Changing the HP,SP,and flag times
    By applesauce in forum WarRock - International Hacks
    Replies: 6
    Last Post: 06-11-2006, 10:09 PM

Tags for this Thread