Please spot out the error

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

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <fstream> 
#include <string> 
#include <iostream> 

using namespace std;
int main(){ 

char input[100];
int character=0;
int word=0;
int line=0;
int x=0;
int sentence=0;
fstream textfile;
textfile.open("compiler.txt");
while(!textfile.eof()){
textfile >> input[x] ;




if (input[x]== '?' || input[x]== '.')
{
sentence++ ;
}

else if(input[x]== '\n' ) 
{
line++ ;
}

else if(input[x]== ' ' ) 
{
word++ ;
}
else 
character++;

x++;
}
textfile.close();



ofstream output;
output.open("output.txt");
output<<"Total lines are "<<line<<endl;
output<<"Total words are "<<word<<endl;
output<<"Total sentences are "<<sentence<<endl;
output<<"Total characters are "<<character<<endl;
output.close();


return 0;
}



My output file says;
Total lines are 0
Total words are 0
Total sentences are 2
Total characters are 56



And my input/Compiler file is;
This is a text file for my compiler assignment number one .
sdfghjkl.


You see, it tells me the sentences and char, but not the words & lines
Last edited on
textfile >> input[x] ;

Doing this ignores any whitespace (like spaces or newlines), hence why you aren't ever seeing any.
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
Last edited on
 
17 textfile >> input[x] ; 



Commenting it = Segmentation eRRor

Not Commenting it = My error is still there
Use std::getline to read in whitespaces. As firedraco mentioned it std::cin >> reads until a whitespace is found.

You know what..It looks like you are trying to read in character by character. Maybe you want std::cin.get
Last edited on
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
    }
}
Last edited on
Topic archived. No new replies allowed.