Thread: scanf in while problem

  1. #1
    Registered User
    Join Date
    Nov 2015
    Posts
    24

    scanf in while problem

    Hello everybody,
    I'm new at programming in C and because of that when I was trying to make a loop in C a problem came up and I have no idea of what is going on, hope you can help me with this :

    Code:
    void le_posicao(int *coor_i, int *coor_j, int dim_tab)
    {
        int nconv=0;
        do
        {
        printf("Insira as coordenadas de uma luz na forma (a,b): ");
        nconv=scanf("(%d,%d)",coor_i,coor_j);
        }while(nconv!=2);
    }

    I created a function to read data from the user but the condition goes to infinity i don't know why...

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Do you actually type in (123,456) when prompted?

    Unless the first character seen by scanf is literally '(', it will just go into an endless loop, unless you do something to the input stream.

    See FAQ > Flush the input buffer - Cprogramming.com
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2015
    Posts
    24
    Well that's the point, what I want to do is force the user to type the '(', the ',' and the ')' but in spite of asking new values to the user it just goes to infinity. Is there a way to make it work in a way that the user must type (a,b) in this precise format?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Like I showed you, flush the input buffer if they get it wrong, and go round the loop again.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    Opportunistic scanning is your friend. If you read the input (line) into a buffer, you can use sscanf() with different patterns. For example,
    Code:
        char line[200], dummy;
        double x, y;
    
        if (fgets(line, sizeof line, stdin) == NULL) {
    
            /* No more input: user pressed CTRL+D,
             * or no more piped or redirected input.
             * You should exit here, as there will
             * not be any more input.
            */
    
        } else
        if (sscanf(line, " ( %lf , %lf ) %c", &x, &y, &dummy) == 2 ||
            sscanf(line, " [ %lf , %lf ] %c", &x, &y, &dummy) == 2 ||
            sscanf(line, " < %lf , %lf > %c", &x, &y, &dummy) == 2 ||
            sscanf(line, " ( %lf %lf ) %c", &x, &y, &dummy) == 2 ||
            sscanf(line, " [ %lf %lf ] %c", &x, &y, &dummy) == 2 ||
            sscanf(line, " < %lf %lf > %c", &x, &y, &dummy) == 2 ||
            sscanf(line, " %lf %*1[:;,/xX] %lf %c", &x, &y, &dummy) == 2) {
            sscanf(line, " %lf %lf %c", &x, &y, &dummy) == 2) {
    
            /* It was a 2D vector.
             * The line did not contain anything except the vector
             * (we verified that by scanning an additional "dummy" char).
             * The components are in the x and y variables.
            */
    
        } else {
    
            /* Tell the user you couldn't understand the line, and exit.
             * Or, tell the user about the expected formats, and retry.
            */
        }
    The %*1[:;,/xX] pattern is an interesting one there. It uses the [ conversion character (with the list of allowed characters immediately following it), and ends with ]. Due to the *, it does not store anything, and even if it matches, it is not counted as a conversion in the result value. Due to the 1, it matches one character (technically, always at least one, up to the specified number, of characters). That character must be one of : ; , / x X .

    Because you read the line, you don't need to exit if the line could not be parsed, but can for example tell the user about the formats supported.

    (The reason for allowing : / x in addition to a semicolon or a comma is that I like to specify vectors as single command-line parameters using format widthxheight or xxx:yyy or xxx/yyy/zzz -- these do not interfere with shell special characters like ; and space do. Some locales use , as the decimal point or thousands separator, so its not a good list separator. (If you just include <locale.h> and call setlocale(LC_ALL, ""); before trying to scan anything, both scanning and printing will automagically use the decimal point specified in your locale. Which is nice.)

  6. #6
    Registered User
    Join Date
    Nov 2015
    Posts
    24
    Alright I think I understood your piece of code, thank you so much! So basically with that any type of input will be accepted, is it?

  7. #7
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    Quote Originally Posted by khanny View Post
    So basically with that any type of input will be accepted, is it?
    Why don't you test it, and find out?

    Just add a printf("x=%f, y=%f\n", x, y); into the if case body, and then throw different vector formats at it to see what works. That's how I decided on those patterns, anyway.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem with scanf and EOF
    By almogz in forum C Programming
    Replies: 2
    Last Post: 05-10-2013, 04:11 AM
  2. problem with scanf
    By shruthi in forum C Programming
    Replies: 10
    Last Post: 01-19-2012, 03:10 AM
  3. problem with scanf() in while
    By hugoguan in forum C Programming
    Replies: 9
    Last Post: 11-23-2010, 10:17 AM
  4. Problem with RPC and scanf
    By Ricardo_R5 in forum C Programming
    Replies: 11
    Last Post: 01-08-2007, 06:15 PM
  5. scanf problem
    By a1dutch in forum C Programming
    Replies: 8
    Last Post: 04-19-2005, 04:14 AM