1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

scanf

Discussion in 'C' started by Amit Kumar Saha, Aug 8, 2008.

  1. Amit Kumar Saha

    Amit Kumar Saha New Member

    Joined:
    Aug 1, 2008
    Messages:
    14
    Likes Received:
    0
    Trophy Points:
    0
    :thinking: I want to know scanf statement does not wait for a input in compiler?
    or when it takes more input than the fixed number of input??
    I use Microsoft visual studio 6.0
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    Could you rephrase the question please, because it doesn't make any sense. scanf gets input from the user, I don't understand what "scanf statement does not wait for a input in compiler" means. scanf doesn't wait for anything from the compiler.

    Are you having a problem with some code?
     
  3. Amit Kumar Saha

    Amit Kumar Saha New Member

    Joined:
    Aug 1, 2008
    Messages:
    14
    Likes Received:
    0
    Trophy Points:
    0
    Many thanks. Here is the detailed information.
    I am executing a infinite while loop. now I want check a character input to justify whether the user is still interested tu carry on the loop. But here the scanf is not working.
    thanking again
     
  4. faizulhaque

    faizulhaque New Member

    Joined:
    May 23, 2008
    Messages:
    213
    Likes Received:
    3
    Trophy Points:
    0
    Occupation:
    Student
    Location:
    Karachi
    Home Page:
    use getche() rather than scanf command for Character inputs.
    like

    Code:
    char ch;
    ch=getche();
    printf("%c",ch);
    
    
     
    Last edited: Aug 13, 2008
  5. Amit Kumar Saha

    Amit Kumar Saha New Member

    Joined:
    Aug 1, 2008
    Messages:
    14
    Likes Received:
    0
    Trophy Points:
    0
    Thanks for help.

    But I have tried that one. But it still does not work. Is there any other solution?

    Thanking you

    Amit
     
  6. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    How exactly doesn't it work? What doesn't it do that you want it to?
    Do you want the check for user input to pause the loop, or do you want the loop to continue running while a check for user input is made?
     
  7. faizulhaque

    faizulhaque New Member

    Joined:
    May 23, 2008
    Messages:
    213
    Likes Received:
    3
    Trophy Points:
    0
    Occupation:
    Student
    Location:
    Karachi
    Home Page:
    would u plz give the snap of error command line.
     
  8. Amit Kumar Saha

    Amit Kumar Saha New Member

    Joined:
    Aug 1, 2008
    Messages:
    14
    Likes Received:
    0
    Trophy Points:
    0
    Yes I was trying to check any input to break a infinite while loop.

    thank you
     
  9. faizulhaque

    faizulhaque New Member

    Joined:
    May 23, 2008
    Messages:
    213
    Likes Received:
    3
    Trophy Points:
    0
    Occupation:
    Student
    Location:
    Karachi
    Home Page:
    use this
    int a=1;
    while (a==1)
    {
    printf("Enter Any Number Accept 1");
    scanf("%d",&a);
    }

    above one is giving u infinit loog but when the user give input 1 than it's terninate.
     
  10. Amit Kumar Saha

    Amit Kumar Saha New Member

    Joined:
    Aug 1, 2008
    Messages:
    14
    Likes Received:
    0
    Trophy Points:
    0
    But I was trying to take input a character in the same way. I have already applied the technique that you suggested for me. But I want to take a character. Is there any way in the same manner??

    Thanking you in advance
     
  11. faizulhaque

    faizulhaque New Member

    Joined:
    May 23, 2008
    Messages:
    213
    Likes Received:
    3
    Trophy Points:
    0
    Occupation:
    Student
    Location:
    Karachi
    Home Page:
    for charachtor
    Code:
    main();
      char another='y';
      while (another=='y')
      {
        printf("Type Any charactor Except y ");
        another=getche();
      }
    }
     
  12. Amit Kumar Saha

    Amit Kumar Saha New Member

    Joined:
    Aug 1, 2008
    Messages:
    14
    Likes Received:
    0
    Trophy Points:
    0
    I have already tried that one. And I am honest. Thank you for your suggestion.
     
  13. faizulhaque

    faizulhaque New Member

    Joined:
    May 23, 2008
    Messages:
    213
    Likes Received:
    3
    Trophy Points:
    0
    Occupation:
    Student
    Location:
    Karachi
    Home Page:
    what type of probelm u are facing i couldn't get u
     
  14. Amit Kumar Saha

    Amit Kumar Saha New Member

    Joined:
    Aug 1, 2008
    Messages:
    14
    Likes Received:
    0
    Trophy Points:
    0
    The way that you suggested me, exactly in the same way I have tried. but it did not do the trick for me. if I use fflush then it takes a input. But why it does not take without fflush?
     
  15. Amit Kumar Saha

    Amit Kumar Saha New Member

    Joined:
    Aug 1, 2008
    Messages:
    14
    Likes Received:
    0
    Trophy Points:
    0
    can ant body give me code to find out Fibonacci number?

    thank you inadvance
     
  16. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    probably better to start a new thread for a new topic.
     
  17. aditiya

    aditiya New Member

    Joined:
    Aug 21, 2008
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Ur question is not clear.if u want to take a character during the loop then you can take it like this.It is working fine

    char ch='Y';
    printf("Do U want to continue loop ?Press Y:N");
    while(ch=='Y')
    {
    scanf("%c",&ch);
    fflush(stdin);
    }
     
  18. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    You still haven't explained how or why our suggestions don't work.

    A shot in the dark: are you asking if it's possible to check the keyboard without stopping the while loop from running? If the problem with scanf is that it stops processing while it waits for input?
     
  19. Chinmoy

    Chinmoy New Member

    Joined:
    Feb 13, 2008
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    Scanf reads input overflows from preious input statements. Use scanf with care i would suggest. Or use a space in front of the control string in scanf("%c",...) as scanf(" %c",...). This destroys the previous input buffer.
     
  20. Amit Kumar Saha

    Amit Kumar Saha New Member

    Joined:
    Aug 1, 2008
    Messages:
    14
    Likes Received:
    0
    Trophy Points:
    0
    Your suggestion helped me out a lot thanks.
     

Share This Page