Thread: input using scanf()

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    5

    input using scanf()

    I am trying to use scanf() to read in input someone types to the command line after the program is running. The problem i am running into is i need multiple different forms of input. For example they might type "quit" the first time and "quit 5" the next. Is there a way to pull the entire thing in as a string and then parse out the quit and the number if it got entered? I want to be able to have a char[] with quit inside and the 5 stored as an int. Thanks for your help.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    use fgets and sscanf pair
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    methinks the scanset functionality of scanf() is your best bet
    Code:
    char s[100];
    scanf("%[^\n]", s);

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by itCbitC
    methinks the scanset functionality of scanf() is your best bet
    What vart suggested might be better, especially since your example is a bad bet. It should be:
    Code:
    char s[100] = "";
    scanf("%99[^\n]", s);
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Well fgets() and sscanf() would work just as well as [s]scanf() with the scanset control string format.
    And yes to prevent going out-of-bounds it's a good idea to prefix the length of the target array in the control string.
    Last edited by itCbitC; 09-09-2009 at 12:43 PM.

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by itCbitC View Post
    Well fgets() and sscanf() would work just as well as [s]scanf() with the scanset control string format.
    And yes to prevent going out-of-bounds it's a good idea to prefix the length of the target array in the control string.
    Just that scanf is a lot more complex itself... You do not use space shuttle to get to the shop next door?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. fscanf in different functions for the same file
    By bchan90 in forum C Programming
    Replies: 5
    Last Post: 12-03-2008, 09:31 PM
  3. input from scanf into structure, how?
    By Elite in forum C Programming
    Replies: 8
    Last Post: 03-04-2005, 02:59 PM
  4. Restrict scanf() input?
    By difficult.name in forum C Programming
    Replies: 4
    Last Post: 09-20-2004, 12:27 PM
  5. help with input and scanf
    By kpw in forum C Programming
    Replies: 9
    Last Post: 08-28-2001, 08:21 PM