Convert Fahrenheit to Celsius with ANSI C: Homework Question and Attempt

  • Thread starter Thread starter STEMucator
  • Start date Start date
AI Thread Summary
The discussion focuses on creating a Fahrenheit to Celsius converter in ANSI C, highlighting issues with user input handling. The main problem arises when the program uses `getchar()` to read individual characters, leading to incorrect conversions for multi-digit inputs. Suggestions include using a loop to accumulate digits into a complete integer value or employing `sscanf()` for string conversion. Additionally, clarification is provided on how to properly signal EOF in different operating systems. The conversation emphasizes the importance of correctly managing input types to ensure accurate temperature conversions.
STEMucator
Homework Helper
Messages
2,076
Reaction score
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.
 
Physics news on Phys.org
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:
rcgldr said:
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 add the digit, 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.

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.
 
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.
 
rcgldr said:
To get an EOF from user input, ask the user to input ctrl-Z (for dos / windows) or ctrl-D (for unix).

Zondrina said:
Yeah its ctrl + c for my compiler.
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.
 

Similar threads

Replies
3
Views
1K
Replies
2
Views
2K
Replies
7
Views
2K
Replies
9
Views
4K
Replies
1
Views
3K
Replies
8
Views
1K
Back
Top