2017-10-10 52 views
0

我想弄清楚我的checkQuestion函數有什麼問題。我不知道爲什麼,但有時它不會檢查某些問題或完全跳過它們。我創建了一個函數createQuestion,我在createQuestion中調用了checkQuestion,所以我不知道是什麼原因導致一切都搞亂了。任何幫助,將不勝感激,我只需要一些線索,什麼是錯的。如果你想運行它,我發佈了該程序的鏈接。檢查功能不起作用

https://repl.it/MQsD/142

#include <stdio.h> 
#include <stdlib.h> 
#include <time.h> 

    int main(void) { 

    srand(time(NULL)); 
    printf("Math quiz. \n"); 
    printf("This is level 1. There are a total of 10 questions. \n\n"); 

    createQuestion(); 
    checkQuestion(); 

    } 

//Functions 

int integers(void) { 

    int digit; 
    digit = rand() % 10; 

    return digit; 

} 

char operations(void) { 

    int digit; 
    digit = rand() % 4; 

    if (digit == 0) { 

    return '+'; 

    } else if (digit == 1) { 

    return '-'; 

    } else if (digit == 2) { 

    return '*'; 

    } else if (digit == 3) { 

    return '/'; 
    } 

} 

int createQuestion(void) { 

    int i; 
    int count = 0; 
    int answer; 
    int sum; 

    for (i = 1; i <= 10; i++) { 

    count++; 
    printf("%d)%d", count, integers()); 
    printf("%c", operations()); 
    printf("%d", integers()); 
    printf("="); 
    scanf("%f", & answer); 
    checkQuestion(integers(), integers(), operations(), answer); 

    } 
} 

void checkQuestion(float a, float b, float c, char d) { //a integer b integer c answer //d operator 

    int answer1; 
    int answer2; 
    int answer3; 
    int answer4; 

    if (operations() == '+') { 

    answer1 == a + b; 
    answer1 == c; 
    if (answer1 == c) { 

     return messagesGood(); 

    } else { 

     return messagesBad(); 

    } 

    } else if (operations() == '-') { 

    answer2 = a - b; 

    if (answer2 == c) { 

     return messagesGood(); 

    } else { 

     return messagesBad(); 

    } 

    } else if (operations() == '*') { 

    answer3 = a * b; 

    if (answer3 == c) { 

     return messagesGood(); 

    } else { 

     return messagesBad(); 

    } 

    } else if (operations() == '/') { 

    answer4 = a * b; 

    if (answer4 == c) { 

     return messagesGood(); 

    } else { 

     return messagesBad(); 

    } 
    } 

} 

void messagesGood(void) { 

    int digit; 
    digit = rand() % 4; 

    switch (digit) { 

    case 0: 
    printf("Very good! \n"); 
    break; 

    case 1: 
    printf("Excellent! \n"); 
    break; 

    case 2: 
    printf("Nice Work! \n"); 
    break; 

    case 3: 
    printf("Keep up the good work! \n"); 
    break; 
    } 
} 

void messagesBad(void) { 

    int digit; 
    digit = rand() % 4; 

    switch (digit) { 

    case 0: 
    printf("No. Please try again. \n"); 
    break; 

    case 1: 
    printf("Wrong. Try once more. \n"); 
    break; 

    case 2: 
    printf("Don’t give up! \n"); 
    break; 

    case 3: 
    printf("No. Keep trying. \n"); 
    break; 
    } 
} 
+1

看,好試試。兩件事情。整數()函數每次都會返回一個隨機整數,對吧?所以當你在printf這個問題的時候調用它,當你調用它來傳遞一個參數到checkQuestion時,你認爲它們是兩個整數嗎?其次,在「checkQuestion」中評估'answer1'時,你會混淆使用'=='和'='。 – ncke

回答

1

每次調用operations時間,你可以得到不同的結果。您需要在條件下與d進行比較,而不是針對operator。您在createQuestion中遇到了類似的問題,傳遞給checkQuestion的內容可能不是用戶顯示的內容。

例如:

if (d == '+') { 
    // ... 
} 
+0

謝謝!我改變了它,但現在我沒有收到任何消息。試圖弄清楚發生了什麼。 – Neo

+0

是的,並且在'createQuestion()'中調用'integers()'時出現同樣的問題。 – ncke

0

一個奇怪的是,你有一個返回字符的功能:

**char** operations(void){ 
    etc... 
} 

然後你把它放在一個函數,需要一個浮動:

checkQuestion(integers(), integers(), **operations()**, answer); 
void checkQuestion(float a, float b, **float c**, char d) 
+0

謝謝!我在寫回答的時候犯了一個錯誤。 – Neo