C newbie cannot find syntax error

I am having trouble finding an error in the following code:

#include <stdio.h> #define LOOP 0 #define ENDLOOP 1 main() { int c, loop; loop = LOOP; while ((c = getchar()) loop != ENDLOOP) { if (c == 'e'|| c == 'E') { printf ("END LOOP\n"); loop = ENDLOOP; } else if (c == 'c' || c == 'C') printf ("Cheese\n"); else printf ("Not Cheese\n"); } } 

The terminal gives me this error:

 1-1.c: In function 'main': 1-1.c:8: error: syntax error before 'loop' 1-1.c: At top level: 1-1.c:13: error: syntax error before 'else' 
+4
source share
5 answers

You have a problem:

 ((c = getchar()) loop != ENDLOOP) 

Must be:

 ((c = getchar()) && loop != ENDLOOP) 

I would recommend writing it in a completely different way:

 #include <stdio.h> int main() { int c; while (c = getchar()) { if (c == 'e' || c == 'E') { printf ("END LOOP\n"); break; } if (c == 'c' || c == 'C') { printf ("Cheese\n"); } else { printf ("Not Cheese\n"); } } return 0; } 

I think this method is less likely to make mistakes. You may also consider using a tolower .

+6
source

Perhaps you are missing an operator?

 while ((c = getchar()) && loop != ENDLOOP) { 
+4
source

At least one mistake is that the operator is missing here:

  while ((c = getchar()) loop != ENDLOOP) 

I assume that you mean "And," and therefore it should be:

  while ((c = getchar()) && loop != ENDLOOP) 
+3
source

You can get rid of the ugly conditional expression loop != ENDLOOP and simplify your program in the process.

 #include <stdio.h> int main() { int c; while (EOF != (c = getchar())) { if (c == 'e'|| c == 'E') { printf ("END LOOP\n"); break; } else if (c == 'c' || c == 'C') printf ("Cheese\n"); else printf ("Not Cheese\n"); } return 0; } 

The unequal comparison of EOF makes it clear how getchar() can complete the while loop. Otherwise, break does if 'e' or 'E' is taken from stdin.

Int is before the main one, and return 0 is to clear ANSI C, so it's mostly stylistic, but good style.

+2
source
 #include<stdio.h> #include<conio.h> int x,y,z; int large(); int main() { printf("Enter the number"); scanf("%d%d%d",&x,&y,&z); large(); return 1; } int large() { printf("large is %d\n",x); } { else if (y>x;&&y>z) { printf("%d the larger\n",y); } else { printf("%d larger is",x); } } 
-2
source

Source: https://habr.com/ru/post/1301883/


All Articles