Thread: Passing input as an argument to a function

  1. #1
    Registered User GeorgeIO's Avatar
    Join Date
    Nov 2017
    Posts
    33

    Passing input as an argument to a function

    Hi all, I have a query - I've implemented a queue data structure and although it works fairly well, there's something I want to implement and I'm not sure how to do it. My program uses commands for different queue functions, ie to add an element to the queue the user presses 'i' on the keyboard. They are then prompted to enter their id number, which is then added to the queue.

    However, what I would like to be able to do is allow the user to input the command and their id number (the argument) at the same time, rather than having to do it in two stages. For example, a user whose id number is 764 must type in this command on arrival:>>> i 764.

    Can anybody give me any guidance as to how I would implement this?

    My current code is as follows:

    Code:
    /*ENQUEUE FUNCTION (Inserts element at rear end)*/
    
    void insert(int data) {
        system("clear");
        {
            if ((rear + 1) % MAX == front)
            {
                printf("\nSorry, the waiting queue is full. Please try again later.\n");
            }
            else
            {
                if (front == -1 && rear == -1)
                {
                    front = front + 1;
                }
                rear = rear + 1;
                queue[rear] = data;
                printf("Welcome Patient No. %d\nAll Doctors a busy right now.\nPlease relax in the waiting area.\n", data);
    
    
                for (int i = front; i <= rear; i++)
                {
                    if (queue[i % MAX] == data) {
                        printf("Position in queue is %d \n\n", (i%MAX + 1) - front);
                    }
                }
            }
        }
    }
    which ties in with this part of my switch statement of commands:

    Code:
    case 'i':            
                printf("Enter patient ID #: \t");
                scanf("%d", &id);
                insert(id);
                break;
    Thank you

  2. #2
    Banned
    Join Date
    Aug 2017
    Posts
    861
    scanf is able to take in more than one at a time.
    Code:
    scanf("%d%d",&i,&d:);

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    It's not really something you need to implement... Even if you didn't read it with a scanf call like userxbw's, if the user knows how the program works, then they could just type ahead. The first scanf for the menu would read the i, and then the second scanf for the id would read the number.

    I would be careful though, the best way to read letters in scanf is to start the string with a space and then the %c specifier. The space will stop any stray \n from being read into your menu option variable (the one that you switch on later).

  4. #4
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by whiteflags View Post
    It's not really something you need to implement... Even if you didn't read it with a scanf call like userxbw's, if the user knows how the program works, then they could just type ahead. The first scanf for the menu would read the i, and then the second scanf for the id would read the number.

    I would be careful though, the best way to read letters in scanf is to start the string with a space and then the %c specifier. The space will stop any stray \n from being read into your menu option variable (the one that you switch on later).
    so that is the trick. I'm going to go give that a try.

    something like
    Code:
    #include <stdio.h>
    
    
    int main (void)
    {
    char boo[10];
    int a;
    
    printf("enter id and name\n");
    scanf(" %d", &a);
    scanf(" %s", boo);
    
    switch (a)
    {
        case 32:
            printf("got id: %d name: %s\n",a,boo);
            break;
        default:
            printf("didn't get anything\n");
            break;
    }
    return 0;
    }
    results
    Code:
    userx@slackwhere:~/bin
    $ ./testing_scanf
    enter id and name
    32 bob
    got id: 32 name: bob
    or this
    Code:
    #include <stdio.h>
    
    void incert(int d)
    {
        printf("got id: %d ",d);
    }
    int main (void)
    {
    char boo[10];
    int a;
    
    printf("enter 32 \n");
    scanf(" %d", &a);
    
    
    switch (a)
    {
        case 32:
            
            printf("enter id and name\n");
            scanf(" %d", &a);
            scanf(" %s", boo);
            incert(a);
            break;
        default:
            printf("didn't get anything\n");
            break;
    }
    printf("name: %s\n",boo);
    return 0;
    }
    results
    Code:
    userx@slackwhere:~/bin
    $ ./testing_scanf
    enter 32 
    32
    enter id and name
    55 bob
    got id: 55 name: bob
    of does it have to be a c and is that not just one char (letter)?
    Last edited by userxbw; 11-19-2017 at 08:37 PM.

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    It's characters that you need to watch out for. %c is different from most other specifiers because white space is not ignored. These calls have different results:
    Code:
    int foo;
    char bar;
    
    sscanf("123\tM", "%d%c", &foo, &bar);
    
    sscanf("123\tM", "%d %c", &foo, &bar);
    The first sscanf will give bar a tab, but the second will give bar the M. The difference in the format string is what makes the behavior different, so it depends on what you want.

    %s already consumes white space before it reads a word, and stops reading when it encounters white space.

  6. #6
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by whiteflags View Post
    It's characters that you need to watch out for. %c is different from most other specifiers because white space is not ignored. These calls have different results:
    Code:
    int foo;
    char bar;
    
    sscanf("123\tM", "%d%c", &foo, &bar);
    
    sscanf("123\tM", "%d %c", &foo, &bar);
    The first sscanf will give bar a tab, but the second will give bar the M. The difference in the format string is what makes the behavior different, so it depends on what you want.

    %s already consumes white space before it reads a word, and stops reading when it encounters white space.
    Ok I see what you're talking about, I've been staying away from scanf because of loops and getting another scanf to stick in the loop without having to resort to getchar() to stop the loop which requires an enter to get past it then scanf to get next needed data. so I've been favoring fgets and fgetc

    though
    Code:
    scanf("%s", something);
    scanf("%d", &somethingelse);
    works too in order to get two separate data info off the same line. but he not got his tie in to see exactly what is really going on.
    Last edited by userxbw; 11-19-2017 at 08:45 PM.

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Yeah, if it sounded like I was saying that calling scanf a particular way would make it so you can type ahead, that's not what I meant. You can technically type ahead at any time with any call to an input function (like scanf), assuming that the programmers didn't counter that move. If a programmer decides that a user thinking that they know what prompts are coming is a bad idea and they don't want to let people type ahead there are ways to stop it. I don't want to get off topic though, since I don't think it matters much here. The OP seems to like/want that behavior.

  8. #8
    Registered User GeorgeIO's Avatar
    Join Date
    Nov 2017
    Posts
    33
    Thanks very much for the help guys. I didn't realise it was something so simple. Greatly appreciated!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing function as argument
    By NeMewSys in forum C Programming
    Replies: 16
    Last Post: 06-11-2008, 02:03 PM
  2. Replies: 5
    Last Post: 02-26-2008, 01:29 PM
  3. Passing function as an argument.
    By hyaku_ in forum C Programming
    Replies: 1
    Last Post: 12-11-2005, 04:43 AM
  4. passing a function as an argument
    By angelscars in forum C++ Programming
    Replies: 6
    Last Post: 12-06-2005, 09:53 PM
  5. Passing a function as an argument
    By Xzyx987X in forum C Programming
    Replies: 10
    Last Post: 04-20-2004, 10:32 PM

Tags for this Thread