Another C++ problem
Results 1 to 3 of 3

Thread: Another C++ problem

Hybrid View

  1. #1
    Senior Member
    Join Date
    Jun 2004
    Posts
    379

    Another C++ problem

    Sorry to keep posting my C++ programing problems but I'm still learning and have questions.
    so here is my question I'm trying to take a float variable that is holding the amount of seconds it take for something to happen such as 1928.61582 seconds and break that down into minutes and seconds.

    now transTime needs to be a float or i loose the decimal places rite from the beginning which is not good because my time will be off but my code will not work unless every thing is an int which doesn't work either because i need the time for seconds out to the 5th decimal place and if its an int and i force the decimal place i just get all zeros which isnt any good any way. hope you guys understand if not just ask more questions.

    float transTime = 1928.61582 // total seconds that something takes to happen
    int minSec = 60 // number of seconds in a minute
    int minutes = 0
    float seconds = 0

    minutes = transTime / minSec; // gives me the minutes
    seconds = transTime % minSec; // gives me the seconds

    cout << "The ammount of time is: " << '\n' // print out time
    << fixed << showpoint << minutes << " minutes " << setprecision(5) << seconds << " seconds" << '\n';

  2. #2
    Custom User
    Join Date
    Oct 2001
    Posts
    503
    Bear in mind I've not actually compiled and checked this, and that I haven't written any C++ for years, but I think what you're after is:

    Code:
    float transTime = 1928.61582 // total seconds that something takes to happen
    int minSec = 60 // number of seconds in a minute 
    int minutes = 0
    float seconds = 0
    
    minutes = (int)(transTime / (float)minSec); // gives me the minutes 
    seconds = transTime &#37; (float)minSec; // gives me the seconds
    
    cout << "The ammount of time is: " << '\n' // print out time 
    << fixed << showpoint << minutes << " minutes " << setprecision(5) << seconds << " seconds" << '\n';
    What you have to do is explicitly cast minSec to a float to make sure that you don't lose the precision from the float before you've carried out the divide or mod operation. What you may also want to do is explicitly round up or down, but I'm not sure how to do this in C++.

    I suspect declaring minSec as a float would have the same effect, and you wouldn't have to do the casts then.

    Also, seconds has to be a float if you want to store the seconds to 5 decimal places. It isn't possible to store decimal or floating point values in an integer.

    ac
    Last edited by gothic_type; October 6th, 2009 at 10:55 AM.

  3. #3
    Senior Member
    Join Date
    Jun 2004
    Posts
    379
    hey thank you for your help but i figured out a solution.

    I just added a var TMin to that dose nothing but hold the out out put of the minutes times minSec which gives me the amount of minutes in seconds then i just minus that from transTime which gives me the remaining seconds.


    transTime = packetAmmount / transSpeed;
    minutes = transTime / minSec;
    TMin = minutes * minSec;
    seconds = transTime - TMin;

    thank you for your help.

Similar Threads

  1. The Problem Saga Continues
    By The Texan in forum Operating Systems
    Replies: 16
    Last Post: June 22nd, 2006, 08:04 PM
  2. A Headache of an Email Problem
    By AngelicKnight in forum General Computer Discussions
    Replies: 14
    Last Post: June 15th, 2006, 04:04 AM
  3. 500 mile email problem
    By Tedob1 in forum Tech Humor
    Replies: 0
    Last Post: December 23rd, 2002, 03:58 PM
  4. C problem...
    By Rna in forum General Programming Questions
    Replies: 4
    Last Post: May 22nd, 2002, 07:03 AM
  5. Help! I've got a nasty IDE problem
    By thesecretfire in forum Hardware
    Replies: 16
    Last Post: May 17th, 2002, 12:31 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

 Security News

     Patches

       Security Trends

         How-To

           Buying Guides