读取用户输入“string”和“int”在一行中用空格分隔(C)

时间:2018-03-15 18:25:58

标签: c user-input

我有一些函数,如push,pop,delete等,用于单链表,并实现了以下函数来获取用户输入:

void user_input(){
char input[10];
while(fgets(input, 9, stdin)){
    if(strncmp(input, "add", 3) == 0){
        int x;
        printf("Number to add: ");
        scanf("%d", &x);
        push(x);
        printf("%d added.\n", x);
    }
    else if(strncmp(input, "del", 3) == 0){
        int x;
        printf("Number to delete: ");
        scanf("%d", &x);
        delete(x);
        printf("%d deleted.\n", x);
    }
    else if(strncmp(input, "q", 1) == 0){
        printf("Program terminated.\n");
        break;
    }
    // and some other if else statements...
}

所以我可以输入一个像“add”这样的字符串,然后strncmp会比较它,我得到另一个提示,要求我输入我要添加的数字并使用scanf存储在x中。像这样:

add
Number to add: 5
5 added.

但是我正在寻找一种能够输入这样的东西的方法:

add 5
del 2
etc.

基本上是一行中的字符串和int值,用空格分隔,而不是先写“添加”,按下输入并写入数字。我尝试过使用sscanf,但还没有运气。

3 个答案:

答案 0 :(得分:2)

您可以使用scanf()之类的

char str[10];
int c;
if( scanf("%9s %d", str, &c)!=2 )
{
    perror("Something went wrong.");
}

%s的宽度比str的宽度小1。额外字符适用于\0

scanf()会返回它所做的成功作业的数量,在这种情况下应为2

现在,如果输入

之类的输入
del 2

str"del"c2

答案 1 :(得分:2)

如果您使用fgets然后使用sscanf,则更容易跨过输入错误。您只需读取另一个输入字符串,而不是在输入无效数据时解除阻塞输入。

#include <stdio.h>
#include <string.h>

void push(int x) {
}

void delete(int x) {
}

void triple(int x, int y, int z) {
}

int main(void) 
{
    char input[100];
    char oper[20];
    int x, y, z;        // romantic expansion for more than one argument
    int res;
    int err;

    while(1) {
        err = 0;
        if(fgets(input, sizeof input, stdin) != NULL) {
            res = sscanf(input, "%19s%d%d%d", oper, &x, &y, &z);

            if(res == 2 && strcmp(oper, "add") == 0){
                push(x);
                printf("%d added.\n", x);
            }
            else if(res == 2 && strcmp(oper, "del") == 0) {
                delete(x);
                printf("%d deleted.\n", x);
            }
            else if(res == 4 && strcmp(oper, "trip") == 0) {  // fantasy
                    triple(x, y, z);
                printf("%d %d %d tripled.\n", x, y, z);
            }
            else if(res == 1 && strcmp(oper, "q") == 0){
                printf("Program terminated.\n");
                break;
            }
            // some other if else statements...

            else {
                err = 1;
            }
        }

        else {
            err = 1;
        }

        if(err) {
            printf("Bad operation.\n");
        }
    }
    return 0;
}

答案 2 :(得分:1)

正在尝试做的一个好策略是:

  1. 逐行阅读输入。
  2. 处理每一行。
  3. 继续,直到没有输入或用户选择退出。
  4. 这是核心大纲。

    // Make LINE_SIZE big enough for your needs
    #define LINE_SIZE 100
    
    void processLine(char line[]);    
    
    int main()
    {
       char line[LINE_SIZE];
       while ( fgets(line, LINE_SIZE, stdin) != NULL )
       {
          processLine(line);
       }
    }
    
    
    void processLine(char line[])
    {
    }
    

    processLine中,你的第一份工作就是拉出命令。根据命令做必要的事情。

    void processLine(char line[])
    {
        char command[20];
        int num = 0;
    
        // Read the command and gather the number of characters read
        // This allows you to read more data from (line + num)
        int n = sscanf(line, "%19s%n", command, &num);
        if ( n != 2 )
        {
           // Problem
           exit(0);
        }
    
        // The command is to quit, exit.
        if ( isQuitCommand(command) )
        {
           exit(0);
        }
    
        char* commandData = line + num;
        if ( isAddCommand(command) )
        {
           processAdd(commandData);
        }
        else if ( isDeleteCommand(command) )
        {
           processDelete(commandData);
        }
        else { ... }
    }
    

    这是程序的一个版本,其中存根用于几个函数。

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    // Make LINE_SIZE big enough for your needs
    #define LINE_SIZE 100
    
    void processLine(char line[]);    
    
    int isQuitCommand(char command[]);
    
    int isAddCommand(char command[]);
    
    int isDeleteCommand(char command[]);
    
    void processAdd(char commandData[]);
    
    void processDelete(char commandData[]);
    
    
    int main()
    {
       char line[LINE_SIZE];
       while ( fgets(line, LINE_SIZE, stdin) != NULL )
       {
          processLine(line);
       }
    }
    
    
    void processLine(char line[])
    {
       char command[20];
       int num = 0;
    
       // Read the command and gather the number of characters read
       // This allows you to read more data from (line + num)
       int n = sscanf(line, "%19s%n", command, &num);
       if ( n != 2 )
       {
          // Problem
          exit(0);
       }
    
       // The command is to quit, exit.
       if ( isQuitCommand(command) )
       {
          exit(0);
       }
    
       char* commandData = line + num;
       if ( isAddCommand(command) )
       {
          processAdd(commandData);
       }
       else if ( isDeleteCommand(command) )
       {
          processDelete(commandData);
       }
       else
       {
          // ...
       }
    }
    
    int isQuitCommand(char command[])
    {
       return (command[0] == 'q');
    }
    
    int isAddCommand(char command[])
    {
       return (strcmp(command, "add") == 0);
    }
    
    int isDeleteCommand(char command[])
    {
       return (strcmp(command, "del") == 0);
    }
    
    void processAdd(char commandData[])
    {
       // Add code to process commandData
    }
    
    void processDelete(char commandData[])
    {
       // Add code to process commandData
    }