• 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

    • I can tell you I don't need one. I use Start11 to make it Win10 styled and custom game tiles so it's essentially an easier way to launch games than opening Steam but I definitely don't 'need' it and don't use it to launch any applications.
    • I was replying to a user comment that literally started with "So, user error then."
    • Is the Start menu in Windows actually useful? Do we even need it? by Usama Jawad A few days ago, I shared my viewpoint regarding how Microsoft hasn't been able to convince me to migrate to Windows 11 as my sole daily driver, despite the OS being available for roughly four years. Within that piece, I highlighted the fact that even though the Start menu has been completely revamped in the latest version of Windows, it's not something that I use regularly. And that got me thinking: is the Start menu really a crucial piece of my Windows experience? I get it, I know that the Start menu is an iconic Windows element that has existed in the operating system for as long as many of us can remember. And yet, I can't really tell you about the last time I actually used the Start menu. The reason for italicizing the word "used" above is that while the Start menu can be launched when you click on the Windows icon in the Taskbar or the Windows key, I am not counting that as actual usage for the purpose of this piece. This is because I do launch the Start menu several times a day, I actually use it to get to Windows Search. I don't actually use the list of apps, app folders, and recently opened files. I simply tap the Windows key and start typing the search query for the item that I am looking for. For example, if I want to open Power BI, I don't open the Start menu and find the pinned app. I just tap on the Windows key and start typing "Po..." and quickly hit Enter as soon as the right option comes up, which is usually after a couple of keystrokes. Similarly, if I want to open "Gifts.xlsx", I don't open Excel from the Start menu or even peruse my list of recently opened files, I just start typing "Gi..." and press Enter. I feel like this is a much faster experience and you don't need to rely on muscle memory to find the item that you are looking for. And as your list of installed software grows, it only becomes more tedious to keep track of where you pinned an app or the folder that you kept it in. Windows Search gets rid of that problem completely. Additionally, the Start menu detracts me from whatever else I may be focusing on in parallel. If I am using Word and now want to open PowerPoint, I would probably get distracted by the Start menu as I launch it and try to locate the PowerPoint icon. Instead, I can just get to PowerPoint within a few keystrokes without even realizing the effort involved. I'd also like to highlight that I am not inherently against the idea of pinning stuff that you use frequently or recently, but I do think that the Taskbar and Desktop view already solves that problem to some extent. Right now, my Taskbar contains my most used software, while my desktop contains other secondary software and files. I understand that people have their fair share of complaints against Windows Search, but it works quite well for my use-cases. As it currently stands, my actual usage of the Start menu is nil. And although this popular Windows UX is always the subject of debate each time Microsoft makes even a minor change to it, I simply couldn't care less. That said, while the Start menu in its traditional use is dead to me, I am not sure how many people actively utilize this UX. Any evidence I have is purely anecdotal, but I would be very interested in seeing the telemetry that Microsoft tracks regarding its usage. Seeing how frequently Microsoft makes changes to the Start menu and the ensuing debate that follows, I feel like I am in the minority. But I just can't imagine opening the Start menu and then relying on muscle memory or a fixed grid of icons to get to the app I need, when I can do more with a couple of keystrokes.
    • >Fixed: Certain displays might be unexpectedly green. I believe that this fixed the Windows mishandling Dolby Vision issue in RGB 12 bit Full mode we saw recently.
  • Recent Achievements

    • Week One Done
      Wayne Robinson earned a badge
      Week One Done
    • One Month Later
      Karan Khanna earned a badge
      One Month Later
    • Week One Done
      Karan Khanna earned a badge
      Week One Done
    • First Post
      MikeK13 earned a badge
      First Post
    • Week One Done
      OHI Accounting earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      681
    2. 2
      ATLien_0
      277
    3. 3
      Michael Scrip
      208
    4. 4
      +FloatingFatMan
      172
    5. 5
      Steven P.
      142
  • Tell a friend

    Love Neowin? Tell a friend!