代码之家  ›  专栏  ›  技术社区  ›  chris

自定义获取行输入函数

  •  2
  • chris  · 技术社区  · 17 年前

    下面的问题是什么?

    void getInput(int* output) {
       int c, i;
       for(i=0; (c = getchar()) != '\n'; i++)
         output[i] = c; // printf("%c", c) prints the c value as expected
       output[++i] = '\0';
    }
    

    当我运行程序时,它永远不会退出循环,我必须这样做 printf("%c", c); ,在按enter键并创建新行后,它会很好地打印出所有输入。

    7 回复  |  直到 11 年前
        1
  •  7
  •   Community Mohan Dere    9 年前

    下面的问题是什么?

    1. void getInput(int* output) {
    

    可能

    void getInput(char* output) {
    

    这样更好。

    另外,您如何知道输出指针指向您拥有足够内存以写入用户输入的某个位置?也许您必须将最大缓冲区长度作为一个额外参数,以避免缓冲区溢出错误 PW pointed out .

    5.   output[++i] = '\0';
    

    我已经在for循环中增加了一个额外的时间,因此您可以执行以下操作:

    output[i] = '\0';
    

    除此之外,程序运行良好,输出我们输入的内容,直到返回。

    FWIW,我这样称呼它来测试它:

     int main(void)
    {
        char o[100];
        getInput(o);
        printf("%s", o);
        return 0;
    }
    
        2
  •  1
  •   jjnguy Julien Chastang    17 年前

    请确保“\n”实际上正在将其转换为c。

    有时“\n”将作为分隔符丢弃。

        3
  •  1
  •   Raz    17 年前

    您最近发布的代码有3个错误,我可以看到:

    char* userInput[MAX_INPUT_SIZE];
    

    应该是:

    char userInput[MAX_INPUT_SIZE+1];
    

    (这已经被暗黑破坏神Pax提到了)

    getInput(&userInput);
    

    应该是:

    getInput( userInput );
    

        4
  •  0
  •   PW.    17 年前

    一种避免缓冲区溢出的简单方法,因为输出的大小从未经过传递/检查

        5
  •  0
  •   1800 INFORMATION    17 年前

        6
  •  0
  •   chris    17 年前

    这是一个完整的程序,从您的输入中进行了几次更新,但它仍然无法脱离循环。顺便说一句,这是第34页的练习1-24

    #include <stdio.h>
    
    #define STACK_SIZE 50
    #define MAX_INPUT_SIZE 1000
    #define FALSE 0
    #define TRUE 1
    
    void getInput();
    int validInput();
    
    int main() {
      char* userInput[MAX_INPUT_SIZE];
    
      getInput(&userInput);
    
      if (validInput(&userInput) == TRUE)
        printf("Compile complete");
      else
        printf("Error");
    }
    
    // Functions
    void getInput(char* output) {
      int c, i;
      for(i=0; (c = getchar()) != '\n' && c != EOF && i <= MAX_INPUT_SIZE; i++)
        output[i] = c;
      output[i] = '\0';
    }
    
    int validInput(char* input) {
      char stack[STACK_SIZE];
      int c;
      int j;
    
      for (j=0; (c = input[j]) != '\0'; ) {
        switch(c){
          case '[': case '(': case '{':
            stack[j++] = c;
            break;
          case ']': case ')': case '}':
            if (c == ']' && stack[j] != '[')
              return FALSE;
            else if (c == '}' && stack[j] != '{')
              return FALSE;
            else if (c == ')' && stack[j] != '(')
              return FALSE;
    
            // decrement the stack's index  
            --j;
            break;
        }
      }
    
      return TRUE;
    }
    
        7
  •  0
  •   chris    17 年前

    #include <stdio.h>
    
    #define STACK_SIZE 50
    #define MAX_INPUT_SIZE 1000
    #define FALSE 0
    #define TRUE !FALSE
    
    void get_input();
    int valid_input();
    
    int main() {
      char user_input[MAX_INPUT_SIZE + 1]; // +1 for the \0
    
      get_input(user_input);
    
      if (valid_input(user_input))
        printf("Success\n");
      else
        printf("Error\n");
    }
    
    // Functions
    void get_input(char* output) {
      int c, i;
      for(i=0; (c = getchar()) != '\n' && c != EOF && i <= MAX_INPUT_SIZE; i++)
        output[i] = c;
      output[i] = '\0';
    }
    
    int valid_input(char* input) {
      char stack[STACK_SIZE];
      char c;
      int i = 0;
      int stack_index = -1;
    
      while ((c = input[i]) != '\0' && i < STACK_SIZE) {
        switch(c){
          case '[': case '(': case '{':
            stack_index++; 
            stack[stack_index] = c;
            break;
          case ']': case ')': case '}':
            if ((c == ']' && stack[stack_index] != '[') ||
                (c == '}' && stack[stack_index] != '{') ||
                (c == ')' && stack[stack_index] != '('))
              return FALSE;
    
            // decrement the stack's index now that the closing bracket is found  
            stack_index--;
            break;
        }
        i++;
      }
    
      // stack index should be back where it started
      return (stack_index == -1);
    }