some kind of error

I can't seem to find the error, but there is one because the sample output is completely incorrect.

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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
  #include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()

{
    unsigned long int wordsn=0, countT=0, vowel=0, max=0, min=0, novowel=0, countSH=0;
    double sum=0;
    string minWord, maxWord;
    ifstream wordfile;
    string line;
    wordfile.open("/Users/rami/Downloads/default.asp.html");
    if (wordfile.fail())
    {
        cout << "Error opening the file" << endl;
    return 0;
    }
    
    while (!wordfile.eof())
        
    {
        wordfile >> line;
        wordsn++;
        
        int nvow=0;
        if (line[0] == 't') countT++;
        if (wordsn==1) {min=line.length(); max=line.length();}
        if (line[0]=='s' && line[1]=='h') countSH++;
        for (int i=0;i<line.length();i++)
            
        {
            if (line[i]=='a' || line[i]=='e' || line[i]=='i' || line[i]=='o' || line[i]=='u'||line[i]=='y') nvow++;
            
        }
        
        int numberVowY=0;
        
        for(int j=0;j<line.length();j++)
            
        {
            if (line[j]=='a' || line[j]=='e' || line[j]=='i' || line[j]=='o' || line[j]=='u') numberVowY++;
        }
        
        if (numberVowY==line.length()) vowel++;
        if (nvow==0) novowel++;
        sum = sum+line.length();
        if(line.length()<=min && line.length()!=0  ) {min=line.length(); minWord=line;}
        if(line.length()>max) {max=line.length(); maxWord=line;}
        
    }
    
    wordfile.close();
    
    cout << "The total number of words in file: " << wordsn << endl;
    cout << "The total number of words that begin with the letter t: " << countT << endl;
    cout << "The total number of words that have no vowels (a,e,i,o,u), including (y): " << novowel << endl;
    cout << "The average length of the words: "<< sum/wordsn << " letters" << endl;
    cout << "The minimum length of the words: " << min << " The word is: " << minWord << endl;
    cout << "The maximum length of the words: " << max << " The word is: " << maxWord << endl;
    cout << "The total number of words that are all vowels (a,e,i,o,u): " << vowel << endl;
    cout << "The total number of words that begin with 'sh': " << countSH << endl;
    
    return 0;
}
/* Sample Output
 
 The total number of words in file: 798
The total number of words that begin with the letter t: 53
The total number of words that have no vowels (a,e,i,o,u), including (y): 258
The average length of the words: 12.7168 letters
The minimum length of the words: 1 The word is: }
The maximum length of the words: 232 The word is: EXPLORER')?document.contentFrame:window.frames['win'+window.gstrEntryID];try{if(iframe.contentWindow){iframe.contentWindow.printPage();}else{iframe.printPage();}}catch(e){try{iframe.focus();iframe.print();}catch(e){printed=false;var
The total number of words that are all vowels (a,e,i,o,u): 4
The total number of words that begin with 'sh': 0
Program ended with exit code: 0
 
*/
Hi,

First step compilation with warning levels set (I used cpp.sh with all 3 warnings on).

 In function 'int main()': 
31:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] 
40:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] 
46:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]


So the length function returns a std::size_t type, so you should use this type in your for loops.

Line 21: Don't loop on eof. If eof is encountered, the error is set, but a read happens anyway because the error condition is not evaluated until the beginning of the next loop iteration. So loop on the read operation (including the stream) instead, and consider using std::getline . http://en.cppreference.com/w/cpp/string/basic_string/getline

The other thing to do, is consider using a debugger - it will save you days of staring at code. Hopefully there is a GUI one in your IDE. You can step through the code 1 line at a time, keep an eye on the watch-list of variables, see how they change - then deduce where things go wrong. Then see if you can come with ways of preventing that type of error happening by the way you write your code. :+)

Hope this helps !!
Hi,

The first problem I see i this
27
28
29
30
31
32
33
34
35
36
37
38
int nvow=0;
//..
.
.
.
.
.
.
.
.

 int numberVowY=0;


these line should be outside the while loop otherwise the variable will always be assigned 0 value after each loop

also, it would be better if you would show us the contents of your file, it will help us debug it faster
Last edited on
Topic archived. No new replies allowed.