- #1
- 2,076
- 140
Homework Statement
Trying to make a faren to cel converter.
Homework Equations
The Attempt at a Solution
Not really a solution, just experimenting. Here's my code along with a description of what works and what does not :
Code:
#include <stdio.h>
float farenToCelcius(float input);
int main() {
int c, i;
printf("Enter your Fahrenheit value or enter -1 to quit : ");
while((c = getchar()) != EOF){
if(c != '\n' && c != ' ' && c != '\t')
printf("The temperature in celcius is %3.5f\n", farenToCelcius(c-'0'));
else
printf("Enter your Fahrenheit value or enter -1 to quit :");
}
return 0;
}
float farenToCelcius(float tempF) {
return((5*(tempF-32))/9);
}
Few issues I'm having here. Let say I type in digits from 0-9 when getchar() is called at the beginning of my while loop. Then the conversion works perfectly and the correct answer is spat out. The while loop resets and waits from user input.
Now for the problem. Let's say I don't type a digit between 0 - 9, say 15. The loop winds up running twice. Once for 1 and once for 5 producing two different answers ( The answers are correct for each individual digit, but not for the number as a whole ).
So my question is, is there a way to use the WHOLE value of my variable c rather than it splitting the way it does?
Also, since I've declared c as an int, -1 won't actually exit the program because the value of c is treated according to its corresponding ANSI value ( -1 is supposed to correspond to EOF ). So how would I make the program exit safely here?
Thanks for any help in advance.