1

I want to ask the user to input another name, if the name is already in the array list. How do I do that?

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX_SIZE 12
#define MAX_LENGTH 25

char waitList[MAX_SIZE][MAX_LENGTH];
char numberInParty[MAX_SIZE];
int count = 0;

void insertList() {
  char name[MAX_LENGTH];
  int number, i;
  printf("Name? ");
  scanf("%s", name);
  printf("Number of people in party? ");
  scanf("%d", &number);

  strcpy(waitList[count], name); 
  numberInParty[count] = number;
  printf("Inserted\n\n");
  count++; 

1 Answers1

0

You want to iterate over the array and check if each element in it - is name the user just entered.

Therefore, you need to know the size of the array. There is no function for checking the length of an array in C. However, if the array is declared in the same scope as where you want to check, you can do the following

int len = sizeof(waitList)/sizeof(waitList[0]);

Then, use a for loop and strcmp:

for ( int i = 0; i < len ; i++ ) {
    if ( strcmp(waitList[i], name) == 0 ) {
        printf("Name already exists. Please enter another ");
        scanf("%s", name);
    }
}

You can read more about strcmp here.