Program executes without any error. My program is suppose to count
Total lines
Total words
Total sentences
Total characters
from any external txt file. In my case the file is Compiler.txt. User puts the data over there, and my program Counts the lines etc and puts the result into another txt file i.e. Output.txt in my case.
My program successfully tells me the
Total sentences
Total characters
But NOT THE
TOTAL LINES AND TOTAL WORDS
Can u please help what is the error in the code.
My Code
If I comment the line you said;
Dev C++ gives me a Segmentation Error. IDK what that is and my output file remains unchanged (if change my input, it would still show me the last output) but if do not comment that line, it wont give me the error and would still show the Sentences and Char, but not the lines and words, and therefore, my problems remains
You shouldn't comment that, you should substitute it with something else.
In the first place, giving a file with more than 100 chars will crash your program.
So, remove the array and only read one char at a time.
Also get rid of 'x', as it only exists for array in this context.
Then, a good substitute could be
1 2 3 4 5 6 7 8 9 10 11 12 13 14
while(1){
char c;
if(!file.read(&c,1))
break; //file ended or reading error
switch(c){
case' ':
++spaces;
break; //this only breaks the switch
case'\n':
++lines;
break;
//... and so on
}
}