# Homework Help: C question, ANSI related

1. Oct 5, 2012

### Zondrina

1. The problem statement, all variables and given/known data

Trying to make a faren to cel converter.

2. Relevant equations

3. 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 (Text):
#include <stdio.h>

float farenToCelcius(float input);

int main() {
int c, i;

printf("Enter your farenheit 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 farenheit 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. Lets 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 wont 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.

2. Oct 5, 2012

### rcgldr

To get an EOF from user input, ask the user to input ctrl-Z (for dos / windows) or ctrl-D (for unix).

You'll need to create a loop that does a series of getchar(). To create a value, start with i = 0;, then for each digit, add the digit with
i = (10 * i) + c - '0';
Or you could create a function to get a line of data, and use sscanf() to convert the entered string into an integer.

Last edited: Oct 5, 2012
3. Oct 5, 2012

### Zondrina

Yeah its ctrl + c for my compiler.

I tried what you said there and it didn't work for some reason. I got numbers that weren't even in the same region of what I was expecting.

4. Oct 5, 2012

### Staff: Mentor

Why are you using getchar() to input a floating point value?

The biggest problem in your code is that you are using the char type to hold a temperature. If the user wanted to find the Celsius (note spelling) equivalent of 32 °Fahrenheit (note spelling here, too), he or she would press 3 and then 2. Your program would process only the '3' character, and would pass the value 3 to your conversion function, which would return something close to -16° Celsius. Your code would then process the '2' character, producing another Celsius temperature.

5. Oct 5, 2012

### Staff: Mentor

CTRL-C does not produce the EOF character, and this has nothing to do with the compiler - it has to do with the operating system. As rcgldr said, if you're using DOS or Windows, the user should press CTRL-Z. On Unix/Linux, it would be CTRL- D.