• 0

Argghh another C++ problem.


Question

//ENGG233 Lab3
//Lab section: 09
//File: lab3_02.cc
//Author: Robert Kwapisz 298712
//Created: October 02, 2004
//Last modified: October 02, 2004
//Topic: Using functions

#include <iostream>
using namespace std;

#include <cmath>
	void windchill (double airtemp, double windspeed, double chillindex)

{

	if (0 >= chillindex && chillindex > -25)
	{
  cout << "Discomfort." << endl;
  return;
	}
	if (-25 >= chillindex && chillindex > -45)
	{
  cout << "A risk of skin freezing (frostbite)." << endl;
  return;
	}
	if (-45 >= chillindex && 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;
	}

}

int main()

{

double airtemp, windspeed, chillindex;

	airtemp = 0.0;
	windspeed = 0.0;
	chillindex = 0.0;

	windchill (airtemp, windspeed, chillindex);

	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;
	}

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

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

	return 0;

}

The code works fine, except that when I start the program, it always outputs the first function as a line... even though it's voided.

For example, output when I first run the program is:

Discomfort.

Welcome to the wind chill index program.

Please enter air temperature in degrees Celcius.

I know where the discomfort is coming from, but why is it being initialized if it's voided?

Link to comment
Share on other sites

18 answers to this question

Recommended Posts

  • 0

Probably because you're calling it:

windchill (airtemp, windspeed, chillindex);

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

Also, just some pickiness on my part:

if (0 &gt;= chillindex &amp;&amp; chillindex &gt; -25)
{
 cout &lt;&lt; "Discomfort." &lt;&lt; endl;
 return;
}
else if (chillindex &gt; -45)
{
 cout &lt;&lt; "A risk of skin freezing (frostbite)." &lt;&lt; endl;
 return;
}
else if (chillindex &gt; -60)
{
 cout &lt;&lt; "Exposed skin may freeze within minutes." &lt;&lt; endl;
 return;
}
else
{
 cout &lt;&lt; "Exposed skin may freeze in under 2 minutes." &lt;&lt; endl;
 return;
}

Less checks and it doesn't go through every statement, unless it's less than -60.

Link to comment
Share on other sites

  • 0

Just a few questions.

First, am I right in using the return; functions in the void part? Basically I'm trying to tell the program that once the first requirement in the list has been met, it can stop going forward (to prevent duplicate outputs in other programs for example, there x can be great than 0 AND 5 or something like that)...

And also, I'm slightly confused about the void function... when I'm calling it... it basically IS returning a value, isn't it?

Link to comment
Share on other sites

  • 0
Just a few questions.

First, am I right in using the return; functions in the void part? Basically I'm trying to tell the program that once the first requirement in the list has been met, it can stop going forward (to prevent duplicate outputs in other programs for example, there x can be great than 0 AND 5 or something like that)...

And also, I'm slightly confused about the void function... when I'm calling it... it basically IS returning a value, isn't it?

return; isn't overly needed, and I actually missed those when I was looking through and being picky, so it's almost like an else if. I guess it depends on how you want to do it. I'd personally go with an if else there, but it doesn't overly matter, just a choice thing. It's not really returning anything. The calling function gets some memory that the return value is stored in, but in this case it's void, so it's not getting anything back.

Link to comment
Share on other sites

  • 0

No, a void function means it doesn't return anything.

You should use an if, elseif, else structure in your windchill function, that way only one 'case' will be run.

Link to comment
Share on other sites

  • 0
No, a void function means it doesn't return anything.

You should use an if, elseif, else structure in your windchill function, that way only one 'case' will be run.

Oh ok... so basically the way that's been suggested will keep me from getting more than one case returned?

Because what was happening with another one of my programs was that I had a value... let's say... 8, for example... and I had a series of if statements (no else if or else... let's say if x > 3 and if x > 5) I would get two returns, basically.

The method being suggested will sort of... abort the rest of the function if the first case is found to be true?

Link to comment
Share on other sites

  • 0
