Due Tomorrow, please help quick. Converting Fahr to Cent, using only calls in main function.

So I've been working on this problem ALL WEEK LONG, and I cannot for the life of my figure out what to do. The problem asks me to:

"Write a program that asks the user to enter a temperature reading in centigrade and then prints the equivalent Fahrenheit value. It then asks the user to enter a Fahrenheit value and prints out the equivalent centigrade. The program uses standard functions and the main function should have only function calls. "

Here is what I've slaved over and unfortunately made no progress on so far.

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
66
67
68
  #include <stdio.h>

double getCent ();
double getFahr ();
double fahr_to_cent (double x);
double cent_to_fahr (double y);


int main()
{
	int dummy;
	double Cent;
	double Fahr;
	double newCent;
	double newFahr;


	Cent = getCent();
	newCent = fahr_to_cent(Cent);
	Fahr = getFahr();
	newFahr = cent_to_fahr(Fahr);
	
	printf("\nThe temperature you entered in Centigrade is: %f", newFahr);
	printf("  degrees in Fahrenheit.");
	printf("\n\nThe temperature you entered in Fahrenheit is: %f", newCent);
	printf("  degrees in Centigrade.");


	printf("\n\nThe program has finished. Press enter to exit. \n");
	scanf_s("%d", &dummy);
	return 0;
}


//Get Fahrenheit and convert.

double getFahr ()
{
	double Fahr;

	printf("Please enter a temperature in degrees for Fahrenheit: ");
	scanf_s("%f", &Fahr);
	return Fahr;
}

double fahr_to_cent (double x)
{
		return ((x-32)*(5.0/9.0));
}


//Now get Centigrade and convert.

double getCent ()
{
	double Cent;

	printf("Please enter a temperature in degrees for Centigrade: ");
	scanf_s("%f", &Cent);
	return Cent;
}



double cent_to_fahr (double y)
{
	return (y*(9.0/5.0)+32);
}


PLEASE! Help me! This is due tomorrow at midnight so the speedier the reply the better. Thank you so much.
Just a heads up telling us it is due tomorrow won't make us help any more :P Also, first time hearing centigrade for Celsius. By the way aren't these backwards
1
2
3
4
	Cent = getCent();
	newCent = fahr_to_cent(Cent); //dont you want cent to fahr?
	Fahr = getFahr();
	newFahr = cent_to_fahr(Fahr); //dont you want fahr to cent? 
I'll do anything to get any help on this lol sorry.
And yes, now that I've changed them, this is still my output, when the input is 100 Cent and 104 Fahr.


