Hey guys...
I have this code and i need to make the temperature part work with a function but i don't know how.

So
Any help is appreciated.

Thanks

*edit* Also, if there is any way to make this simpler, then please tell.

#include <iostream>
#include <time.h>
#include <string>

using namespace std;

int temp (int, int);

int main()
{
	//Integers
	int a;
	float b = 0;
	int c;
	float d;
	int e;
	

	//Title and choosing what you want
	cout << endl << " Welcome To The TCVM. " << endl;

	cout << endl << " The [T]emperature [C]urrency [V]olume [M]ass [U]nit [C]onverter" << endl << endl << endl;

	while (1)
	{
		system("pause");
		system("cls");

		cout << endl << "What would you like to convert? " << endl;

		cout << endl << "1. [T]emperature. " << endl;

		cout << endl << "2. [C]urrency. " << endl;

		cout << endl << "3. [V]olume. " << endl;

		cout << endl << "4. [M]ass. " << endl;

		cout << endl << " ";

		cin >> a;

		//Temperatures
		if (a == 1)
		{
			system("pause");
			system("cls");
			
			cout << " Ok, you want to convert temperatures. " << endl;
		
			cout << " Please enter the temperature in Farenheit that you wish to convert: " << endl;

			cin >> b;

			b = temp (e, a);

			cout <<"Here is the degree that you wanted converted in Celcius: " << endl;

			cout <<b <<" degrees" << endl;
		}

		//Currency
		else if (a == 2)
		{
			system("pause");
			system("cls");
			
			cout << endl <<" Ok, you want to convert Currency. " << endl;

			cout << endl;
		
			cout << " Please choose what currency you would like to convert to: " << endl;

			cout << endl;

			cout << " 1. Euro. " << endl;

			cout << endl;

			cout << " 2. U.K. Pound. " << endl;

			cout << endl;

			cout << " 3. Indian Rupee. " << endl;

			cout << endl;

			cout << " 4. Australian Dollar. " << endl;

			cout << endl;

			cout << " 5. Japanese Yen. " << endl;

			cout << endl;

			cout << " 6. Chinese Yuan Renminbi. " << endl;

			cout << endl;

			cin >> c;

			if (c == 1)
			{
				system ("pause");
				system ("cls");

				cout << endl <<" Ok, Euro it is." << endl;

				cout << endl;

				cout << endl <<" Please enter the amount of U.S. Dollars that you would like converted: " << endl;

				cout << endl;

				cin >> d;

				d = d * 0.69380;

				cout << endl <<" The converted amount in Euros is: " << d << endl;
			}

			else if (c == 2)
			{
				system ("pause");
				system ("cls");

				cout << endl <<" Ok, the British Pound it is." << endl;

				cout << endl <<" Please enter the amount of U.S. Dollars that you would like converted: " << endl;

				cin >> d;

				d = d * 0.60510;

				cout << endl <<" The converted amount in Pounds is: " << d << endl;
			}

			else if (c == 3)
			{
				system ("pause");
				system ("cls");

				cout << endl <<" Ok, the Indian Rupee it is." << endl;

				cout << endl <<" Please enter the amount of U.S. Dollars that you would like converted: " << endl;

				cin >> d;

				d = d * 45.2400;

				cout << endl <<" The converted amount in Rupees is: " << d << endl;
			}

			else if (c == 4)
			{
				system ("pause");
				system ("cls");

				cout << endl <<" Ok, Australian Dollar it is." << endl;

				cout << endl <<" Please enter the amount of U.S. Dollars that you would like converted: " << endl;

				cin >> d;

				d = d * 0.94971;

				cout << endl <<" The converted amount in Australian Dollars is: " << d << endl;
			}

			else if (c == 5)
			{
				system ("pause");
				system ("cls");

				cout << endl <<" Ok, Japanese Yen it is." << endl;

				cout << endl <<" Please enter the amount of U.S. Dollars that you would like converted: " << endl;

				cin >> d;

				d = d * 76.6102;

				cout << endl <<" The converted amount in Yen is: " << d << endl;
			}

			else if (c == 6)
			{
				system ("pause");
				system ("cls");

				cout << endl <<" Ok, Chinese Yuan Renminbi it is." << endl;

				cout << endl <<" Please enter the amount of U.S. Dollars that you would like converted: " << endl;

				cin >> d;

				d = d * 6.38710;

				cout << endl <<" The converted amount in Chinese Yuan Renminbi is: " << d << endl;
			}
		
		}
	}
	system("pause");
	return 0;
}

int temp(int a)
{
	int e;
	e = a - 32;
	return(e)*.55;
}

Recommended Answers

All 2 Replies

Line 56 Function call:

b = temp (e, a);

temp function : line 209

int temp(int a)

This function expects an integer parameter representing the number of degrees in Fahrenheit and returns an integer representing the number of degrees in Celsius, so give it what it wants and give it the right types.

First off, make sure you WANT it to take an integer and return an integer. If not, change the function to return and accept the type(s) you want it to. Right now you return an int and assign it to a float. Perfectly legal, but if degrees in Celsius is a float, make the function return a float. If it is indeed an int, store it as an int. Personally I think the degrees should all be in floats. Less round-off error and you're multiplying by 0.55 in the function anyway.

Anyway, regardless, the function wants the degrees in Fahrenheit as a parameter, which lines 52 and 54 make clear is stored in the variable b. So pass the function b, not e and a.

Now is a good time for a lecture in variable naming. Name the variables what they stand for. There is less chance for confusion. a, b, e? Easy to confuse and less easy to catch. How about this instead? The naming of the function and variables make it much easier to follow and write the code.

float degreesInCelsius, degreesInFahrenheit;
cout << " Ok, you want to convert temperatures. " << endl;
cout << " Please enter the temperature in Fahrenheit that you wish to convert: " << endl;
cin >> degreesInFahrenheit;
degreesInCelsius = FahrenheitToCelsius(degreesInFahrenheit);

float FahrenheitToCelsius(float degreesInFahrenheit)
{
    float degreesInCelsius = degreesInFahrenheit - 32;
    return(degreesInCelsius)*.55;
}

wow....awesome thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.