Strange scanf error

Started by
2 comments, last by Rudan 23 years, 2 months ago
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 ...
-----------------------------Reporter: Are they slow-moving, chief?Sheriff: Yeah, they're dead. They're all messed up.-Night of the living dead
Advertisement
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
BeSIt's Da BOMB Baby!!!. o O ~ A little nonsense now and then,is relished by the wisest men~ O o .-- Willy Wonka
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?
-----------------------------Reporter: Are they slow-moving, chief?Sheriff: Yeah, they're dead. They're all messed up.-Night of the living dead
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
-----------------------------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

This topic is closed to new replies.

Advertisement