icarus wrote:
Hi, this is a simple temperature converter (Fahrenheit to Celsius to
Kelvin). Using gcc 4.0.
The user is supposed to enter q or any other non-character to exit the
program.
Problem with this code:
I enter 'q' as soon as the program starts. It hangs.
Then run it again. Enter a number, it executes fine. But when I enter
q, it repeats last number and it doesn't 'pay attention' to the letter
entered.
Any ideas on how can I fix it? thanks in advance.
#include <stdio.h>
const double F_TO_CELS_ONE_EIGHT = 1.8;
const double F_TO_CELS_THIRTYTWO = 32.0;
const double C_TO_KELVIN = 273.16;
void Temperatures(double f_temp);
int main(void){
double f_temp;
char quit;
while (quit != "q"){
printf("Enter temperature in Fahrenheit (q to quit):
");
quit = getchar();
scanf("%lf", &f_temp);
Temperatures(f_temp);
}
printf("\nbye!");
return 0;
}
void Temperatures(double f_temp){
double celsius, kelvin;
celsius = (F_TO_CELS_ONE_EIGHT * f_temp) + F_TO_CELS_THIRTYTWO;
kelvin = celsius + C_TO_KELVIN;
printf("Fahrenheit degrees: %.2f\n", f_temp);
printf("Celsius degrees: %.2f\n", celsius);
printf("Kelvin degrees: %.2f\n", kelvin);
}
/* BEGIN new.c */
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>
#define F_TO_CELS_RATIO ((100 - 0) / (212.0 - 32))
#define F_TO_CELS_OFFSET (-32.0)
#define C_TO_KELVIN_OFFSET 273.16
void Temperatures(double f_temp);
int get_line(char **lineptr, size_t *n, FILE *stream);
int main(void)
{
double f_temp;
char **quit;
int rc;
char *buff = NULL;
size_t size = 0;
fputs("Enter temperature in Fahrenheit (q to quit):", stdout);
fflush(stdout);
while((rc = get_line(&buff, &size, stdin)) 0) {
f_temp = strtod(buff, quit);
if (*quit == buff) {
break;
}
Temperatures(f_temp);
printf("Enter temperature in Fahrenheit (q to quit):");
fflush(stdout);
}
puts("\nbye!");
return 0;
}
void Temperatures(double f_temp)
{
double celsius, kelvin;
celsius = F_TO_CELS_RATIO * (f_temp + F_TO_CELS_OFFSET);
kelvin = celsius + C_TO_KELVIN_OFFSET;
printf("Fahrenheit degrees: %.2f\n", f_temp);
printf("Celsius degrees: %.2f\n", celsius);
printf("Kelvin degrees: %.2f\n", kelvin);
}
int get_line(char **lineptr, size_t *n, FILE *stream)
{
int rc;
void *p;
size_t count;
count = 0;
while ((rc = getc(stream)) != EOF
|| !feof(stream) && !ferror(stream))
{
++count;
if (count == (size_t)-2) {
if (rc != '\n') {
(*lineptr)[count] = '\0';
(*lineptr)[count - 1] = (char)rc;
} else {
(*lineptr)[count - 1] = '\0';
}
break;
}
if (count + 2 *n) {
p = realloc(*lineptr, count + 2);
if (p == NULL) {
if (*n count) {
if (rc != '\n') {
(*lineptr)[count] = '\0';
(*lineptr)[count - 1] = (char)rc;
} else {
(*lineptr)[count - 1] = '\0';
}
} else {
if (*n != 0) {
**lineptr = '\0';
}
ungetc(rc, stream);
}
count = 0;
break;
}
*lineptr = p;
*n = count + 2;
}
if (rc != '\n') {
(*lineptr)[count - 1] = (char)rc;
} else {
(*lineptr)[count - 1] = '\0';
break;
}
}
if (rc != EOF || !feof(stream) && !ferror(stream)) {
rc = INT_MAX count ? count : INT_MAX;
} else {
if (*n count) {
(*lineptr)[count] = '\0';
}
}
return rc;
}
/* END new.c */
--
pete