• Popular Now

• 15
• 15
• 11
• 9
• 10

Archived

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

Strange scanf error

This topic is 6263 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

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 on other sites
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 on other sites
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 on other sites
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