Problem with functions... I think

Started by
13 comments, last by KittyRa 17 years, 9 months ago
Well, basically I have been doing c++ for about 2 months now. And I have just learnt how to do basic functions. But, knowing me, I've decided to hurdle before I can crawl. So, I have tried to create a temperature converter. It seems to work all the way up to the point when it actually needs to do the math, I think it has something to do with the variable types, as VS.net lets me compile and run the app, but gives me warnings about truncating. But, then again, nothing like this has happened before I started using functions, so I don't know which is going wrong. Anyways, heres the source: #include &lt;iostream&gt; using namespace std; int getFtemp(); //function prototypes int getCtemp(); int main() { char letter; cout &lt;&lt; "Please insert either \'c\' to convert FROM Celsius or \'f\' to convert FROM fahrenheit: "; cin &gt;&gt; letter; cin.ignore(); switch(letter) { case 'c' : getCtemp(); break; case 'f' : getFtemp(); break; default : cout &lt;&lt; "ERROR: You did not enter either \'c\' or \'f\'"; } cin.ignore(); cin.get(); } int getCtemp() { float Icelsius; int Rfahrenheit; cout &lt;&lt; "Enter a celsius temperatur that you want to convert into fahrenheit: "; cin &gt;&gt; Icelsius; cin.ignore(); Rfahrenheit = (((Icelsius*9)/5)+32); return (int) Rfahrenheit; } int getFtemp() { float factor = 5.0 / 9.0; float freezing = 32.0; int Rcelsius; float ftemp; cout &lt;&lt; "Enter the fahrenheit temperature that you want to convert into celsius: "; cin &gt;&gt; ftemp; cin.ignore(); Rcelsius = factor * (ftemp - freezing); return (int) Rcelsius; } </code> Thanks for reading, Samuel
"I used to wonder if there is a god, and now I know there is... and it's me!"
Advertisement
I don't get it. What exactly is the problem? What do you want it to do and the program does not do it?
Firstly, it is refreshing to see someone trying such a nice simple application when starting out.

Secondly, it would be helpful if you could specify the WAY in which your code is not working.

With this in mind, the only immediatley obvious things to me are that you are not doing anything with the returns from the functions, so they are not being output at all. Also, the truncating warnings will be to do with converting from floats to ints. Could you not just use floats throughout the code, as the return values from your functions? I would have thought the final result would want to be a float as well.

Sorry if this does not help and if you could post with exactly what the problem is, it would be more useful.

[EDIT] Faaar to slow.
There is also no 'return 0;' in the main function.
Well, first of all, thanks for replying. The thing thats not working is that, when the user enters the values to be converted, the program doesn't give an answer, it just leaves a blank gap.

EasilyConfused: I get what your saying, but I am unable to get that into the code. Here is the new code (all the changes are in the function definitions).

#include <iostream>
using namespace std;

int getFtemp(); //function prototypes
int getCtemp();

int main()
{
char letter;

cout << "Please insert either \'c\' to convert FROM Celsius or \'f\' to convert FROM fahrenheit: ";

cin >> letter;
cin.ignore();

switch(letter)
{
case 'c' : getCtemp(); break;
case 'f' : getFtemp(); break;
default : cout << "ERROR: You did not enter either \'c\' or \'f\'";
}

cin.ignore();
cin.get();
}

int getCtemp()
{
float Icelsius;
int Rfahrenheit;

cout << "Enter a celsius temperatur that you want to convert into fahrenheit: ";
cin >> Icelsius;
cin.ignore();

Rfahrenheit = (((Icelsius*9)/5)+32);

return (float) Rfahrenheit;

cout << Rfahrenheit << "F";
}

int getFtemp()
{
float factor = 5.0 / 9.0;
float freezing = 32.0;
int Rcelsius;
float ftemp;

cout << "Enter the fahrenheit temperature that you want to convert into celsius: ";
cin >> ftemp;
cin.ignore();

Rcelsius = factor * (ftemp - freezing);

return (float) Rcelsius;

cout << Rcelsius << "C";
}

EDIT: EkimGram - I didn't think that it was neccessary.
"I used to wonder if there is a god, and now I know there is... and it's me!"
You are nearly there dude. You just have your cout << result; statements after your return statements, so they never get executed.

int getCtemp(){float Icelsius;int Rfahrenheit;cout << "Enter a celsius temperatur that you want to convert into fahrenheit: ";cin >> Icelsius;cin.ignore();Rfahrenheit = (((Icelsius*9)/5)+32);cout << Rfahrenheit << "F" << endl;return (float) Rfahrenheit;}int getFtemp(){float factor = 5.0 / 9.0;float freezing = 32.0;int Rcelsius;float ftemp;cout << "Enter the fahrenheit temperature that you want to convert into celsius: ";cin >> ftemp;cin.ignore();Rcelsius = factor * (ftemp - freezing);cout << Rcelsius << "C" << endl;return (float) Rcelsius;}


