0
#include <stdio.h>
#include <ctype.h>
#include <string.h>
    
int main() {
    char questions[][100] = {"1. what year did the c language debut?:",
                             "2. who is credited with crating c?:",
                             "3. what is the predecossor of c?:"};
    
    char options[][100] = {"a. 1969","b. 1972","c. 1975","d. 1999",
                           "a. dennis r.","b. nikola tesla","c. john carmarck","d. doc b.",
                           "a. objective c","b. b","c. c++","d. c#"};
    
    char answers[3] = {'b','a','b'};
    
    int numberofquestions = sizeof(questions) / sizeof(questions[0]);
    char space[] = "                         ";
    char guess[numberofquestions];
    char results[3][7];
    int score = 0;
    
    for(int i = 0; i < numberofquestions * 4; i++){
        strncat(options[i], space, 25 - strlen(options[i]));
    }
    for(int i = 0; i < numberofquestions; i++){
        printf("\n%s\n", questions[i]);
    
        for(int j = 4 * i; j < 4 * (i + 1); j++){
            printf("%s", options[j]);
        }
    
        printf("\n\nguess: ");
        scanf("%c", &guess[i]);
        scanf("%*c"); // this is for clearing \n from input buffer, * for ??
        
        if(guess[i] == answers[i]){
            score++;
        }
    }
    
    for(int i = 0; i < numberofquestions; i++){
        if(guess[i] == answers[i]){
            strcpy(results[i], "correct");
        }
        else{
            strcpy(results[i], "wrong");
        }
    }
    printf("\n1st question is %s\n", results[0]);
    printf("2nd question is %s\n", results[1]);
    printf("3rd question is %s\n", results[2]);
    
    printf("\ntotal score is %d/%d", score, numberofquestions);
    return 0;

}

W hen I run the code with the correct answers 'b', 'a', and 'b' i get the output

1st question is correctcorrectcorrect
2nd question is correctcorrect
3rd question is correct

total score is 3/3

but if I run with the wrong answers I get the output

1st question is wrong
2nd question is wrong
3rd question is wrong

total score is 0/3

How can I fix this ?

Michael M.
  • 10,486
  • 9
  • 18
  • 34
Enes
  • 1
  • Does this answer your question? [What's the rationale for null terminated strings?](https://stackoverflow.com/questions/4418708/whats-the-rationale-for-null-terminated-strings) – Karl Knechtel Feb 20 '23 at 15:29
  • Welcome to Stack Overflow. In your own words, where the code says `char results[3][7];`, how did you decide on those sizes? – Karl Knechtel Feb 20 '23 at 15:29

1 Answers1

1

The short answer is that your results buffer char results[3][7] isn't big enough.

C strings are NULL terminated which means that each string literal needs an extra char to mark the end.

So, the literal "wrong" actually has length 6, and "correct" has length 8.

The entries in results are arranged consecutively in memory, so when you copy "correct" into results[i] the null terminating character spills into the space for results[i+1][0]. When results[i+1] is copied into place it overwrites that character.

You can fix this here by increasing the size of the buffers: char results[3][8].

motto
  • 2,888
  • 2
  • 2
  • 14