How to specify inputs using scanf in a particular format

This is a discussion on How to specify inputs using scanf in a particular format within the C Programming forums, part of the General Programming Boards category; Hi, I want to make a program that accepts the radius of a circle and co-ordinates of the center. I ...

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    3

    How to specify inputs using scanf in a particular format

    Hi,
    I want to make a program that accepts the radius of a circle and co-ordinates of the center.
    I want the user to specify the inputs as:
    radius:{(x,y)}

    eg, if the radius is 4, and center is at 1,2, then user should specify it as:
    4:{(1,2)}

    How to do this in C?

    Code:
        scanf("%*c %*c %d %*c %d %*c %*c",'{','(',&x,',',&y,')','}');

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,344
    In a format string, each character stands for itself, apart from %

    So
    Code:
    scanf("%d:{(%d,%d)}", &r, &x, &y );
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    3
    Quote Originally Posted by Salem View Post
    In a format string, each character stands for itself, apart from %

    So
    Code:
    scanf("%d:{(%d,%d)}", &r, &x, &y );
    thanx for ur response but its not working!!
    do u hv any other solution?

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,344
    You really need to do better than bouncing back "It doesn't work".

    Of course if works!
    Code:
    $ cat foo.c
    #include<stdio.h>
    int main ( int argc, char *argv[] ) {
        char message[] = "4:{(1,2)}";
        int x, y, r;
        if ( sscanf( message, "%d:{(%d,%d)}", &r, &x, &y) == 3 ) {
          printf("Qapla'!: %s parsed out %d %d %d\n", message, r, x, y);
        } else {
          printf("You have dishonoured the ways of C\n");
        }
        return 0;
    }
    
    $ gcc -Wall foo.c
    $ ./a.out 
    Qapla'!: 4:{(1,2)} parsed out 4 1 2
    Now, YOU post what you actually tried, and what you actually typed in, and so on.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    3
    Actually i want user to provide the input.

    Code:
    printf("\nEnter the radius:");
    scanf("%d",&r);
    printf("\nEnter the coordinates of the center:");
    scanf("%d%d",%x,%y);
    Instead of this, i want user to enter like this:

    Code:
    printf("\nEnter the radius and the coordinates of the center: [r:{(x,y)}]
    scanf(......);
    what should i write in the scanf() so that the user on running the program, see the screen like this:

    Code:
    Enter the radius and the coordinates of the center: [r:{(x,y)}]
    4:{(1,2)}      // user enters only 4,1,2. not :,{,(,),}
    but i dont know how to do this. can u plz help me with this?
    Last edited by Salem; 06-27-2011 at 05:34 AM. Reason: fix tags

  6. #6
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,532
    Code:
    Enter the radius and the coordinates of the center: [r:{(x,y)}]
    4:{(1,2)}      // user enters only 4,1,2. not :,{,(,),}
    If the user only enters the numbers, where does the rest of the format come from?

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,344
    If you want that level of cursor and display control, then you're going to have to use something like ncurses, or some other terminal control library for your OS/Compiler.

    Standard ISO-C doesn't have anything which can do what you ask.

    If you're still learning, you're making this needlessly complicated.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by adityamehta View Post
    Actually i want user to provide the input.
    Code:
    Enter the radius and the coordinates of the center: [r:{(x,y)}]
    4:{(1,2)}      // user enters only 4,1,2. not :,{,(,),}
    but i dont know how to do this. can u plz help me with this?
    1) Stop changing your story... decide what the user has to type and program it...

    2) If you just want the numbers use scanf("%d,%d,%d",&r,&x,&y); ... this would requre the user to type 10,5,2 and hit Enter.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    This thread is a good example of why people hate scanf. Why do they hate it? Because they can't decide what format they really want, and they expect it to be some magical function that translates whatever random thing they decide to type at any given moment into what they really meant.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to handle inputs if the user inputs the wrong thing
    By bassist11 in forum C Programming
    Replies: 5
    Last Post: 09-22-2010, 04:28 AM
  2. Help with inputs
    By RustySpoon in forum C Programming
    Replies: 3
    Last Post: 03-14-2010, 09:38 PM
  3. How do I validate these inputs?
    By Kyeong in forum C Programming
    Replies: 1
    Last Post: 10-19-2008, 02:20 PM
  4. scanf with two inputs acting funny
    By yougene in forum C Programming
    Replies: 2
    Last Post: 08-19-2007, 04:17 PM
  5. Replies: 9
    Last Post: 11-27-2001, 08:25 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21