OR

int main(){char letter;cout << "Please insert either \'c\' to convert FROM Celsius or \'f\' to convert FROM fahrenheit: ";cin >> letter;cin.ignore();switch(letter){case 'c' : cout << getCtemp() << endl; break;case 'f' : cout << getFtemp() << endl; break;default : cout << "ERROR: You did not enter either \'c\' or \'f\'";}cin.ignore();cin.get();}


Note the endl at the ends of the cout lines, to print a newline.

HTH

[EDIT] return 0; isn't STRICTLY necessary since it is considered explicit at the end of main (and only main) if it is not included. Many people (including me) do consider it bad form though, since it can encourage you to forget it in other functions where there is no implicit return.
Quote:
int getCtemp()
{
float Icelsius;
int Rfahrenheit;

cout << "Enter a celsius temperatur that you want to convert into fahrenheit: ";
cin >> Icelsius;
cin.ignore();

Rfahrenheit = (((Icelsius*9)/5)+32);

return (float) Rfahrenheit;

cout << Rfahrenheit << "F";
}

int getFtemp()
{
float factor = 5.0 / 9.0;
float freezing = 32.0;
int Rcelsius;
float ftemp;

cout << "Enter the fahrenheit temperature that you want to convert into celsius: ";
cin >> ftemp;
cin.ignore();

Rcelsius = factor * (ftemp - freezing);

return (float) Rcelsius;

cout << Rcelsius << "C";
}

I've bolded the problem. Your function is ending before it gets to print the values. Put the return statements after the cout <<'s

Edit: beaten to it...by several minutes. Darn.

Edit Edit: Removed unnecessary change.

[Edited by - DigiDude on July 20, 2006 12:14:15 PM]
F-R-E-D F-R-E-D-B-U-R...G-E-R! - Yes!
Well, I finially got it working. So thanks to all you guys! If this program ever becomes a success I will make sure to mention you (next stop: Buying Microsoft).

Anyway, my final code:

#include <iostream>using namespace std;int getFtemp();		//function prototypesint getCtemp();int main(){	char letter;	cout << "Please insert either \'c\' to convert FROM Celsius or \'f\' to convert FROM fahrenheit: ";	cin >> letter;	cin.ignore();	switch(letter)	{	case 'c' : getCtemp(); break;	case 'f' : getFtemp(); break;	default : cout << "ERROR: You did not enter either \'c\' or \'f\'";	}	cin.get();	return 0; //Added return to bottom of code, after all I don't want to have bad form do I ;)}int getCtemp(){	float Icelsius;	float Rfahrenheit; // Vhangeed to float		cout << "Enter a celsius temperatur that you want to convert into fahrenheit: ";	cin >> Icelsius;	cin.ignore();	Rfahrenheit = (((Icelsius*9)/5)+32);	cout << Rfahrenheit << "F"; // Moved above return	return (float) Rfahrenheit;}int getFtemp(){	float factor = 5.0 / 9.0;	float freezing = 32.0;	float Rcelsius; // Changed float variable	float ftemp;	cout << "Enter the fahrenheit temperature that you want to convert into celsius: ";	cin >> ftemp;	cin.ignore();	Rcelsius = factor * (ftemp - freezing);	cout << Rcelsius << "C"; // Moved above return		return (float) Rcelsius;}


Thanks again,
Samuel

EDIT: The program doesn't work again, when I place the return at the end of the main program. Is it supposed to go before or after cin.get();
"I used to wonder if there is a god, and now I know there is... and it's me!"
Ekim_Gram: Not necessary. Yes, this is standard.

DigiDude: Your "correcion" to the arithmetic is quite incorrect.

To the OP: You don't need to declare variables for everything. You can return an expression. Casts apply to expressions as well.

You also do not have to put your variable declarations at the top of scope, and it is recommended that you not do so. Instead, put them near first use, and when possible, initialize them with their first value. Initialization is different from assigning.

// BADint x;do_stuff_not_involving_x();x = 5;// ALSO BADint x = 0;do_stuff_not_involving_x();x = 5;// GOODdo_stuff_not_involving_x();int x = 5;
Quote:Original post by Zahlman
You also do not have to put your variable declarations at the top of scope, and it is recommended that you not do so. Instead, put them near first use, and when possible, initialize them with their first value. Initialization is different from assigning.

// BADint x;do_stuff_not_involving_x();x = 5;// ALSO BADint x = 0;do_stuff_not_involving_x();x = 5;// GOODdo_stuff_not_involving_x();int x = 5;



I will take that into account next time.
"I used to wonder if there is a god, and now I know there is... and it's me!"

This topic is closed to new replies.

Advertisement