Calculations in a Function

First off im new to the forum so Hi and thanks in advanced :)

New to C++ so bare with me, as part of uni assignemnts im working through various chapters, this one is Functions, had a brows through various books and web pages but none really simple and/or with calculations in a contained function.

I need to write a program that will take a users input (this being a Fahrenheit) then covert it to Celsius, the calculation needs to be contained in a Function.The calculation is subtract 32, multiply by 5 then divide by 9

not sure how i would go about writing this??
If you show me some code i will help you out....

please use [code] tags, its on the right hand side under format: it looks like <>
The function would take one parameter (probably an integer or double, depending on what you're allowing your user to enter), and return a double. The function prototype might look something like this:

 
double ConvertToCelcius(int fahrenheit);


This function could be done in one line, something like:

 
return (double)(fahrenheit - 32) * 5.0 / 9.0;


This function can be called from the main function after processing user input. I recommend you validate the user input so that you don't try to pass garbage into your function.

Hope that helps.
Variables

First of all you learn that in C++ there are data types, named like int (integer), float (floating numbers) or double (double precision floating numbers). They all differ in how much memory they assign and how the computer works/calc with them. You can create variables in memory like this:

 
    data_type variable_name;


For example, these are variables:

1
2
    int n; // An integer 'n'
    double pi = 3.1415; // A floating point number (here Pi). This variable is initialized. 


If you do not initialize your vars, they have a random value. So be sure, to set the value to something before you use them.

Functions

If you know more about Variables you can process to functions. Functions can take parameter(s) (which have datatypes) und they can return a value (which datatype you have to declare, too).

You declare functions like this:

 
return_type function_name(datatype1, datatype2); // or more/less parameter... 


For example:

 
double square(double);


Declarations of functions tells the compiler what type of values he can expect of your functoins. To define what function do, do following:

1
2
3
4
double square(double x)
{
    return(x * x);
}


Square takes a parameter typed double x and returns x*x (again a double).

Thats pretty much you need to know about functions. Next topic have to be input/output to console. Google for it, because thats very basic HelloWorld and more. (Hint: look for cout and cin).

If you need more help, feel free to ask.

Maikel
Thanks a lot, ill see how far i get and post it up in a while
Thanks again for the tips/ help its much apreshiated

"
This function could be done in one line, something like:


return (double)(fahrenheit - 32) * 5.0 / 9.0; "

this is something i had in mind, again ill put it together with a users input and see what probablly goes wrong lol



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

#include <iostream>
using namespace std;

double ConvertToCelcius(float fahrenheit);


int main()
{
    float Fahrenheit;

    cout<<"Please enter a Fahrenheit \n";
    cin>> Fahrenheit;

    cout<< Fahrenheit << " converted to Celsius is: \n";

}

double ConvertToCelcius(float fahrenheit)


{
   return (double)(fahrenheit - 32) * 5.0 / 9.0;


    cin.get();
    cin.get();
    return 0;

}


this is what i came up with, no build errors but the console closes down without outputting and answer?? sorry i know its frustrating with newbies but im kind of taking bits form example codes and trying to see how it works
(i) return terminates a function. so after return in line 23 all next lines will not be executed. But the return in line 23 is the correct one.

(ii) in conclusion, if you want the cin.gets to be active move them to the main function and append them to the end there. This will stop your program to close immediatly


Does it help?

Maikel


EDIT: ah, do not forget to call your function in the main function, to print the result. like this:

1
2
3
4
5
    double result  = ConvertToCelcius(Fahrenheit);
    cout << result;

    // or alternativly
    cout << ConvertToCelcius(Fahrenheit);
Last edited on
There are a few things that I can see wrong in what you posted above.

Firstly, you're never calling your 'ConvertToCelcius' function from the main method. Imagine you're the instruction pointer. You'll start in main, output the text, then read the value and store it in your 'Fahrenheit' variable.

You then output the same value followed by some more text. You never call your function ConvertToCelcius :). If you actually want to show the conversion, that last line should be something like this:

 
cout<< Fahrenheit << " converted to Celsius is:" << ConvertToCelcius(Fahrenheit);


Now the reason that the 'console closes down' might be that it displays this line so fast that you don't have time to see it before the program ends. To give you some time to read the value, you could make the user hit a key to end the program using cin.get() inside main (not inside your ConvertToCelcius function, it doesn't belong there).

Note that your cin.get() calls would never be called in your code above, because the function would have already returned from the first statement.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

#include <iostream>
using namespace std;

double ConvertToCelcius(float fahrenheit);


int main()
{
    float Fahrenheit;
    double result  = ConvertToCelcius(Fahrenheit);


    cout<<"Please enter a Fahrenheit \n";
    cin>> Fahrenheit;

    cout<< Fahrenheit << " converted to Celsius is: \n" <<result;

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

double ConvertToCelcius(float fahrenheit)


{
   return (double)(fahrenheit - 32) * 5.0 / 9.0;



    return 0;

}


Thanks for the help so far everyone, it will sink in the more i do it im told lol, ok the above is the latest, yep moving the cin.get() in the main works and the reuslt aswell, i think theres a problem with the calculation unless im wrong, i typed in 45 for example the answer came back -17.7778
?
The flow of your main method is not correct. You should first ask for the Fahrenheit, THEN call your function to convert it, THEN display the result:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int main()
{
	float Fahrenheit;

	cout<<"Please enter a Fahrenheit \n";
    cin>> Fahrenheit;
    
    double result  = ConvertToCelcius(Fahrenheit);

    cout<< Fahrenheit << " converted to Celsius is: \n" <<result;

    cin.get();
    cin.get();

	return 1;
}


Also, you can remove the redundant 'return 0' from your ConvertToCelcius function on line 31. You should understand that the function exits after the first return statement on line 27.
now its working lol, thanks for that, i see where its going now much more clearly than the examples i have from uni
Topic archived. No new replies allowed.