/ / Warum schlägt meine Konvertierung von CELSIUS in RANKINE fehl, wenn der Benutzer zur Eingabe eines Werts für CELSIUS aufgefordert wird? - c, Funktion, Debugging, Typkonvertierung

Warum schlägt meine Konvertierung von CELSIUS zu RANKINE fehl, wenn Benutzer nach einem Wert für CELSIUS gefragt wird? - c, Funktion, Debugging, Typumwandlung

Führen Sie die folgenden Konvertierungen aus, um von CELSIUS nach RANKINE zu konvertieren:
Grad Fahrenheit = (9,0 / 5,0) * Grad Celsius + 32
Grad Rankine = Grad Fahrenheit + 459.67 "

Dieses Programm konvertiert Grad Celsius zu grad Rankine. Bitten Sie den Benutzer um eine Temperatur in Celsius.

#include <stdio.h>

int main(void)
{
double f,c,r;

printf("Enter the temperature in degrees Celsius:" );
scanf("%d", &c);

f = (9.0/5.0) * c +32;
r = f + 459.67;

printf("After your conversion, your temperature in Rankin is: ", r);

return(0);
}

Antworten:

0 für die Antwort № 1

Warum schlägt meine Konvertierung von CELSIUS in RANKINE fehl, wenn der Benutzer zur Eingabe eines Werts für CELSIUS aufgefordert wird?

Code verwendet den falschen Datentyp für die Formatbezeichner. Für typ double, scannen Sie mit "%lf" und zu drucken verwenden "%f"

  1. @ BLUEPIXY Kommentar, um die passenden Formatbezeichner in sncaf() und printf():

  2. Die Eingabeaufforderung wird abgespült.

  3. Prüfen scanf() Rückgabewert.

.

#include <stdio.h>

int main(void) {
double f,c,r;

printf("Enter the temperature in degrees Celsius:" );
fflush(stdout);
if (scanf("%lf", &c) != 1) {
puts("Non-numeric input" );
return -1;
}

f = (9.0/5.0) * c +32;
printf("After your conversion, your temperature in Fahrenheit is: %.1f F", f);

r = f + 459.67;
printf("After your conversion, your temperature in Rankine is: %.1f R", r);
return 0;
}