2017-03-31 83 views
-2

這是我收到的錯誤:試圖使用的atoi和它不工作

search.c: In function ‘checkGrades’: 
    search.c:63: warning: passing argument 1 of ‘atoi’ makes pointer from 
    integer without a cast 
    /usr/include/stdlib.h:148: note: expected ‘const char *’ but argument is 
    of type ‘char’ 
    search.c:63: error: expected ‘)’ before ‘;’ token 

這裏是我的代碼:如果

int checkGrades(STUDENT grades[], int maxrecs, FILE* fp) 
    { 
    char line[LINE_MAX]; 
    int rc; 
    int count = 0; 
    int score; 
    char id; 
    while(fgets(line,LINE_MAX, fp) != NULL) 
    { 
    rc = sscanf(line, 
    "%25[^:]%*c%25[^:]%*c%6s%*c%3d%*c%3d%*c%3d%*c%3d%*c%3d%*c%2c", 
       grades[count].id, 
       &grades[count].score[1], 
       &grades[count].score[2], 
       &grades[count].score[3], 
       &grades[count].grade); 
    if((rc != 9) || 

        rc == atoi (id); 

       grades[count].id < 100000 || 
       grades[count].id > 999999 || 
       grades[count].grade < 0 || 
       grades[count].grade > 110) 
    { 


      printf("Invalid Record: %s", line); 
    } 
    else 
    { 
     count++; 
     if(count == TNARRAY_MAX)break; 
    } 
    printf("ID: ", grades-> id); 
    } 

我不是很確定RC是林應該是用atoi,我是C的初學者,並且嘗試過很多連擊,但都沒有成功。我知道我需要在比較之前將字符串轉換爲int,但是在這裏我很迷茫,關於如何做到這一點。 任何幫助將不勝感激!

+2

'atoi'的第一個參數應該是一個字符串,它是'char *'。 'id'是'char',而不是'char *'。 – Barmar

+2

你也從未設置過'id'。 – Barmar

+0

'if((rc!= 9)||''在'||'後面缺少另一個條件 – Barmar

回答

0

你不能在單個字符上使用atoi。你可以做的是將字符轉換爲整數,然後從中減去字符'0'的ASCII值。

int num; 

num = (int)id - (int)'0'; 

此外,正如在評論中指出的那樣,您實際上並沒有初始化id。

1

的功能的atoi預計單指向一個常量字符串,像這樣:(爲const char * STR)

這裏你可以看到它的用法的例子:https://www.tutorialspoint.com/c_standard_library/c_function_atoi.htm

而且我不知道你的意思是複製你的整個代碼(我假設LINE_MAX和TNARRAY_MAX是在某處定義的宏),但是因爲它有一個大括號不匹配,這意味着你已經發布它的這段代碼需要一個'}'在結束。

相關問題