The line
if(ch!='endl'||';')
does NOT do what you think. You are checking that ch
is not equal to endl
, or that ;
is. And since ;
is always true (not zero), you are not really having a very useful comparison here.
You might be thinking of
if(ch!='endl'|| ch!=';')
Or something like that. Not sure what you are trying to achieve with your comparison...
As for the other part of your question - the top Google hit for "C++ read file line by line" gets you to the following stackoverflow answer:
https://stackoverflow.com/a/7868998/1967396
It's all you need.
One more thing - you need to define more clearly for yourself what a "line of code" is. Is basically "any carriage return, or any semicolon" a new line? What about semicolon followed by new line? What about a line with two semicolons (two statements on one line)?
It will take a bit more puzzling on your part to clear up your thinking. I recommend that you split the logic into
- Get a line from the file
- Count semicolons
- If there are semicolons, is there anything else of note after them
- Decide how to count these
Final thought: the single quote ' '
is used to define a character constant. The double quote " "
is used to define a string constant. The end of line character is usually \n
or \r
; in C++ we use std::endl
as a way to write that - but it's not in quotes. And when you use namespace std
presumably you can just write endl
when you mean '\n'
. But putting single quotes around it confuses me - and probably the compiler too.