scanf how to return an invalid sign.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • scottab1234
    New Member
    • Sep 2018
    • 1

    scanf how to return an invalid sign.

    I want to put a condition in c programing where if I ask a user for an input and they put a space between inputs I can give an error/invalid sign.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    This is very hard. You see, when the user inputs data your program is not running. It is paused by C until the user presses the ENTER key. You cannot control when the user presses this key.


    After that your program resumes and you get to figure out what was entered.


    All of the functions (like scanf, printf, etc) are provided just to get you started but they are not robust.



    The work around is to examine each character as it is entered. Now then the user enters 1 then 2 then 3 you will need to process
    these numbers to make an int of 123. But even now you cannot get the user to stop entering data.


    As a beginner, just assume the user does the right thing. Later you can purchase code libraries that do all this hard stuff.

    Comment

    Working...