I'm new to C and I'm building a quiz game, every time you answer right your score gets higher, however there is a bug that i was not able to find which is even if you answer correctly the code will tell you that you are wrong and moves to the next question in the end you will always get a score of 0/3. my question is what is the reason that is preventing my code from determining that the answer is right, why does it always go to the else statement which is wrong.
this is the code
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main()
{
char questions[][100] = {
"\n1. what company was the first to create a foldable phone? ",
"\n2. what company was the first to create a foldable laptop? ",
"\n3. what company was the first to create a full electric car? "
};
char options[][100] = {
"\nA. apple, B. oppo, C. samsung, D. motorolla",
"\nA. Asus, B. MSI, C. Microsoft, D. Gygabyte",
"\nA. mazda, B. chevrollet, C. toyota, D. Tesla"
};
char answers[] = {'C', 'A', 'D'};
int score = 0;
char ans;
for (int i = 0; i < sizeof(questions) / sizeof(questions[0]); i++)
{
printf("%s", questions[i]);
printf("%s", options[i]);
printf("\n\nwhat is your answer? ");
scanf("%c", &ans);
scanf("%c");
ans = toupper(ans);
if (ans == answers[i])
{
score += 1;
printf("\nthat's right!");
printf("\nnext question\n");
}
else
{
printf("\nwrong!");
printf("\nnext question\n");
}
}
printf("\n\nthe correct answers are: ");
for (int i = 0; i < sizeof(questions) / sizeof(questions[0]); i++)
{
printf("%c ", answers[i]);
}
printf("\nyour score is: %d/3", score);
}
ans is the user guess. if anyone have the solution for this i would really appreciate it. thanks in advance.