• 0

Problem with C++ code...


Question

Most of the code works well...

#include <iostream>
using namespace std;

#include <cmath>

  void windchill (double airtemp, double windspeed, int chillindex)

{

	if (0 >= chillindex > -25)
	{
  cout << "discomfort." << endl;
  return;
	}
	if (-25 >= chillindex > -45)
	{
  cout << "a risk of skin freezing (frostbite)." << endl;
  return;
	}
	if (-45 >= chillindex > -60)
	{
  cout << "exposed skin may freeze within minutes." << endl;
  return;
	}
	if (-60 >= chillindex)
	{
  cout << "exposed skin may freeze in under 2 minutes." << endl;
  return;
	}

	return;
}

int main()

{

double airtemp, windspeed;
int chillindex;

	airtemp = 0;
	windspeed = 0;
	chillindex = 0;

	windchill (airtemp, windspeed, chillindex);

	chillindex = 13.12 + (0.6215 * airtemp) - (11.37 * (pow(windspeed, 0.16))) + (0.3965 * airtemp * (pow(windspeed, 0.16)));

	cout << "Welcome to the wind chill index program." << endl;

	cout << "Please enter air temperature in degrees Celcius." << endl;
	cin >> airtemp;

	cout << "Now enter the wind speed in kilometers per hour." << endl;
	cin >> windspeed;

	if (windspeed < 4.8)
	{
  cout << "Wind chill only applies to wind speeds of over 4.8km/h" << endl;
  return 0;
	}

	cout << "If the temperature is " << airtemp << " degrees Celcius, and the wind speed is " << windspeed << " kilometers per hour..." << endl;
	cout << "The wind chill index will be " << chillindex << "." << endl;
	cout << "This could mean " << windchill << endl;

	return 0;

}

but in my output, I get something like this:

This could mean 0041FA8C
discomfort

and my wind chill index is always 13... the value doesn't change no matter what values I put in...

Why is this happening?

EDIT: UPDATED CODE

Edited by Ravager
Link to comment
Share on other sites

6 answers to this question

Recommended Posts

  • 0

Since you have a void function there's really no need to return anything in the function once you called it, and since you're using cout to try and print the function results, you're going to get hexadecimal code (I believe).

If you fixed the chillindex problem, you could run the function like this...

cout << "The wind chill index will be " << chillindex << "." << endl;
cout << "This could mean ";
windchill(airtemp, windspeed, chillindex);
cout << endl;

My code without chillindex problem fixed...

#include <iostream>
using namespace std;

#include <cmath>

void windchill (double airtemp, double windspeed, int chillindex)
{
if ((chillindex <= 0) && (chillindex > -25))
{
 cout << "discomfort." << endl;
 return;
}
if ((chillindex <= -25) && (chillindex > -45))
{
 cout << "a risk of skin freezing (frostbite)." << endl;
 return;
}
if ((chillindex <= 45) && (chillindex > -60))
{
 cout << "exposed skin may freeze within minutes." << endl;
 return;
}
if (chillindex <= -60)
{
 cout << "exposed skin may freeze in under 2 minutes." << endl;
 return;
}

return;
}

int main()

{

double airtemp, windspeed;
double chillindex;

airtemp = 0;
windspeed = 0;
chillindex = 0;

chillindex = 13.12 + (0.6215 * airtemp) - (11.37 * (pow(windspeed, 0.16))) + (0.3965 * airtemp * (pow(windspeed, 0.16)));

cout << "Welcome to the wind chill index program." << endl;

cout << "Please enter air temperature in degrees Celcius." << endl;
cin >> airtemp;

cout << "Now enter the wind speed in kilometers per hour." << endl;
cin >> windspeed;

if (windspeed < 4.8)
{
 cout << "Wind chill only applies to wind speeds of over 4.8km/h" << endl;
 return 0;
}

cout << "If the temperature is " << airtemp << " degrees Celcius, and the wind speed is " << windspeed << " kilometers per hour..." << endl;
cout << "The wind chill index will be " << chillindex << "." << endl;
cout << "This could mean ";
windchill(airtemp, windspeed, chillindex);
cout << endl;

return 0;

}

Edited by Vector9
Link to comment
Share on other sites

  • 0

Alright. I'm getting the junk now...

But what's happening is that the junk value never changes either... I'm guessing it's with my order.

No matter what my wind chill index is (it changes now), my program will always output "discomfort".

Link to comment
Share on other sites

  • 0
Alright. I'm getting the junk now...

But what's happening is that the junk value never changes either... I'm guessing it's with my order.

No matter what my wind chill index is (it changes now), my program will always output "discomfort".

if (0 >= chillindex > -25)

You can't do that like that. It gives the result of 0 >= chillindex and compares it to -25, which 0 and 1 (true and false, which are numbers in C) are always greater than. What you need to do is:

if (chillindex <= 0 && chillindex > -25)

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.