how?

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. 


Any ideas?

thanks
Great. What's the question?
I am not sure about the way to do the comment line
Think about where in a sentence count should go up.

You are on the right lines though.
count should not be effected on the comment lines, block lines, statments '{' || '}' or strings.

But since i am reading char by char and displaying it at the same time,

I cant put it together...
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?
Well, you'll need additional variables. For example, a bool variable that keeps track of whether you're currently in a /*C style comment*/.
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.

A bool is NOT a good idea however.
Last edited on

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?
Last edited on
hanst99,

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. 
Last edited on
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.
I am not near my code:

but i have this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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;

}
//
}
Words. Like what I am typing right now. Use them to describe what your problem is.
Sorry...
Im supposed to write a program that reads a file.
Prints the file on screen.

Read '{' or '}' to see if the file is godd or bad.

Basiclly if the braces are balanced.

Have to be aware for braces inside comments,comments block and so on
It looks like the way that i am aproching is wrong.
A suggestion on how to do it.
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.
Last edited on
Topic archived. No new replies allowed.