2017-10-10 54 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