Oh ok... so basically the way that's been suggested will keep me from getting more than one case returned?

Because what was happening with another one of my programs was that I had a value... let's say... 8, for example... and I had a series of if statements (no else if or else... let's say if x > 3 and if x > 5) I would get two returns, basically.

The method being suggested will sort of... abort the rest of the function if the first case is found to be true?

Yep, if you use 'else if' then the first true case that is found will be run. Any other 'else if' section of that if block won't be run.

Link to comment
Share on other sites

  • 0
Oh ok... so basically the way that's been suggested will keep me from getting more than one case returned?

Because what was happening with another one of my programs was that I had a value... let's say... 8, for example... and I had a series of if statements (no else if or else... let's say if x > 3 and if x > 5) I would get two returns, basically.

The method being suggested will sort of... abort the rest of the function if the first case is found to be true?

With the returns yes. Without the return; statements in it, it will just skip keep going down until it finds the right case and then skip the rest (and in this case reach the end of a void function and return...and return is automatically put in void functions which is why I said they weren't really necessary unless you knew you wanted to return early and not execute some other code), in assembly it would keep jumping to the next place if it the condition wasn't met, and if it was, it would execute some code and then skip to the point after the last else. Which reminds me, set your compiler to output assembly code once and look at it. It's rather interesting what C++ turns into.

Link to comment
Share on other sites

  • 0

Well the thing is that in most cases you don't have your extra side functions making decisions and outputting stuff. One or the other. If I had written that program I probably would have made the windchill() function a string rather than void and have it return what I wanted to display, then call it in main() using something like cout << windchill(stuff).

Link to comment
Share on other sites

  • 0

Yeah, it's preferable to return that than to have the function print out the stuff. That way the caller function is in control of what happens. If it wanted to print it out to a file instead of stdout or do something other than print it out (which is probably not the case with your current code), it could.

Link to comment
Share on other sites

  • 0
The code works fine, except that when I start the program, it always outputs the first function as a line... even though it's voided.

For example, output when I first run the program is:

Discomfort.

Welcome to the wind chill index program.

Please enter air temperature in degrees Celcius.

I know where the discomfort is coming from, but why is it being initialized if it's voided?

I think you may be misunderstanding the use of the void type in C/C++. Perhaps if you started by telling us how you understand that the program works, we can help you understand what's really going on. I don't really understand what you're saying, about the function being 'voided'. The function is not voided out, uninitialized, or anything like that. It works just like any other function in C/C++. As a matter of fact, the void type is there in the language specifically so that functions that return no value can work identically to functions that do return a value.

void in C/C++ is a type, like int or any other type you can think of in the language. When used as a return type of a function, it means that the function returns 'nothing'. This is why you can use return; to leave the function. If the return type of the function were int, for example, you'd have to do something like return 0; from the function. Typically you might do this to communicate something specific to the calling function.

Aside from some extra checks that people have pointed out already, you are calling your windchill() function twice - once at the beginning of main() and once after you have read the variables in with cin(). Your first call to windchill() would look like this:

windchill ( 0.0, 0.0, 0.0 )

Called in this manner, the windchill() function will output "Discomfort" and then return to main(). (Trace through the program yourself to see what I'm talking about). Then your program gets the inputs, and then calls windchill() again with the inputs. So when you run the program, you will always see that initial output. Just remove the first call to windchill() and you will be set. But since you're learning, make sure you understand why that's causing the problem, and try to think about why you thought you needed that initial call.

Edited by SengokuX
Link to comment
Share on other sites

  • 0

I cleaned up your code a bit, modified unnecssary calls and variable passing and such. You also didn't have a case for when there is a positive windchill (+10 ?C, 10 windspeed)

//ENGG233 Lab3
//Lab section: 09
//File: lab3_02.cc
//Author: Robert Kwapisz 298712
//Created: October 02, 2004
//Last modified: October 02, 2004
//Topic: Using functions

#include &lt;iostream&gt;
using namespace std;

#include &lt;cmath&gt;


char * windchill (double chillindex)
{
	char * csReturn = "";

	if(0 &lt; chillindex)
  csReturn = "Comfortable temperature.\n";

	else if (0 &gt;= chillindex &amp;&amp; chillindex &gt; -25)
  csReturn = "Discomfort.\n";

	else if (-25 &gt;= chillindex &amp;&amp; chillindex &gt; -45)
  csReturn = "A risk of skin freezing (frostbite).\n";

	else if (-45 &gt;= chillindex &amp;&amp; chillindex &gt; -60)
  csReturn = "Exposed skin may freeze within minutes.\n";

	else if (-60 &gt;= chillindex)
  csReturn = "Exposed skin may freeze in under 2 minutes.\n";


	return csReturn;

}

int main(int argc, char * argv[])
{
	char * csDisplay = "";
	double airtemp, windspeed, chillindex;

	airtemp = 0.0;
	windspeed = 0.0;
	chillindex = 0.0;

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

	cout &lt;&lt; "Please enter air temperature in degrees Celcius: -&gt;";
	cin &gt;&gt; airtemp;

	cout &lt;&lt; "Now enter the wind speed in kilometers per hour. -&gt;";
	cin &gt;&gt; windspeed;

	if (windspeed &gt;= 4.8)
	{
  chillindex = 13.12 + (0.6215 * airtemp) - (11.37 * (pow(windspeed, 0.16))) + (0.3965 * airtemp * (pow(windspeed, 0.16)));
  csDisplay = windchill(chillindex);

  cout &lt;&lt; "\n\nIf the temperature is " &lt;&lt; airtemp &lt;&lt; " degrees Celcius and the wind speed is "
 	 &lt;&lt; windspeed &lt;&lt; " km / hr..." &lt;&lt; endl &lt;&lt; endl;
  cout &lt;&lt; "The wind chill index will be " &lt;&lt; chillindex &lt;&lt; " degress Celcius.\n \n" &lt;&lt; endl;
  cout &lt;&lt; "This could mean:" &lt;&lt; endl &lt;&lt; csDisplay &lt;&lt; endl;
	}

	else
  cout &lt;&lt; "Wind chill only applies to wind speeds of over 4.8km/h" &lt;&lt; endl;

	return 0;

}

Link to comment
Share on other sites

  • 0

Here I go being picky again:

char * windchill (double chillindex)

{

char * csReturn = "";

if(0 < chillindex)

return "Comfortable temperature.\n";

else if (chillindex > -25)

return "Discomfort.\n";

else if (chillindex > -45)

return "A risk of skin freezing (frostbite).\n";

else if (chillindex > -60)

return "Exposed skin may freeze within minutes.\n";

else

return "Exposed skin may freeze in under 2 minutes.\n";

}

Link to comment
Share on other sites

  • 0
Here I go being picky again:

char * windchill (double chillindex)

{

char * csReturn = "";

if(0 < chillindex)

return "Comfortable temperature.\n";

else if (chillindex > -25)

return "Discomfort.\n";

else if (chillindex > -45)

return "A risk of skin freezing (frostbite).\n";

else if (chillindex > -60)

return "Exposed skin may freeze within minutes.\n";

else

return "Exposed skin may freeze in under 2 minutes.\n";

}

*cough* csReturn *cough* :p

Link to comment
Share on other sites

  • 0
*cough* csReturn *cough*  :p

Naw, I *think* that should work, but I'm pretty tired right now, so don't take my word for it.

Edit: Ah wait are you saying to take it out (the definition) or to put it back in (setting it and returning it)?

Link to comment
Share on other sites

  • 0
Naw, I *think* that should work, but I'm pretty tired right now, so don't take my word for it.

Edit: Ah wait are you saying to take it out (the definition) or to put it back in (setting it and returning it)?

Just saying you don't need to declare csReturn.

Link to comment
Share on other sites

  • 0
Just saying you don't need to declare csReturn.

Heh, told you I was tired, I tend to miss things like that when I am. Not to mention when I hack up other people's code I miss things too. Good call though ;)

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.