2 Replies - 167 Views - Last Post: 19 November 2012 - 02:37 PM Rate Topic: -----

#1 GrowingDark  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 38
  • Joined: 09-March 11

C++ Homework Help

Posted 19 November 2012 - 02:25 PM

Could somebody please tell me why my degrees Celsius is always zero?

#include <iostream>
#include <iomanip>

using namespace std;

double input();
double FtoC(double f);
void output(double f, double c);

int main()
{
	double f,c;
	
	f = input();
	while (f != -999)
	{
		c = FtoC(f);
		output(f,c);
		f = input();
	}

	return 0;
}

double input()
{
	double f;

	cout << "Please enter degrees fahrenheit (-459.67 or more) or -999 to quit: ";
	cin >> f;
	while(f < -459.67 && f != -999)
	{
		cout << "Error! Must be -459.67 or more: ";
		cin >> f;
	}

	return f;
}

double FtoC(double f)
{
	double c;
	c = (f-32)*(5/9);

	return c;
}

void output(double f, double c)
{
	cout << endl << endl << f << " degrees fahrenheit is " << c << " degrees celsius\n\n";
}


Is This A Good Question/Topic? 0
  • +

Replies To: C++ Homework Help

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2100
  • View blog
  • Posts: 3,198
  • Joined: 21-June 11

Re: C++ Homework Help

Posted 19 November 2012 - 02:33 PM

c = (f-32)*(5/9);


5 and 9 are integers, so 5/9 performs integer division, resulting in 0. This 0 is then converted to double and multiplied with the result of f-32, resulting in 0.0 which is then assigned to c.

To perform the division using floating point arithmetic, at least one of the operands should be a floating point value.
Was This Post Helpful? 1
  • +
  • -

#3 GrowingDark  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 38
  • Joined: 09-March 11

Re: C++ Homework Help

Posted 19 November 2012 - 02:37 PM

Gah! *facepalm* Well I feel stupid now. Thanks man.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1