Time's Tables Quiz (Error)

50 Views Asked by At

HELP NEEDED :D

So trying to embark on making a time's tables quiz for my brother.Very new to coding so trying to do it as simple as possible but really stuck. Any help would be awesome Basically I keep getting this error

[ In function 'main': Line 17: error: expected expression before 'else']

#include <stdio.h>

int main (){

int answers_eight[] = {8,16,24,32,40,48,56,64,72,80,88,96};
int answer ;

printf ("8x1 = : ");
scanf ("%d",&answer);

If (answer == '8');

{
    printf ("Correct");
}

else 
 {
    printf ("Incorrect");
}

return 0;
}
3

There are 3 best solutions below

0
On BEST ANSWER

Your code has various syntax errors (If instead of if, semicolon after if-condition). Additionally, your code has a logical problem where you read an int and then compare against a string. This version works and is properly indented:

    #include <stdio.h>

int main (){

    int answers_eight[] = {8,16,24,32,40,48,56,64,72,80,88,96};
    int answer ;

    printf ("8x1 = : ");
    scanf ("%d",&answer);

    if (answer == 8) {
        printf ("Correct");
    } else {
        printf ("Incorrect");
    }

    return 0;
}
0
On

Its syntax error remove semicolon after if statement. If (answer == '8'); Also answer compare as int not as Char.

if(answer==8)
{
printf("Correct");
}
else{
printf("Incorrect");
}
0
On

Working version is here:

#include <stdio.h>

int main (){

    int answers_eight[] = {8,16,24,32,40,48,56,64,72,80,88,96};
    int answer ;

    printf ("8x1 = : ");
    scanf ("%d",&answer);

    if (answer == 8) {
            printf ("Correct");
    } else {
                 printf ("Incorrect");
         }

    return 0;
}

What I have changed?

1) I in if Statement was capitalized. It shouldn't be

2) You are reading number in scanf but in if statement, you were comparing character, not number.