scanf problem - should have easy answer
The following compiles, links, and runs fine in Visual C++. It inputs 2 numbers and outputs the sum of the 2 numbers. The only problem is that it always outputs 0.00000 as the sum of the 2 numbers. For example, if I input 5 for value1 and 4.6 for value2, it outputs 0.00000 as the sum. I verified this with the debugger too. The dubugger is not showing the values of value1 and value2 correctly as they are input. Instead, it shows a really small number like 4.3100000E-213 (or something like that). There is probably a really obvious answer to this such as visual C++ does not compile C code correctly, but I''m not sure. Yes, I know I could use cout and cin or another method instead, so please don''t suggest that. I want to get pure C to work correctly for me.
#include <stdio.h>
int main()
{
double value1 = 0, value2 = 0, sum = 0;
printf("Enter a value: ");
scanf("%d", &value1);
printf("Enter another value: ");
scanf("%d", &value2);
sum = value1 + value2;
printf("The sum of the numbers is: ");
printf("%f", sum);
return 0;
}
Rhino2876
Just so you all know, I just tried to compile it in a C only compiler and got the same results, so it''s not a problem with the compiler, it''s me.
Rhino2876
Nevermind, I solved my own problem. I just realized that scanf could only take in a float, int, or char. It can''t be a double. Who would of thunk it?
Rhino2876
Try using "%lf" as the format specifier for scanf and printf. Hopefully that will give you a "long float" (which is actually a double). Works for me, but I''m not using MSVC++, so YMMV.
I think u r mistaken. %d does not represent a double. It actually represents an integer. So, ur explanation is not right....
This is IndianaJones.
This is IndianaJones.
This is Jones - IndianaJones
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement