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

  • Thread starter STEMucator
  • Start date
In summary: This will signal the end of the input stream, and your program can check for this condition within the while loop and exit gracefully.In summary, the conversation is about a student trying to create a Fahrenheit to Celsius converter using C programming language. The student is experiencing issues with the conversion, specifically when inputting numbers with multiple digits. They are also unsure of how to safely exit the program. Suggestions are given to use getchar() and sscanf() to properly convert and handle user input, as well as using CTRL-Z or CTRL-D to signal EOF and exit the program gracefully. The student is also advised to use appropriate data types for their variables to avoid incorrect conversions.
  • #1
STEMucator
Homework Helper
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.
 
Physics news on Phys.org
  • #2
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:
  • #3
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.
 
  • #4
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
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.
 

What is the difference between C and ANSI?

C is a programming language, while ANSI is a set of standards for the C language. ANSI standards were created to ensure that C programs are portable and can be run on different computer systems.

What is the purpose of ANSI in relation to C?

The purpose of ANSI in relation to C is to establish a set of standards that ensure the portability and compatibility of C programs across different computer systems. This allows for more efficient and reliable programming, as well as easier maintenance and updates.

Is ANSI compliance necessary when programming in C?

While not mandatory, it is highly recommended to follow ANSI standards when programming in C. This ensures that your code will be portable and compatible with different systems, and can also help to avoid potential errors and bugs.

What are some key features of ANSI C?

Some key features of ANSI C include standardized library functions, improved data types, and stricter rules for type conversions. ANSI C also introduced the concept of function prototypes, which allows for more reliable and maintainable code.

How do I know if my C code is ANSI compliant?

You can check if your C code is ANSI compliant by using a compiler that supports ANSI standards and running your code through it. The compiler will flag any non-compliant code or syntax errors. Additionally, you can consult the ANSI C standard documentation to ensure that your code follows all necessary guidelines.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
3
Views
838
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
25
Views
13K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
950
  • Engineering and Comp Sci Homework Help
Replies
9
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
Back
Top