Home > other >  Trying to make QuizMaker in C, but some steps are ignored with !X
Trying to make QuizMaker in C, but some steps are ignored with !X

Time:09-27

Basically I'm trying to make QuizMaker by asking user how many questions they want, then make a string of an array. I'm new to C, so I might be missing some little details. Please look at the screenshot of console which I included.

Screen shot of my console

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define BUFFER_SIZE 4096

int input(char *str, int n);

int main() {
    int a, i, length = 0;
    printf("How many questions do you want?\n");
    scanf("%d", &a);
    char *strarr[a]; 
    char buffer[BUFFER_SIZE]; //string holder
    
    while (getchar() != '\n'); //in case there is \n, flush it
        
    for(i = 0; i < a; i  ) {
        printf("Question number #%d:\n", i   1);
        length = input(buffer, BUFFER_SIZE);
        //input method returns number of chars we've entered
        strarr[i] = malloc((length) * sizeof(char));
        //allocating memory for each pointers to array of chars
        strcpy(strarr[i], buffer);
        //copy the string you've just created to an array of strings
    }
    //printing results
    printf("_____________\n");
    for(i = 0; i < a; i  ) {
        printf("%s\n", strarr[i]);
    }
    
    return 0;
}

int input(char *str, int n) {
    int ch, i = 0;
    while ((ch = getchar()) != '\n') {
        if (i < n)
            str[i  ] = ch;
        str[i] = '\0';
    }
    return i;
}

CodePudding user response:

There are some problems in the code:

  • the test if (i < n) is not strict enough: you should stop storing characters to the destination array before it is full to save space for the null terminator.

  • you must allocate one extra byte for the null terminator: malloc((length 1) * sizeof(char)) or just malloc(length 1) as sizeof(char) is 1 by definition.

  • you should test for EOF in addition to '\n' in the reading loops.

Here is a modified version:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define BUFFER_SIZE 4096

int input(char *str, int n);

int main() {
    int a, c, i, length = 0;
    printf("How many questions do you want?\n");
    if (scanf("%d", &a) != 1 || a <= 0) {
        fprintf(stderr, "invalid input\n");
        return 1;
    }
    char *strarr[a]; 
    char buffer[BUFFER_SIZE]; //string holder
    
    // flush the rest of the input line
    while ((c = getchar()) != EOF && c != '\n')
        continue;
        
    for (i = 0; i < a; i  ) {
        printf("Question number #%d:\n", i   1);
        //input method returns number of chars we've entered
        length = input(buffer, BUFFER_SIZE);
        //allocating memory for each pointers to array of chars
        strarr[i] = malloc((length   1) * sizeof(char));
        if (strarr[i] == NULL) {
            fprintf(stderr, "allocation error\n");
            a = i;
            break;
        }
        //copy the string you've just created to an array of strings
        strcpy(strarr[i], buffer);
    }
    //printing results
    printf("_____________\n");
    for (i = 0; i < a; i  ) {
        printf("%s\n", strarr[i]);
    }
    return 0;
}

int input(char *str, int n) {
    int ch, i = 0;
    while ((ch = getchar()) != EOF && ch != '\n') {
        if (i   1 < n)
            str[i  ] = ch;
    }
    if (i < n)
        str[i] = '\0';
    return i;
}

Also note that you can use strdup() to allocate and copy the string in a single call:

    for (i = 0; i < a; i  ) {
        printf("Question number #%d:\n", i   1);
        input(buffer, BUFFER_SIZE);
        /* allocate a copy of the string */
        strarr[i] = strdup(buffer);
        if (strarr[i] == NULL) {
            fprintf(stderr, "allocation error\n");
            a = i;
            break;
        }
    }
  • Related