zyn
❔ how to create an array to enter user for super admin
include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NUMSTR 3
#define BUFFSIZE 100
int main(void) {
char words[NUMSTR];
char buffer[BUFFSIZE];
size_t i, count = 0, slen;
for (i = 0; i < NUMSTR; i++) {
printf("Enter Username: ");
if (fgets(buffer, BUFFSIZE, stdin) == NULL) {
fprintf(stderr, "Error reading string into buffer.\n");
exit(EXIT_FAILURE);
}
slen = strlen(buffer);
if (slen > 0) {
if (buffer[slen-1] == '\n') {
buffer[slen-1] = '\0';
} else {
printf("Exceeded buffer length of %d.\n", BUFFSIZE);
exit(EXIT_FAILURE);
}
}
if (!buffer) {
printf("Incorrect User.\n");
exit(EXIT_FAILURE);
}
words[count] = malloc(strlen(buffer)+1);
if (!words[count]) {
printf("Cannot allocate memory for string.\n");
exit(EXIT_FAILURE);
}
strcpy(words[count], buffer);
count++;
}
printf("\nYour strings:\n"); for (i = 0; i < count; i++) { printf("words[%zu] = %s\n", i, words[i]); free(words[i]); words[i] = NULL; } return 0; }
printf("\nYour strings:\n"); for (i = 0; i < count; i++) { printf("words[%zu] = %s\n", i, words[i]); free(words[i]); words[i] = NULL; } return 0; }
42 replies