Originally Posted by
ZuK
use "%lf" as printf-format string to print a double.
EDIT: actually "%f" should do. there is no difference between float and double for variadic functions.
It's been sometime since I coded in C but assuming he does exactly as u say, he would be leaving scanf to expect a int and then store it in space reserved for a double...this would still not produce the output the OP is looking for. Assuming your way:
Code:
double degree = 0.0;
double result = 0.0;
printf("\nEnter A to convert celsius to fahrenheit or B to convert fahrenheit to celsius: ");
scanf("%c", &type);
printf("\nEnter degree: ");
scanf("%d", °ree);
switch (toupper(type))
{
case'A':
result = degree*1.8+32;
printf("\n%f degrees celsius is %d degrees fahrenheit", degree, result);
break;
I'm curious to see what the outcome on your system would be assuming this.
Apparently the OP solved the issue. However solving and not knowing how it was done, especially when learning C isn't much help. Thus if you're declaring and expecting a certain data type, then make sure scanf's format string is set to accept that type. In some cases when calling printf you may have a little flexibility and want to change how the variable will be outputted to the end user ex:
Code:
#include <stdio.h>
int main(void){
char c;
scanf("%c", &c);
printf("you put: %c which is %d in decimal", c, c);
getchar();
return 0;
}
However you are not doing anything like this.
Thus to the correct way would be to give scanf the proper string it is expecting and follow through with your printf statements
Code:
/*exercise 3.1 converting temperatures*/
#include <stdio.h>
#include <ctype.h>
int main()
{
char type = 0;
double degree = 0.0;
double result = 0.0;
printf("\nEnter A to convert celsius to fahrenheit or B to convert fahrenheit to celsius: ");
scanf("%c", &type);
printf("\nEnter degree: ");
scanf("%lf", °ree);
switch (toupper(type))
{
case'A':
result = degree*1.8+32;
printf("\n%lf degrees celsius is %lf degrees fahrenheit", degree, result);
As a slight aside if there are no arguments given to main then it should be set as compiling with at least -Wall should have given you at least some warnings about this.