• 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.