C error expected unqualified-id before 'if', 'else' and ' '{' token -


i writing basic program in c++. haven't programmed in c before, seem getting fundamental errors. program small quiz. here is:

#include <stdio.h> int main(void) int correct = 0  {   int q1;   printf("what 24/(6-3)-7?\n ");   scanf("%d",&q1) } {   int q2;   printf("what -3x4+16?\n ");   scanf("%d",&q12) }{   int q3;   printf("what 9²(1/27)?\n ");   scanf("%d",&q1) }  if(q1 == 1){   correct == correct + 1 } if(){ } else if  if(q2 == 4){   correct == correct + 1 } if(q3 == 3){   correct == correct + 1 } 

please bear in mind have composed of bits , pieces picked off internet. suggestions appreciated. thank you

  1. you need put braces around body of function. , shouldn't have braces around each block asks question -- makes variables q1 local block, can't use them later in function.

  2. every statement must end ;.

  3. if() totally meaningless -- have put inside (); don't see point of doing check before checking answer question 2.

  4. to update correct, have use =, not ==; latter comparison, not assignment. btw, idiomatic way write correct = correct + 1 correct++.

  5. since main() declared return int, should have return statement @ end.

  6. you had bunch of typos. instance, used &q12 , &q1 when meant &q2 , &q3 in scanf() calls.

#include <stdio.h> int main(void) {     int correct = 0;      int q1;     printf("what 24/(6-3)-7?\n ");     scanf("%d",&q1);      int q2;     printf("what -3x4+16?\n ");     scanf("%d",&q2);      int q3;     printf("what 9^(1/2)?\n ");     scanf("%d",&q3);      if(q1 == 1){         correct = correct + 1;     }     if(q2 == 4){         correct = correct + 1;     }     if(q3 == 3){         correct = correct + 1;     }      printf("you got %d answers correct!\n", correct);     return 0; } 

Comments

Popular posts from this blog

java - pagination of xlsx file to XSSFworkbook using apache POI -

Unlimited choices in BASH case statement -

apache - How do I stop my index.php being run twice for every user -