percentage c in scanf
+ Reply to Thread
Results 1 to 4 of 4

Thread: percentage c in scanf

  1. #1
    Join Date
    Sep 2004
    Location
    Chennai
    Posts
    1,489

    Default percentage c in scanf

    in gcc, if i do something like

    Code:
    scanf("%c", &ch);
    if i had entered a previous \n [newline] through input, almost always ch is assigned that newline. i tried doing
    Code:
    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 :
    Code:
    scanf("%s", temp);
    ch = temp[0];
    am i doing anything wrong?? any way to flush input buffer for sure??
    I Will Be Back!!!

  2. #2

    Default

    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
    மின்னல் பாதி தென்றல் பாதி மங்கை ஆனது

  3. #3
    Join Date
    Sep 2004
    Location
    Chennai
    Posts
    1,489

    Default

    cariiage return + line feed taken as two diff. inputs??? 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...
    I Will Be Back!!!

  4. #4

    Default

    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.
    மின்னல் பாதி தென்றல் பாதி மங்கை ஆனது

+ Reply to Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts