Archived

This topic is now archived and is closed to further replies.

Rudan

Strange scanf error

Recommended Posts

Rudan    158
I get a very strange error in my program with this code:
	float temp;

	printf("X: ");
	scanf("%f", &temp);
	fwrite(&temp,sizeof(float),1,file);
 
It compiles fine but when I run it I get a debug error and the whole thing crash. The funny thing is that if I recompile it works sometimes and sometimes not even through it''s still the same code. For example, I tried to change the &temp to just temp and ofcourse that didn''t work(I knew that but I have been trying to get this to work for hours so I was desperate). Then when I changed it back it worked. And then, about 20 minutes later it crashed again. I know the problem is at the scanf line, ''cause if I delete it everything works fine. HELP before I do something stupid, like setting fire to myself ...

Share this post


Link to post
Share on other sites
wrenhal    122
quote:
Original post by Rudan

I get a very strange error in my program with this code:

float temp;

printf("X: ");
scanf("%f", &temp);
fwrite(&temp,sizeof(float),1,file);


It compiles fine but when I run it I get a debug error and the whole thing crash. The funny thing is that if I recompile it works sometimes and sometimes not even through it''s still the same code. For example, I tried to change the &temp to just temp and ofcourse that didn''t work(I knew that but I have been trying to get this to work for hours so I was desperate). Then when I changed it back it worked. And then, about 20 minutes later it crashed again. I know the problem is at the scanf line, ''cause if I delete it everything works fine. HELP before I do something stupid, like setting fire to myself ...


Are you waiting for input from a user???
Because otherwise, it''s going to pull in whatever is in the buffer. I''d say you might flush the buffer right before.
What compiler are you using? DOS?, or Win32 compiled as a Console app?? I''m just learning so I don''t know much, but I''ll try and help.




BeS
It''s Da BOMB Baby!!!
. o O ~
A little nonsense now and then,
is relished by the wisest men
~ O o .
-- Willy Wonka

Share this post


Link to post
Share on other sites
Rudan    158
When I use scanf, wont it wait until I hit enter? It sure seems so to me...

Well, right now it''s working and I found something that might have been wrong. I wasn''t closing a file correctly but that shouldn''t affect the scanf, right?

Share this post


Link to post
Share on other sites
ImmaGNUman    122
Always give the debug info if you have debugged it, especially if you say you debugged it cuz thats just annoying.

Anyway, try adding a printf after the scanf and a printf after the the fwrite. Also, can you post all the code of the program, or at least the function(where you fopen, etc.).

-----------------------------

A wise man once said "A person with half a clue is more dangerous than a person with or without one."

The Micro$haft BSOD T-Shirt

Share this post


Link to post
Share on other sites