• 0

C++ program converting hours and minutes to decimal?


Question

This is what I got so far:

 

#include <iostream>
using namespace std;

int main()
{
    int hours = 0;
    int minutes = 0;

    cout << "Enter number of hours and minutes: ";
    cin >> hours >> minutes;

    cout << "The number of hours is: " << hours << endl;
    cout << "and the number of minutes is: " << minutes << endl;

    return 0;
}
 

 

Much help will be appreciated, thank you!

 

7 answers to this question

Recommended Posts

  • 0

Can you clarify your problem? I don't understand what you are asking. Do you want to convert hours to minutes, represent hours and minutes as a single integer, merely display the values the user enters, or something else entirely?
 

  On 17/10/2013 at 00:23, Lord Method Man said:
float decTime = (float)hours + ((float)minutes / 60.0);

Give the OP's obvious beginner status, that may very well be the answer he is looking for. However if his question is to be taken literally, a floating point result is clearly not the same as decimal. Therefore if he is looking for the type of solution you proposed, I think it is likely that he wants to convert hours to minutes, not the reverse.

  • 0

Thank you Lord Method Man, well here is the problem that I am given +xorangekiller and I am kind of new at this:

 

Write a program that estimates the temperature in a freezer ( in degrees Celsius ) at some time ( t) after a power failure. The user should be prompted to enter the elapsed time since the power failure ( in hours and minutes ). The temperature ( T ) is modeled by the formula
T = 4t2/(t + 2 ) ? 20 ( t is assumed to be given in units of hours ). Based on Temperature (T) formula, convert Degrees Celsius into Degrees Fahrenheit modeled by the formula C* 9/5 +32.

 

I want to first convert the hours and minutes into decimal like for example 2 hours and 30 minutes into 2.5 and then use it for that for the Degrees Celsius formula which is T = 4t2/(t+2)-20 and convert it into Degrees Fahrenheit. I am just missing the conversion of hours and minutes into decimal to input for t and the conversion for Degrees Fahrenheit.

 

This is what I got:

 

#include <iostream>

using namespace std;
int main()
{
int time;
double elapsedTime;
cout<<"Enter a time for elapse"<<endl;
cin >> time;
elapsedTime = (4*(time*time))/(time 2)-20;
cout<< "Temp is " << elapsedTime<<endl;
return 0;
}

  • 0

If all you are missing is the conversion of hours and minutes into hours, then Lord Method Man's solution will work. However there are a couple things of which you should be aware. First, in programmer's parlance  a "decimal number" refers exclusively to an integer. You should use the term "floating point number" to refer to fractional numbers instead. Second, Neowin has a "code" tag. Please use it. It makes the source code you post much easier to read.

  • 0

I just changed my code a bit:

 

#include <iostream>
using namespace std;

int main ()
{
        int hours = 0;
        double minutes = 0;
        double Celsius = 0;
        double Fahrenheit = 0;
        double T = 0;

        Fahrenheit = Celsius*(9/5)+32;
        Celsius = (4*(T*T))/(T+2)-20;
        T = hours+(minutes/60);

        cout << "How many hours and minutes has the power has been shut off?";
        cin >> hours >> minutes;

        cout << " After the power has been shut off for " << hours + (minutes/60) << " hours," << endl;

        cout << " the temperature in the freezer is about " << (4*(T*T))/(T+2)-20 << " degrees Celsius"  << endl;

        cout << " or " << (Celsius*(9/5))+32 << " degrees Fahrenheit" << endl;

        return 0;
}

 

 

So the conversion to time works, the problem now is just that the time isn't converting into the right temperature

  • 0

You have several major problems with this revision, but they are all related to the same thing. You assign your key variables before accepting the values that should be used to calculate them. Remember, variables store concrete results, not formulas! Therefore Farenheight, Celcius, and T are effectively constant in your implementation. Fahrenheit = 0.0*(9/5)+32 = 32.0; Celsius = (4*(0.0*0.0))/(0.0+2)-20 = -20.0; T = 0+(0.0/60) = 0.0; They are not influenced by your user's input in any way.

 

The first step to correcting your problems is to calculate Farenheight, Celcius, and T after prompting your user for hours and minutes, not before. You should also use the results stored in those variables thereafter rather than doing the calculations every time.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • Burrrrn. Bought COD WWII last month when it was on sale for PC. Oh well. Excellent game tho, and the PC version plays/looks amazing (and has it's own PC achievements). ...wondering if this is a lesson on waiting on sales. ...also hoping Rise of the Tomb Raider has it's own PC Achievements
    • Google Chrome 137.0.7151.120 (offline installer) by Razvan Serea The web browser is arguably the most important piece of software on your computer. You spend much of your time online inside a browser: when you search, chat, email, shop, bank, read the news, and watch videos online, you often do all this using a browser. Google Chrome is a browser that combines a minimal design with sophisticated technology to make the web faster, safer, and easier. Use one box for everything--type in the address bar and get suggestions for both search and Web pages. Thumbnails of your top sites let you access your favorite pages instantly with lightning speed from any new tab. Desktop shortcuts allow you to launch your favorite Web apps straight from your desktop. Chrome has many useful features built in, including automatic full-page translation and access to thousands of apps, extensions, and themes from the Chrome Web Store. Google Chrome is one of the best solutions for Internet browsing giving you high level of security, speed and great features. Important to know! The offline installer links do not include the automatic update feature. Google Chrome 137.0.7151.120 changelog: [$7000][420697404] High CVE-2025-6191: Integer overflow in V8. Reported by Shaheen Fazim on 2025-05-27 [$4000][421471016] High CVE-2025-6192: Use after free in Profiler. Reported by Chaoyuan Peng (@ret2happy) on 2025-05-31 [425443272] Various fixes from internal audits, fuzzing and other initiatives Download web installer: Google Chrome Web 32-bit | Google Chrome 64-bit | Freeware Download: Google Chrome Offline Installer 64-bit | 128.0 MB Download: Google Chrome Offline Installer 32-bit | 115.0 MB Download page: Google Chrome Portable Download: Google Chrome MSI Installers for Windows (automatic update) View: Chrome Website | Release Notes Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • -Drop the art style, it's cool but doesn't fit the franchise at all. -Make it a gritty single player game, like Halo -Include deathmatch and all that stuff, extraction can be a separate mode If they don't do that, it's dead on arrival IMO.
  • Recent Achievements

    • One Month Later
      Custom Greek Shirts earned a badge
      One Month Later
    • Week One Done
      Custom Greek Shirts earned a badge
      Week One Done
    • One Year In
      Custom Greek Shirts earned a badge
      One Year In
    • Week One Done
      topantidetectbrowser earned a badge
      Week One Done
    • Explorer
      Jdoe25 went up a rank
      Explorer
  • Popular Contributors

    1. 1
      +primortal
      672
    2. 2
      ATLien_0
      281
    3. 3
      Michael Scrip
      223
    4. 4
      +FloatingFatMan
      190
    5. 5
      Steven P.
      145
  • Tell a friend

    Love Neowin? Tell a friend!