The temperature you entered in Centigrade is: -514220027312023830000(cont.'s as zero) degrees in Fahrenheit.

The temperature you entered in Fahrenheit is: -166607288838320360000(cont.'s as zero) degrees in Centigrade.


Output should be:


The temperature you entered in Centigrade is: 212 degrees in Fahrenheit.

The temperature you entered in Fahrenheit is: 40 degrees in Centigrade.
1
2
3
4
	Cent = getCent();
	newCent = fahr_to_cent(Cent);
	Fahr = getFahr();
	newFahr = cent_to_fahr(Fahr);
actually it should be
1
2
3
4
Cent = getCent();
newFahr = cent_to_fahr(Cent);
Fahr = getFahr();
newCent = fahr_to_cent(Fahr);
after looking at your print statements.

scanf_s why not just use scanf I haven't done much with c so not sure if there is a difference. I use c++ with cout for output and cin for input :P Though according to http://www.cplusplus.com/reference/cstdio/scanf/?kw=scanf you need lf for reading into a double.

so scanf("%lf", x); You can verify the reading isn't working by simply outputting Cent or Fahr. Which would also make it more readible if you had something like The temperature you enterred in Centigrade(100) is: 212 degrees in Fahrenheit.

*Edit after doing a bit of research it seems the _s means it is a "new and more secure" function by microsoft and not in C99 or earlier standards.
Last edited on
Here is the entire thing now:

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
66
67
68
#include <stdio.h>

double getCent ();
double getFahr ();
double fahr_to_cent (double x);
double cent_to_fahr (double y);


int main()
{
	int dummy;
	double Cent;
	double Fahr;
	double newCent;
	double newFahr;


	Cent = getCent();
	newCent = fahr_to_cent(Cent);
	Fahr = getFahr();
	newFahr = cent_to_fahr(Fahr);
	
	printf("\nThe temperature you entered in Centigrade(%lf) is:\n %lf", Fahr,newFahr);
	printf("  degrees in Fahrenheit.");
	printf("\n\nThe temperature you entered in Fahrenheit(%lf) is:\n %lf", Cent,newCent);
	printf("  degrees in Centigrade.");


	printf("\n\nThe program has finished. Press enter to exit. \n");
	scanf_s("%d", &dummy);
	return 0;
}


//Get Fahrenheit and convert.

double getFahr ()
{
	double Fahr;

	printf("Please enter a temperature in degrees for Fahrenheit: ");
	scanf_s("%lf", &Fahr);
	return Fahr;
}

double fahr_to_cent (double x)
{
		return ((x-32)*(5.0/9.0));
}


//Now get Centigrade and convert.

double getCent ()
{
	double Cent;

	printf("Please enter a temperature in degrees for Centigrade: ");
	scanf_s("%lf", &Cent);
	return Cent;
}



double cent_to_fahr (double y)
{
	return (y*(9.0/5.0)+32);
}


Now my output is backwards:

Please enter a temperature in degrees for Centigrade: 100
Please enter a temperature in degrees for Fahrenheit: 104

The temperature you entered in Centigrade(104.000000) is: 
 219.200000 degrees in Fahrenheit.

The temperature you entered in Fahrenheit(100.000000) is:
 37.777778 degrees in Centigrade.


?
After looking over everything and doing a few out of program calculations I realized my error.

Program before, output was either random or backwards.
1
2
3
4
5
6
7
8
9
        Cent = getCent();
	newCent = fahr_to_cent(Cent);
	Fahr = getFahr();
	newFahr = cent_to_fahr(Fahr);
	
	printf("\nThe temperature you entered in Centigrade(%lf) is:\n %lf", Cent,newFahr);
	printf("  degrees in Fahrenheit.");
	printf("\n\nThe temperature you entered in Fahrenheit(%lf) is:\n %lf", Fahr,newCent);
	printf("  degrees in Centigrade.");


This is what I changed, because now the variables are read correctly.
1
2
3
4
5
6
7
8
9
        Cent = getCent();
	newCent = cent_to_fahr(Cent);
	Fahr = getFahr();
	newFahr = fahr_to_cent(Fahr);
	
	printf("\nThe temperature you entered in Centigrade(%lf) is:\n %lf", Cent,newCent);
	printf("  degrees in Fahrenheit.");
	printf("\n\nThe temperature you entered in Fahrenheit(%lf) is:\n %lf", Fahr,newFahr);
	printf("  degrees in Centigrade.");


Basically, I should not have named the variables newCent and newFahr, rather something like centNow and fahrNow. I had them plugged so that I was converting Centigrade-->Centigrade and Fahrenheit-->Fahrenheit.

Thanks for the help giblit! Fortunately, I figured this out!

P.S. Quick Q. How do I make the numbers print only 1-2 zeroes rather than 6 after the decimal?
Last edited on
You could do something like printf("...%3.3f...) the number before period (.) is for the minimum number of numbers you want, if less than that is there say 99 instead of 100 then it will pad it with a space so instead of an output of
99
it will be [code] 99[/output] the period(.) and number after in this case (a floating point) are the number of digits after the decimal point(mantissa) by default it is set to 3. I figured you wouldn't need any past 200 degrees or any more accuracy than 3 digits. You can change to your needs if you want. For more information on printf please refer to here: http://www.cplusplus.com/reference/cstdio/printf/?kw=printf
Topic archived. No new replies allowed.