检查功能不起作用

时间:2017-10-10 00:04:41

标签: c

我正试图找出我的checkQuestion函数出了什么问题。我不知道为什么,但有时它不会检查某些问题或完全跳过它们。我创建了一个函数createQuestion,我在checkQuestion中调用了createQuestion,所以我不知道是什么导致一切都搞砸了。任何帮助将不胜感激,我只需要一些关于错误的线索。如果你想运行它,我发布了该程序的链接。

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;
  }
}

2 个答案:

答案 0 :(得分:1)

每次拨打operations时,您都会得到不同的结果。您需要在条件中与d进行比较,而不是与operator进行比较。您在createQuestion中遇到了类似的问题,其中传递给checkQuestion的内容可能不是向用户显示的内容。

例如:

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

答案 1 :(得分:0)

一个奇怪的是,你有一个返回char的函数:

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

然后你把它放在一个期望浮动的函数中:

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