I am reading a file character by character and displaying it on the screen.
1 2 3 4 5 6 7
char c;
int count = 0;
if (c == '{' || c == '}')
count++;
if (c=='/')
// i wamnt to write a functiion tha will read each character but not effect my count if it is a { inside my comment.
count should not be effected on the comment lines, block lines, statments '{' || '}' or strings.
So, you're trying to parse a C file? I suppose with "comment lines" you mean anything after '//' in a line or anything within /* */, and with strings you mean anything within " "? What do you mean with "statements" and "block lines" though?
I think he needs states, not variables. I mean, "additional variables" is of course the logical conclusion, but what he actually needs is the ability to set up state based behavior.
I think he needs states, not variables. I mean, "additional variables" is of course the logical conclusion, but what he actually needs is the ability to set up state based behavior.
As soon as you keep updating some variables and increase count based on their values, you already have state based behavior.
I'm not sure what you had in mind.
A bool is NOT a good idea however.
Those comments can't be recursive, so it would do fine.
What kind of solution are you thinking of?
So, you're trying to parse a C file? I suppose with "comment lines" you mean anything after '//' in a line or anything within /* */, and with strings you mean anything within " "? What do you mean with "statements" and "block lines" though?
1 2 3 4
if (ch =='{' || ch == '}')
//the program shoud read this and print it out,
// however count should not be affected by this.
Can you put what you are actually trying to do in words? Since your code isn't doing what you want it to do - you wouldn't be here if it did - it doesn't help me understanding what you want.
ifstream in;
int count = 0;
char c;
while (in)
{
in.get(ch);
if (c =='{' || c == '}')
count ++;
if ( c == '/')
{
in.peek();
if (c=='*')
if (c=='{')
count --;
if (c== '*')
in.peek():
if (c=='/')
break;
}
//
}
For checking if braces are balanced you can use a stack:
If you encounter an opening brace, push it on the stack. If you encounter a closing brace, pop from the stack and check if what you get is a matching opening brace, if it's not the braces are unbalanced. The same also applies if the stack is empty when you encounter a closing brace (too many closing braces) or if the stack is not empty when you're done parsing the file (too many opening braces).
If you just want to check for 1 kind of braces you could just do the same thing with a counter like you did instead of with a stack.
As to comment awareness etc:
Use a FSM:
It should have these states: Normal, Inline Comment, Comment Block, String.
In normal you check for matching brace, in the other states you don't.
The transitions would be:
Normal: If // is encountered, to Inline Comment
Normal: If /* is encountered, to Block Comment
Normal: If " is encountered, to String
Inline Comment: If end of line is encountered, to Normal
Block Comment: If */ is encountered, to Normal
String: If unescaped " is encountered, to Normal
Note that additional states are required because some state transitions require multiple input symbols - that's a detail you will have to figure out on your own though.