简体   繁体   中英

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. 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. Please help.:, PS: ignore the functions called but not defined, I am working on each one first'

#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. You should pass a pointer to int. Try to replace by:

scanf_s("%d", &ID);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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