代码之家  ›  专栏  ›  技术社区  ›  Noel Sebastian Lim Jenq Ning

使用atoi时无法将char转换为int

  •  -1
  • Noel Sebastian Lim Jenq Ning  · 技术社区  · 9 年前

    我正在创建函数 findBookID 。当我尝试将图书id转换为整数时,它显示了一个错误。
    谁能给我一个解决办法吗?当做
    错误最后出现在文章中。

    strncpy(bookId, source + pos_text + 1, len_text - pos_text);
    int i = atoi(bookId);       //atoi converts the string to integer
    if (id == i)
    {
      return true;
    }
    

    下面是我如何声明函数

    bool findBookId(char source[], int id)
    {
        char input[10] = "Book ID: ";
        char bookId[5];
        int int_id = 0;
        int pos_search = 0;
        int pos_text = 0;
        int len_search = 10;
        size_t len_text = strlen(source);
        if (len_search < len_text)
        {
    
            for (pos_text = 0; pos_text < len_search - 1; ++pos_text)
            {
                if (source[pos_text] == input[pos_search])
                {
    
                    ++pos_search;
                    if (pos_search == len_search - 1)
                    {
                        // match
    
                        strncpy(bookId, source + pos_text + 1, len_text - pos_text);
                        int i = atoi(bookId);       //atoi converts the string to integer
                        if (id == i){
                            return true;
                        }
    
    
                    }
                }
                else
                {
                    pos_text -= pos_search;
                    pos_search = 0;
                }
            }
        }
        return false;
    }
    

    完整编码供参考: https://drive.google.com/open?id=1zHc_26kFPVHs0b99-gkX1hdQ3AYykCPL9KegI5QobdY

    enter image description here

    1 回复  |  直到 9 年前
        1
  •  2
  •   deamentiaemundi    9 年前

    许多IDE的一个主要问题是缺乏清晰的警告。如果我试图用GCC编译您的代码(Google不适合编写代码,您的条目会出现C&P错误),我会得到以下错误:

    $ gcc -g3 -std=c11 -W -Wall bookid.c  -o bookid
    bookid.c: In function ‘addbook’:
    bookid.c:106:3: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[50]’ [-Wformat=]
       scanf("%s", &book.name);
       ^
    bookid.c: In function ‘editbook’:
    bookid.c:180:3: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[50]’ [-Wformat=]
       scanf("%s", &book.name);
       ^
    bookid.c: In function ‘deletebook’:
    bookid.c:270:5: warning: format ‘%c’ expects a matching ‘int’ argument [-Wformat=]
         printf("%c, c");
         ^
    bookid.c: In function ‘findBookId’:
    bookid.c:469:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
       if (len_search < len_text) {
                      ^
    bookid.c:464:7: warning: unused variable ‘int_id’ [-Wunused-variable]
       int int_id = 0;
           ^
    /tmp/ccijPXxB.o: In function `main':
    bookid.c:85: undefined reference to `search'
    collect2: error: ld returned 1 exit status
    

    对于第一个错误更改 scanf("%s", &book.name); scanf("%s", book.name);

    printf("%c, c"); 应该是 printf("%c", c);

    comparison between signed and unsigned integer 暂时可以忽略(当然应该稍后修复!)

    如果不需要变量,请将其注释掉。

    最后一个致命错误的原因是一个名为 Search() 以以下名称调用 search() .C区分大小写。

    区分大小写在破坏的搜索中也起着很大的作用:您保护了文件 booklist.txt 但想阅读 BookList.txt (这在Windows中仍然有效吗?)。