简体   繁体   English

无效参数被传递给 C 中的 function

[英]Invalid parameter was passed to function in C

'the lines in bold, in the first scanf of case 3 in switch, I get a breakpoint error saying the following: Invalid parameter was passed to a function that considers invalid parameter fatal. '粗体行,在 switch 中案例 3 的第一个 scanf 中,我收到一个断点错误,内容如下:无效参数已传递给认为无效参数致命的 function。 I am trying to define a function that asks the user for the ID of a city then goes through an array until it finds that ID and prints its number of cases.我正在尝试定义一个 function ,它询问用户一个城市的 ID,然后遍历一个数组,直到找到该 ID 并打印其案例数。 Please help.:, PS: ignore the functions called but not defined, I am working on each one first'请帮助.:, PS:忽略调用但未定义的函数,我正在处理每个函数

#include<stdio.h>
void menu(void);
void filling_arrays(int*, int*, int, int);
int search_cases(int, int*, int*, int);

int main() {
  int cities_id[40], n_cases[40], n, choice, x = 0, ID=0;

  do {
      menu();
      scanf_s("%d", &choice);

      switch (choice) {

      case 1:printf("\nPlease input the number of cities: ");
          scanf_s("%d", &n);
          filling_arrays(cities_id, n_cases, n, x);
          x += n;
          break;
    **case 3:printf("Input the city ID that you would like to know its number of cases: ");
          scanf_s("%d", ID);
          while (search_cases(ID, cities_id, n_cases, x) == -1) {
              printf("Wrong ID!\n");
              printf("Please try again!");
              scanf_s("%d", ID);
          }
          printf("The city with the ID %d has %d cases.",ID, search_cases(ID, cities_id, n_cases, x));
          break;**
      default: printf("Invalid Choice :/\n");
      }
  } while (choice != 5);
  return 0;
}

void menu(void) {
  printf("______________________________________________________\n");
  printf("|________________________Menu_________________________|\n");
  printf("|1. Fill Arrays                                       |\n");
  printf("|2. Display Arrays Content                            |\n");
  printf("|3. Search for the number of cases in a city          |\n");
  printf("|4. Sort the Number of Cases                          |\n");
  printf("|5. Quit                                              |\n");
  printf("|_____________________________________________________|\n");

  printf("\nPlease Make Your Choice: ");
}

void filling_arrays(int* cities_id, int* n_cases, int n, int x) {
  for (int i = 0; i < n; i++) {
      printf("Input a city ID followed with the number of covid19 cases: ");
      scanf_s("%d %d", &cities_id[i + x], &n_cases[i + x]);
  }
}


**int search_cases(int ID, int* cities_id, int* n_cases, int x) {
  int i;
  for (i = 0; i < x; i++) {
      if (cities_id[i] == ID)
          return n_cases[i];
      else
          return -1;
  }
}**

In scanf_s, you pass ID which is an int.在 scanf_s 中,您传递一个 int 的 ID。 You should pass a pointer to int.您应该传递一个指向 int 的指针。 Try to replace by:尝试替换为:

scanf_s("%d", &ID);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM