but I cannot figure out what I am doing wrong
This program compiles and runs but scanf won't give me any numbers but 0 for a
floating integer. This is a homework problem, and not finished yet, but all I
need is for someone to please show me where I am goofing it...
program:
include <stdio.h>
main()
{
int weight, deliveryCharge;
printf("Please enter in the weight of the package in kg\n");
scanf(" %f", &weight);
printf("The delivery charge will be %.2f.\n",deliveryCharge);
system("pause");
return 0;
here is what I get:
Please enter in the weight of the package in kg
2
0.00
The delivery charge will be 0.00.
Press any key to continue . . .
When I change it to an integer (using %d) it works fine.
That second print f line is only to tell me what I am getting from scanf,
which is apparently nothing...
This project is in C, on a Vista machine...I have been doing all my work on
this machine, and after getting a few bugs worked out, it hasn't been a
problem.
Thanks for any help
froglipz
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Yeah, my instructor said not to include those...probably so I could spend a
few hours staring at it before I figured out that I declared an integer for a
float variable :/
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Then your instructor is a fool. Why would you not want teh compiler to help
you detect coding errors!?
Using Dev-C++ is not too btight an idea either; when learning to code, you'd
benefit from a decent symbolic debugger, and that is not a description that
fits Dev-C++'s dismal debugging facility.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I think he wants us to make our mistakes so we can learn from them...anyway,
it matters not what we think, I just have to do as instructed for a couple
more months then I can go back to using code::blocks :)
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Code::Blocks wasn't what I was thinking of; it uses the same MinGW/GCC
toolchain, and the same underlying GDB debugger; although the integration with
the debugger is somewhat more fully featured and robust, but not a patch on
VC++2008 Express.
Regarding making your own mistakes and learning from them, you still too that
with a higher warning level - only faster! No professional developer of any
merit would deliberately cripple the compiler's ability to improve the quality
of his code. In fact well equipped developers use additional static analysis
tools to further inspect the code.
Moreover warnings are issued for syntactically valid code that is suspect of
dangerous in some circumstances; it is therefore highly likely that you will
produce bad code that will appear to work - until it doesn't. The whole
concept of switching off warnings is foolish. I'd gladly explain this to your
tutor if you care to direct him to this forum.
My point is (had you not found it yourself), you'd have got the solution by
asking humans on this forum and waiting for a reply; OR you could have had the
compiler tell you immediately; which is the most efficient,? And would you
really have learned less by using the more efficient method?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
... I suspect the real reason for your tutor's directive is more likely that
he like many poorer practitioners of the art he erroneously think that
warnings are not important (when often they are semantic errors), and most
probably his own code generates warnings because of its poor quality, and he
prefers to put his head in the sand rather than figure out what is wrong with
his code.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
but I cannot figure out what I am doing wrong
This program compiles and runs but scanf won't give me any numbers but 0 for a
floating integer. This is a homework problem, and not finished yet, but all I
need is for someone to please show me where I am goofing it...
program:
include <stdio.h>
main()
{
int weight, deliveryCharge;
printf("Please enter in the weight of the package in kg\n");
scanf(" %f", &weight);
printf("%.2f\n",weight);
if(weight<2.5)
{
deliveryCharge = weight * 3.5;
}
else deliveryCharge = weight * 2.45;
printf("The delivery charge will be %.2f.\n",deliveryCharge);
system("pause");
return 0;
here is what I get:
Please enter in the weight of the package in kg
2
0.00
The delivery charge will be 0.00.
Press any key to continue . . .
When I change it to an integer (using %d) it works fine.
That second print f line is only to tell me what I am getting from scanf,
which is apparently nothing...
This project is in C, on a Vista machine...I have been doing all my work on
this machine, and after getting a few bugs worked out, it hasn't been a
problem.
Thanks for any help
froglipz
figured it out, but thanks :)
If you add teh compiler options -Wformat -Werror, the compiler would have
caught that error for you. For good measure ass -Wall too.
Yeah, my instructor said not to include those...probably so I could spend a
few hours staring at it before I figured out that I declared an integer for a
float variable :/
Then your instructor is a fool. Why would you not want teh compiler to help
you detect coding errors!?
Using Dev-C++ is not too btight an idea either; when learning to code, you'd
benefit from a decent symbolic debugger, and that is not a description that
fits Dev-C++'s dismal debugging facility.
I think he wants us to make our mistakes so we can learn from them...anyway,
it matters not what we think, I just have to do as instructed for a couple
more months then I can go back to using code::blocks :)
Code::Blocks wasn't what I was thinking of; it uses the same MinGW/GCC
toolchain, and the same underlying GDB debugger; although the integration with
the debugger is somewhat more fully featured and robust, but not a patch on
VC++2008 Express.
Regarding making your own mistakes and learning from them, you still too that
with a higher warning level - only faster! No professional developer of any
merit would deliberately cripple the compiler's ability to improve the quality
of his code. In fact well equipped developers use additional static analysis
tools to further inspect the code.
Moreover warnings are issued for syntactically valid code that is suspect of
dangerous in some circumstances; it is therefore highly likely that you will
produce bad code that will appear to work - until it doesn't. The whole
concept of switching off warnings is foolish. I'd gladly explain this to your
tutor if you care to direct him to this forum.
My point is (had you not found it yourself), you'd have got the solution by
asking humans on this forum and waiting for a reply; OR you could have had the
compiler tell you immediately; which is the most efficient,? And would you
really have learned less by using the more efficient method?
... I suspect the real reason for your tutor's directive is more likely that
he like many poorer practitioners of the art he erroneously think that
warnings are not important (when often they are semantic errors), and most
probably his own code generates warnings because of its poor quality, and he
prefers to put his head in the sand rather than figure out what is wrong with
his code.