Thread: Check if input has been recieved

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    110

    Check if input has been recieved

    Is there a way to check whether an input has been received in c?

    for example say If I need to input a variable with scanf, then trigger a string to be printed with printf, then input another variable, then run the program. e.g:

    Enter username: (printf)
    gorgeous george (If input recieved...)
    Enter password: (...then print this)
    george123 (scanf again)

    is there any function to do this? cheers

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    There is no standard function for doing this. Both Linux and Windows have system specific functions that in one way or another can let you know "is there anything entered" or "if no input for X time, tell me that there was nothing there".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    All the input functions will wait patiently while input is typed in.

    You should also investigate fgets, to get the whole line vs. until the first space.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    110
    Quote Originally Posted by matsp View Post
    There is no standard function for doing this. Both Linux and Windows have system specific functions that in one way or another can let you know "is there anything entered" or "if no input for X time, tell me that there was nothing there".

    --
    Mats
    Ok, thanks, how does one make use of these commands in the program? I'm currently doing the programming on ubuntu.

    All the input functions will wait patiently while input is typed in.

    You should also investigate fgets, to get the whole line vs. until the first space.
    Thanks, i'll have a look a fgets, I can most definitelty use something like that.

    Actually Once the input is entered and you hit return, doesn't the program just continue to the next line, so I could use printf.. scanf... printf... scanf, and once I've entered the second input, it would continue?

  5. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    ncurses

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    110
    I'll have to look into these things, but right now I'm hitting the sack. Thanks for the replies

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If you want to do it natively in Linux, you will need to use:
    http://www.manpagez.com/man/3/tcgetattr/
    http://www.manpagez.com/man/3/tcsetattr/
    http://www.manpagez.com/man/4/termios/

    There is also a "how to read input without enter" in the FAQ which shows some of the usage of these functions.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Oct 2008
    Posts
    110
    Quote Originally Posted by matsp View Post
    If you want to do it natively in Linux, you will need to use:
    http://www.manpagez.com/man/3/tcgetattr/
    http://www.manpagez.com/man/3/tcsetattr/
    http://www.manpagez.com/man/4/termios/

    There is also a "how to read input without enter" in the FAQ which shows some of the usage of these functions.

    --
    Mats
    Thanks, however I don't think I will actually need any of these things yet after all, as I realised all I want to do was continue with the program once the input had been recieved, so I now have something like:

    Code:
    float a,b;
    
    printf("Enter first value:");
    scanf("%f", b);
    printf("Enter second value:");
    scanf("%f",a);
    and this works how I want it too, since you are only prompted for the second value once you enter the first and hit return. I was thinking I might have to use an if function to see whether the input has been recieved, and i it has, then print the next line to prompt for value 2, but I see it's not that complicated after all.

  9. #9
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    And for robustness it would be a good idea to check if input has been received after all the program should not have to wait indefinitely for input.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by itCbitC View Post
    And for robustness it would be a good idea to check if input has been received after all the program should not have to wait indefinitely for input.
    Why not? Unless there is a problem with waiting indefinitely (e.g. some other operation needs to be performed within a certain time), I don't see the problem with waiting. Yes, there are situations that you don't want to wait forever, but in most cases it's just fine to wait.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Registered User
    Join Date
    Oct 2008
    Posts
    110
    Quote Originally Posted by matsp View Post
    Why not? Unless there is a problem with waiting indefinitely (e.g. some other operation needs to be performed within a certain time), I don't see the problem with waiting. Yes, there are situations that you don't want to wait forever, but in most cases it's just fine to wait.

    --
    Mats
    yeh and it saves me learning a hell of alot of information on linux specific functions, when all I want to do is wait for the user to input a value, then continue.

  12. #12
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by matsp View Post
    Why not? Unless there is a problem with waiting indefinitely (e.g. some other operation needs to be performed within a certain time), I don't see the problem with waiting. Yes, there are situations that you don't want to wait forever, but in most cases it's just fine to wait.

    --
    Mats
    Sure no harm waiting not like its a resource hog but why wait indefinitely. The only reason for coding something like this would be to provide a shell environment for other programs.

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by itCbitC View Post
    Sure no harm waiting not like its a resource hog but why wait indefinitely. The only reason for coding something like this would be to provide a shell environment for other programs.
    Because there is no reason to NOT wait indefinitely, and it is a much easier way to handle things. If you want a timeout, you need to set things up with a timeout of some sort, then check the result and deal with what happens if you get a timeout. What are you going to do if there is a timeout? Exit, I suppose?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #14
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by matsp View Post
    Because there is no reason to NOT wait indefinitely, and it is a much easier way to handle things. If you want a timeout, you need to set things up with a timeout of some sort, then check the result and deal with what happens if you get a timeout. What are you going to do if there is a timeout? Exit, I suppose?

    --
    Mats
    Just a suggestion and as the O/P is content with waiting so be it and of course it keeps things simple. Certainly the code gets complicated if the program needs to either exit after a certain time or supply a default value and continue instead of waiting indefinitely.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Input class project (again)
    By Elysia in forum C++ Programming
    Replies: 41
    Last Post: 02-13-2009, 10:52 AM
  2. can someone help me with these errors please code included
    By geekrockergal in forum C Programming
    Replies: 7
    Last Post: 02-10-2009, 02:20 PM
  3. allegro issues
    By mramazing in forum C++ Programming
    Replies: 1
    Last Post: 01-07-2009, 11:56 PM
  4. HELP!!!!emergency Problem~expert please help
    By unknowppl in forum C++ Programming
    Replies: 9
    Last Post: 08-21-2008, 06:41 PM
  5. check if user input matches a word
    By fakebloo in forum C++ Programming
    Replies: 1
    Last Post: 12-05-2004, 07:12 PM