• 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

    • Another win for EU users? Ads in WhatsApp won't be coming this year by David Uzondu You might have heard that ads are making their way to WhatsApp after years of the company promising it would never happen. If you are in the EU (lucky you), you won't be seeing ads until 2026 at the earliest. A new report from POLITICO confirms that Meta, which owns the messaging service, has informed Ireland's privacy regulator that the new advertising model will not roll out in the European Union for quite some time, even as it appears elsewhere in the coming months. This is not some charitable act, of course. The delay gives European regulators time to scrutinize the plan, which involves using ad preferences from linked Facebook and Instagram accounts to target users. This situation follows a pattern of other "wins" for EU users, like the changes in iOS 17.4 that finally enabled sideloading. This opened the door for alternative app stores and the (temporary) return of games like Fortnite to iPhones in the region. Similarly, we are seeing Microsoft finally back off from shoving Edge down the throats of EU users, all thanks to the Digital Markets Act. This legislation has put pressure on big tech companies to operate more "fairly" within the bloc, leading to changes that users everywhere else can only dream of for now. These regulations are precisely what companies like Apple hate. Remember, Apple has issued a warning to Australia, telling the country not to follow Europe's lead on these matters because it would create massive security and privacy risks. Apple argues that its control over the ecosystem keeps users safe, so any attempt to break that open is dangerous. The Irish Data Protection Commission will be meeting with WhatsApp to discuss the matter further. According to Commissioner Des Hogan, they plan to discuss the ad model with other European data protection authorities to gather any collective concerns. Commissioner Dale Sunderland noted that discussions with the company are "still early days", and it is too soon to identify what, if any, specific "red line issues" might exist with Meta's advertising plans. For now, Europeans can continue using their ad-free messenger, while the rest of the world prepares for the inevitable.
    • Welcome to Neowin!
    • Idiots never imagine their insane actions troubling everyone.  
    • Photo Variants 2.3 by Razvan Serea Photo Variants is an all-in-one photo editor for Windows. Quickly cull, import, and edit your images with powerful tools. Enjoy full layer support, precise retouching features, and a wide range of filters and color adjustments. Create multiple versions of a photo instantly with presets, or design from scratch using vector graphics and advanced editing options. Free for personal and commercial use. Photo Variants key Features: Advanced Adjustment Tools: Provides precise control over image modifications. ​ Extensive Filter Collection: Offers over 99 photo filters to apply various effects. ​ Animated Photo Effects: Enables the addition of dynamic elements to images. ​ Automatic Face Retouching: Includes features for enhancing facial features automatically. ​ Support for Multiple Formats: Compatible with over 100 graphic formats, including RAW and PSD files, allowing users to open, edit, and save in these formats. ​ Drawing and Transformation Tools: Facilitates freehand drawing, erasing, filling, cropping, resizing, rotating, and flipping images. Photo Variants supports a wide array of image formats, making it a versatile tool for all your editing needs. Key supported formats include: Raster Formats: .jpeg, .jpg, .png, .bmp, .gif, .tiff, .webp, .ico, .pcx. Camera RAW: .crw, .cr2, .dng, .nef, .raf, .arw, .orf, .x3f, .raw. Professional Formats: .psd, .ai, .svg, .tga, .pdf, .pcl. Specialized Formats: .dicom, .dcm, .heic, .heif, .avif, .exr, .dds. Other: .wmf, .emf, .xps, .jpeg2000 (.jp2)...etc... With support for these formats, Photo Variants offers seamless editing and flexibility for photographers, designers, and creatives. Photo Variants 2.3 changes: New effects for layers. New shapes and options for brushes. Download: Photo Variants 2.3 | 70.5 MB (Freeware) View: Photo Variants Home page | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • Internal PSU for these applications is bueno. External would be prone to noise and that is exactly something you do not want.
  • 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
      692
    2. 2
      ATLien_0
      269
    3. 3
      Michael Scrip
      204
    4. 4
      +FloatingFatMan
      170
    5. 5
      Steven P.
      144
  • Tell a friend

    Love Neowin? Tell a friend!