This program is used to get names and numbers of N person, print out the numbers of a person. Please help me improve this code.
Additional Question:
To avoid a very long name that exceeds char's size, I limit it to 255. Is there a better way?
To avoid buffer, I included a space before % sign when using scanf and not use getchar(). Is there a better way?
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
// Limiting string input
const unsigned int STRINGCONSTRAINT = 255;
// Person struct to store name and numbers
typedef struct {
char name[STRINGCONSTRAINT];
char numbers[STRINGCONSTRAINT];
} person;
void getNameNumber(person people[], int size);
void printNumber(person people[], int size, char personFind[]);
// Asked for array's size and a person's name to find
int main(void) {
unsigned int size = 0;
printf("Enter the amount of people: ");
scanf(" %d", &size);
person people[size];
getNameNumber(people, size);
char personFind[STRINGCONSTRAINT];
puts("Who do you want to find?");
printf("Person to find: ");
scanf(" %[^\n]s", personFind);
printNumber(people, size, personFind);
return 0;
}
// Get name and numbers for the array
void getNameNumber(person people[], int size) {
unsigned int i;
for (i = 0; i < size; i++) {
printf("Name: ");
scanf(" %[^\n]s", people[i].name);
printf("Numbers: ");
scanf(" %[^\n]s", people[i].numbers);
}
}
// Find the person and print the numbers
void printNumber(person people[], int size, char personFind[]) {
unsigned int i;
int flag = 1;
for (i = 0; i < size; i++) {
flag = strcmp(people[i].name, personFind);
if (flag == 0) {
puts("Found!");
printf("You can call %s at %s.\n", personFind, people[i].numbers);
}
}
}