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

  • Thread starter Thread starter STEMucator
  • Start date Start date
Click For Summary

Discussion Overview

The discussion revolves around a homework problem involving the conversion of Fahrenheit to Celsius using ANSI C. Participants share their code attempts, identify issues, and seek solutions related to user input handling and data type usage.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • The original poster describes their attempt at creating a Fahrenheit to Celsius converter and notes issues with handling multi-digit input using getchar().
  • Some participants suggest using a loop to accumulate digits into a single integer value instead of processing each character individually.
  • There is a discussion about how to properly signal EOF in different operating systems, with conflicting information about the correct key combinations.
  • One participant questions the use of getchar() for inputting floating-point values and points out that the current approach leads to incorrect temperature conversions.
  • Another participant reiterates the importance of using the correct key combination for EOF, emphasizing that CTRL-C does not produce EOF.

Areas of Agreement / Disagreement

Participants express multiple competing views on how to handle user input and EOF signaling. There is no consensus on the best approach to resolve the issues presented.

Contextual Notes

Participants have not reached a resolution regarding the handling of multi-digit input and the appropriate method for exiting the program. There are also unresolved questions about the implications of using char versus int for temperature values.

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 celsius 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 ·
Replies
3
Views
2K
  • · Replies 25 ·
Replies
25
Views
15K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
9
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 9 ·
Replies
9
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 5 ·
Replies
5
Views
2K