比较用户输入int与结构变量;

时间:2019-05-02 12:50:28

标签: c

我不是100%理解结构,所以也许这很容易; 总而言之,我有一个给定的结构,希望将用户输入的整数值与该结构中的变量之一所保存的值进行比较。 基本上,该函数检查它们是否相等,并且是否返回true;

这是一些代码,首先是struct,然后是我尝试的check函数;

结构:

struct locations *   bombs = (struct locations *) malloc(sizeof(struct locations) * 2);

    bombs[0].x = 2;
    bombs[0].y = 2;
    bombs[0].found = false;

    bombs[1].x = 2;
    bombs[1].y = 3;
    bombs[1].found = false;

用户输入功能

void  get_user_char(int* a ) {
  scanf("%d", a);

}

输入值的提示和对函数的调用

        int chosenX = 0;
        int chosenY = 0;
        printf("Enter  X coordinate:");
        get_user_char(&chosenX);
        printf("Enter  Y coordinate:");
        get_user_char(&chosenY);

        bool found = false;
        check_found(chosenX, chosenY, bombs, size_of_grid, &found);

检查功能

void check_found(int row, int col, struct   locations bombs[], int size, bool* found) {

  for (int i = 0; i < 2; i++) {
    if (bombs[i].x = row) {
        if (bombs[i].y = col) {
            bombs[i].found == true;
            *found = true;
        }
    }
  }

}

无论输入什么值,该函数始终返回true; 任何帮助表示赞赏!

0 个答案:

没有答案
相关问题