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
you need put braces around body of function. , shouldn't have braces around each block asks question -- makes variables
q1local block, can't use them later in function.every statement must end
;.if()totally meaningless -- have put inside(); don't see point of doing check before checking answer question 2.to update
correct, have use=, not==; latter comparison, not assignment. btw, idiomatic way writecorrect = correct + 1correct++.since
main()declared returnint, should havereturnstatement @ end.you had bunch of typos. instance, used
&q12,&q1when meant&q2,&q3inscanf()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
Post a Comment