The code:
#include <stdio.h>
#include <stdbool.h>
typedef enum{
counting1,
counting2,
searching,
possiblecom,
possibleend,
} read_state;
bool issep(char a){
return (a == ' ' || a == '\n' || a == '\t');
}
read_state state = searching;
int count = 0;
int c = getchar();
bool wasword = false;
while (c != -1)
{
if(state == searching)
{
if(c == '{'){state = counting1;}
else if(c == '('){state = possiblecom;}
}else
{
if(state == counting2 && c == '*'){
c = getchar();
if(c == ')'){
state = searching;
if(wasword = true){
count++;
}
}else{
if (issep(c)){
if (wasword == true){count++;}
wasword = false;
}else{wasword = true;}
}
}
else if(state = counting1 && c == '}'){state = searching;}
else if (state == counting1 || state == counting2){
if (issep(c) == false){
wasword = true;
}
else{
if(wasword == true){count++;}
wasword = false;
}
}
}
c = getchar();
if(state = possiblecom){
if(c == '*'){
state = counting2;
c = getchar();
wasword = false;
}else{state = searching;}
}
}
printf("%d", count);
These are errors:
1: comcount.c:24:9: error: initializer element is not constant
24 | int c = getchar();
| ^~~~~~~
2: comcount.c:28:1: error: expected identifier or ‘(’ before ‘while’
28 | while (c != -1)
| ^~~~~
3:comcount.c:84:8: error: expected declaration specifiers or ‘...’ before string constant
84 | printf("%d", count);
| ^~~~
comcount.c:84:14: error: expected declaration specifiers or ‘...’ before ‘count’
84 | printf("%d", count);
| ^~~~~
There are several problems:
I tried replacing int type with char. I and wrote something like: int c = getchar(); \n n = c;.
In normal situation int c = getchar(); works but in this code something breaks it and I don't know what.
After noting the good comment about the apparent missing encapsulation of the code within a "main" function, I copied your code, added the missing statements to have the pertinent portion of the program logic within a "main" function, and then compiled the code. This led to the first bit of warnings from the compiler that needed to be addressed.
Those warnings were in regard to where an apparent test was to be performed for equality (==), but only one equal sign was in place.
So those were refactored and corrected per the compiler warnings/suggestions.
Following is a refactored version of the code.
Utilizing some assumptions that some type of word count was taking place, following is the test output at the terminal where the entry of text took place.
FYI, I was testing on a Linux system and could not get the "Ctrl-Z" end-of-file functionality to work, so I substituted an unlikely character, "^", to simulate the end of text entry.
The one thing that I noticed was that the word count appeared to be off by one (printed a count of 15 words instead of 16 words if you tally up the phrase). So, this program probably needs a bit more polish.
The main take-away from this exercise probably is to familiarize yourself more with the errors and warnings that are produced by your compiler.