Here is the code from the book for a Conversion program:
#include %26lt;stdio.h%26gt;
main()
{
int fahr;
for (fahr = 0; fahr %26lt;= 300; fahr = fahr + 20)
printf("%3d %6.1f\n", fahr, (5.0 / 9.0) * (fahr - 32));
}
And here it is my code:
#include %26lt;stdio.h%26gt;
main()
{
int fahr;
scanf ("%d", fahr);
printf("%3d %6.1f\n", fahr, (5.0 / 9.0) * (fahr - 32));
}
I tried this code, and I also tried to change something on the printf and put a d for both and others! Nothing works! I do not want the with the for loop, I just want me to input my temperature!
What am i doing wrong? When I compile the book's code it works perfectly, when i compile mine, I get segmentation fault, core dumped!
I am desperate with this, since I want to learn C and I will not move on until I resolve this problem
Thank you
C annoying and stupid problem!If i put the code from the book, it works, if I put mine, It doesn't work!?
http://www.cppreference.com/stdio/scanf....
You want to do scanf ("%d", %26amp;fahr);
Scanf takes in arguments by reference.
EDIT: Note the %26amp; sign.
I actually missed it at first and spotted it when you said you had a segmentation fault; I then guessed correctly you forgot the %26amp; in scanf.
Segfaults usually occur with bad memory access. Passing in an integer instead of a reference to an integer is one way to cause a segfault.
Also, to nitpick, it should be int main(), not just main(). As defined by C and C++ standards, main() return an integer. Yes, I know quite a bit of code including that in many C books omits the int.
One more thing, this is a really basic beginner problem (with scanf), and anyone who has decent experience in C would have caught it. You probably want to find a forum with such experienced people. I would suggest http://forums.devshed.com/ or http://cboard.cprogramming.com/ (it's down right now, will be back up)
Both are frequented by actually knowledgeable people, and the forums are conducive to programmer's posts.
Reply:put int in front of main()
would look like
int main()
It compiles then---both cases....whether the semantecs are correct is a different story.
Reply:I'm not sure if this is the problem, but you're doing floating point arithmetic on an integer without casting it.
Reply:No error codes? It only takes one comma off or one pair parentheses not matched, or one ' or " unmatched and you are in trouble. ~
Mismatched variables... concentrate on your changes only.
Sleep on it and then look again.
Reply:I do not have a C compiler. Try this. Remove the scanf and enter a value for fahr as follows.
fahr=50.0 (do not input)
See if your program works. If it does, then the problem is with scanf and your compiler.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment