HOW TO SCAN MULTIPLE TIMES USING while()?????

Discussion in 'C' started by jose_peeterson, Jun 27, 2011.

  1. jose_peeterson

    jose_peeterson New Member

    Joined:
    May 19, 2011
    Messages:
    56
    Likes Received:
    1
    Trophy Points:
    0
    Code:
    //hi experts,
      //              i can't get the following code to read in  multiple times after the 
    "more(y/n)" printout. it does not scan a second time it just exists the loop after just once 
    //can you please fix the bug in this simple program.
    
    char op = 'y';
    
    while(op == 'y') 
     {    
      printf("Enter formula: ");
      scanf("%s",molecule);
     
      l = strlen(molecule);
    
      calc_weight(element,s,molecule,l);
     
     printf("More? (y/n) : "); // THIS DOES NOT WORK???
     scanf("%c",&op);       //      WHY???????
    
     }
     
    
    return 0;    
        
    }
     
    Last edited by a moderator: Jun 27, 2011
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Use fflush before the scanf and that should work.
     
  3. priyatendulkar

    priyatendulkar New Member

    Joined:
    Jun 20, 2011
    Messages:
    20
    Likes Received:
    1
    Trophy Points:
    0
    The issue is in scanf
    scanf reads the input after enter/return key is pressed.But the newline generated by it is not consumed by scanf.Hence next time when scanf reads the input it reads newline instead of your input.

    I tried using fflush on my Linux system..2.6.9-22.EL..But it doesnt seem to work

    Alternative solution could be as under:
    U can use scanf as
    scanf("%c%*c",&op);
    In this case scanf will read the newline and discard it
     
  4. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    scanf is a crap way of getting user input. Use fgets instead and parse the string.
     
  5. jose_peeterson

    jose_peeterson New Member

    Joined:
    May 19, 2011
    Messages:
    56
    Likes Received:
    1
    Trophy Points:
    0
    hi thanks for replies BUT priyatendulkers method reads a second time but when it reaches the condition in the while loop it fails and exits the loop even after typing y.
    please help me
    thanks
     
  6. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    After using fgets you can still use sscanf if you really want to. But writing parsing code yourself is better; user input is usually not rigidly formatted enough for the scanf family to handle it. You'll need to know about the family of functions that includes atoi() (ascii to integer).
     
  7. priyatendulkar

    priyatendulkar New Member

    Joined:
    Jun 20, 2011
    Messages:
    20
    Likes Received:
    1
    Trophy Points:
    0
    It fails as u might have not modified
    scanf("%s",molecule);

    Even here u'll need to consider the same

    scanf("%s%*c",molecule);

    Use the above method if u still wish to use scanf...

    Else u can use fgets instead as suggested by xpi0t0s which would be a better approach
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice