Programming assignment help needed

In both of my functions the the correct temperature is calculated, which is proven by the printf line, but when it is returned the result always equals 3.

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
#include <stdio.h>
#include <stdlib.h>

main() { 
	int choice = 0;
	int fahrenheit = 0;
	int celsius = 0;

	printf("Choose from the following:\nEnter 1 to convert from fahrenheit to celsius");
	printf("\nEnter 2 to convert from celsius to fahrenheit\nEnter 3 to quit");
	printf("\nType your choice here: ");
	scanf_s("%i", &choice);

	switch (choice) {
	case 1:
		printf("\nYou choose fahrenheit to celsius\n");
		printf("Enter a temperature in fahrenheit: ");
		scanf_s("%i", &fahrenheit);
		celsius = fahrenheittocelsius(fahrenheit);
		printf("\n%i degrees fahrenheit equals %i degrees celsius\n\n", fahrenheit, celsius);
		break;
	case 2:
		printf("\nYou choose celsius to fahrenheit\n");
		printf("Enter a temperature in celsius: ");
		scanf_s("%i", &celsius);
		fahrenheit = celsiustofahrenheit(celsius);
		printf("\n%i degrees celsius equals %i degrees fahrenheit\n\n", celsius, fahrenheit);
		break;
	case 3:
		printf("\ncongratulations you have gained nothing\n");
		break;
	}
	system("pause");
}

float fahrenheittocelsius(int f){
	int result;
	result = (f - 32.0) * (5.0/9.0);
        printf("%i\n", result);

	return result;
}

float celsiustofahrenheit(int c) {
	int result;
	result = (9.0 / 5.0) * c + 32.0;
	printf("%i\n", result);

	return result;
}
Last edited on
Hello QuanCalzone,

First question is this a C or C++ program?

I tried in 2 formats and your code does not compile.

After your includes you are missing the prototypes.

Your compiler must be very old to allow main(). The newer compilers need int main() to be acceptable.

A "double" is preferred over "float".

The functions promises to return a "float", but you are trying to return an "int".

Your formula is a floating point calculation that you try to put in an "int". This will loos the decimal portion of the number.

Other than "choice" in "main" a;; the numeric variables should be "double"s.

In the "printf" statements the "%i"s should be "%f"s.

Andy
Topic archived. No new replies allowed.