• 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

    • Rumor: Apple eyeing over 5 million Samsung displays for '2026' foldable iPhone by David Uzondu You've probably been hearing rumors about Apple foldables for a while now, and in fact, this is perhaps the umpteenth article you've read about the topic. Still, new information from analyst Ming-Chi Kuo, claims that production of the first foldable iPhone could finally kick off later this year, pointing toward a launch in 2026. This news centers on a major component order, with Samsung being the exclusive supplier for the initial run, which neatly corroborates previous supply chain reports we have seen over the years. Apple and Samsung are supposedly working together to solve the one thing that has bugged people since foldables became a thing: the crease. Samsung has improved its panels over time, but Apple is reportedly pushing to have it nearly invisible before any phone ships to customers. This obsession with getting the hardware perfect feels very on-brand. After all, this is the company that is usually late to the party with new product categories, perfecting its take on devices like iPhones, iPads, the Apple Watch, and also AI. Okay, that last one isn't going smoothly for the company, but its track record with hardware is generally a different story. And the evidence for that hardware push is now getting much more specific. The new report from Kuo (translated) claims that Samsung Display is setting up a production line with the capacity of close to 8 million units. For context, the original Galaxy Fold sold less than a million units, and Samsung has been notoriously cagey about the exact number ever since. Despite their growing popularity, foldables are still a pretty niche category, but an initial order of this size, if true, suggests Apple thinks it can be the company to finally push them into the mainstream. Then again, Apple has not had a ton of success with its recent foray into the niche mixed-reality segment with the Apple Vision Pro, so maybe its magic touch has its limits. Of course, you should always take this sort of information with a grain of salt until you hear an official word from Apple. The specs are not finalized, and tech timelines can change depending on production issues or last-minute tweaks.
    • Cheaper maybe; effective, based on past experience, not likely. And yes, companies are on the market to generate profits but there is a huge difference between long terms plans and short ones generating profits but compromising the long term health, and consequently profits, of companies.
    • Lightweight = 100 MB, really? IrfanView is 4 MB!
    • Cool! Something no one wants that eats up storage space and battery life for free! /s
    • GT games are boring compared to Forza (Horizon or Motorsport)
  • Recent Achievements

    • First Post
      MikeK13 earned a badge
      First Post
    • One Month Later
      OHI Accounting earned a badge
      One Month Later
    • Week One Done
      OHI Accounting earned a badge
      Week One Done
    • First Post
      Thornskade earned a badge
      First Post
    • Week One Done
      Higante88 earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      699
    2. 2
      ATLien_0
      267
    3. 3
      Michael Scrip
      202
    4. 4
      +FloatingFatMan
      171
    5. 5
      Steven P.
      134
  • Tell a friend

    Love Neowin? Tell a friend!