Anonymous asked in Computers & InternetProgramming & Design · 10 months ago

c - adding valid user input into character array?

So I'm trying to add an array called char usersAnswers[10] which holds up to 10 answers as there are only 10 rounds in my game. My struggle is finding a way to implement every valid answer the user gives into that array. I was thinking of adding a for loop below the second if statement and doing something like usersAnswers[i] = userInput but I wasn't sure. Any help would be appreciated.

My part of code:

if(numberofCharacters == 3){


numberofAnswers += 1;


printf("Please try again. \n");



printf("Only enter 3 characters. \n");


What I planned to do was:


numberofAnswers += 1;

for(int i = 0; i < 10; i++){

usersAnswers[i] = userInput;


but I thought that might be a problem because there's no condition below the for loop. I thought it'd make every element in the array the same answer at once cause there's no other if statement or condition stopping that for loop?

2 Answers

  • 10 months ago
    Favorite Answer

    I don't see enough description of the problem or code to figure out exactly what you're trying to do, but I do see something that raises a big red flag. You talk about storing 10 answers in a char[10] array, which leaves just 1 char per answer; but the code that seems to be validating a single answer is expecting 3 characters.

    One thing I suggest is to make sure you're enabling all warnings that your C compiler supports. For GNU gcc and compatibles like clang, that's -Wall, and I also throw in -Wextra. For Visual C++ used to compile C, that's /W4. Then recompile and don't consider the coding done everything compiles without triggering a warning.

    The C language allows all sorts of irrational code to compile "normally"; and though most compilers have options to detect probable errors (like using = instead of == in an if condition or assigning a pointer to an int or char variable) those options are turned off by default.

    Meanwhile, add details. What is the declaration of userInput and how did it get a value? What is the meaning of numberOfCharacters? Most important, what are you planning to do with those answers?

    • Commenter avatarLogin to reply the answers
  • Anonymous
    10 months ago

    try this...

    const char answers[] = {'a','b','c'};

    const size_t size = sizeof(answers) / sizeof(answers[0]);

    char user_answers[] = {'b', 'a', 'c'};

    int score = 0;

    for (int i = 0; i < size; i++) {

    if (answers[i] == user_answers[i]) {




    printf("You answered %d questions correctly", score);

    • Commenter avatarLogin to reply the answers
Still have questions? Get your answers by asking now.