Fahrenheit to Celsius and vice versa conversion help

Can you please help?
This is what i have.

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
34
35
36
37
38
#include <iostream>
#include <string>
#include "cstdlib"

using namespace std;

double far (double a, double c);
double cel (double b, double c);

int main()
{
    double a, b, c;
    char x;
    
    cout << "Enter a 1 for celsius to fahrenheit, 2 for celsius to fahrenheit: ";
    cin >> x;
    cout << "Enter a temperature: ";
    cin >> c;
    switch (x)
    {
        case '1' : cout << far (a, c);
                   break;
        case '2' : cout << cel (b, c);
                   break;
    }
}

double far (double a, double c)
{
    a = .55555 * (c - 32);
    return a;
}

double cel (double b, double c)
{
    b = (1.8 * c) + 32;
    return b;
}
Last edited on
You declared a and b as parameters, but they don't have a value when you pass it as an argument.
Last edited on
The program "works" it just doesn't do the math correctly and I don't know how to fix it
It "works" correctly, just the code in line 15:

cout << "Enter a 1 for celsius to fahrenheit, 2 for celsius to fahrenheit: ";

has the wrong sentence.
And I'm thinking you're wasting variables like a and b, in line 12:

double a, b, c;
Just to give you and idea...

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>

//Function Prototypes.
double C_to_F(double); // Returns the Fahrenheit and takes one argument as Celcius.
double F_to_C(double); // Returns the Celcius and takes one argument as Fahrenheit.

int main()
{
	enum menuChoices{FAHRENHEIT = 1, CELCIUS, EXIT};

	bool keepLooping{ true };
	while (keepLooping)
	{
		int choice{};
		double temperature{};

		std::cout << "1 for celsius to fahrenheit\n";
		std::cout << "2 for fahrenheit to celcius\n";
		std::cout << "3 for exit the program\n";
		std::cout << "Enter your choice: ";
		std::cin >> choice;
		switch (choice)
		{
		case FAHRENHEIT:
			std::cout << "Celcius to Fahrenheit\n";
			std::cout << "Enter the temperature in Celcius: ";
			std::cin >> temperature;
			std::cout << C_to_F(temperature) << " degs Fahrenheit\n";
			keepLooping = true;
			break;

		case CELCIUS:
			std::cout << "Fahrenheit to Celcius\n";
			std::cout << "Enter the temperature in Fahrenheit: ";
			std::cin >> temperature;
			std::cout << F_to_C(temperature) << " degs Celcius\n";
			keepLooping = true;
			break;

		case EXIT:
			std::cout << "Program ending.\n";
			keepLooping = false;
			break;

		default:
			std::cout << "Your choice " << choice << " is incorrect.\n";
			std::cout << "Your choices are between " << FAHRENHEIT <<
				" and " << EXIT << std::endl;
		}
	}

	return 0;
}

double C_to_F(double C)
{
	const double F{ 1.8*C + 32 };
	return F;
}

double F_to_C(double F)
{
	const double C{ ((F - 32) * 5) / 9 };
	return C;
}
The far() and cel() routines are the wrong way round, and have too many arguments (as has already been pointed out).
Topic archived. No new replies allowed.