PDA

View Full Version : percentage c in scanf



Narayana
08-07-2006, 02:42 PM
in gcc, if i do something like



scanf("%c", &ch);

if i had entered a previous \n [newline] through input, almost always ch is assigned that newline. i tried doing
fflush(stdin); before accepting the value of ch, but it doesn't seem to work.. Noticed this some 2 years back,, but always used the work around :


scanf("%s", temp);
ch = temp[0];


am i doing anything wrong?? any way to flush input buffer for sure??
:Ksp: :Ksp:

ashokcsn_2000
08-07-2006, 07:54 PM
depending on the platfom and the compiler you are using the espace character acceptance using scanf varies some of them take the newline sequence as "cr + lf" whcih typically translates to 2 sepeparate inputs, while someother platforms translate it to plain ascii 13 (reminds you of the devil, doesnt it, exactly what it is) The reason why

scanf("%s", temp);
ch = temp[0];

works since it treats it as a string and irrespective you are choosing the first character (cr = ascii 13)

The workaround again depends on the platform. if you are on the DOS environment use BIOS keyboard commands instead.

Ashok

Narayana
08-08-2006, 02:02 AM
cariiage return + line feed taken as two diff. inputs??? :00: :00: i didn't know that..

but i DON'T want the newline character... Its the newline I enter for some previous input.. I actually want to discard it, and take the next input character...

ashokcsn_2000
08-08-2006, 02:18 AM
Strange it is... but computers were built at a time when there were no LCS or even monitors and all output was through a print writer and new line was always cr (move the print carraige to 0 pos) + lf (move to next line) and many systems still consider <enter> key that way.