find consonants

i wrote the following function to find consonants in a series of strings. My program compiles fine but I am not getting the right number of consonants. I used the toupper function then used nested ifs to simplify instead of writing out all the consonants. Maybe I just totally confused my self could anyone point me in the right direction.

1
2
3
4
5
6
7
8
9
10
11
12
void count_consonants(string word, int& numcons)
{
    int wlength;
    wlength = word.length();
    for (int i=1; i<wlength; i++)
    {
        word[i] = toupper(word[i]);
        if (word[i] >= 'A' || word[i] <= 'Z')
            if (word[i] != 'A' || word[i] != 'E' || word[i] != 'I' || word[i] != 'O' || word[i] != 'U')
        numcons = numcons + 1;
    }
}
i just realized that i used || instead of && in the first if statement. Thats gets me closer but still not getting the right number of consonants?
by the way the string can include digits and punctuation marks, which i thought i eliminated with the >= A && <= Z if statement.
You need && in the second if statement as well, since if the letter is 'E', it will still be != to 'A', 'I', etc.
closed account (S6k9GNh0)
semispoiler wrote:
http://codepad.org/NnLNmz3r
Last edited on
I tried that also and it gives me too few consonants.
here is my data code and sample file. I tried all suggestions and I still get an incorrect count for my consonants.

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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <iostream>
#include <string>
#include <cctype>
#include <fstream>

using namespace std;

bool at_endl(ifstream&);
void skip_blanks(ifstream&);
void reformat(string&);
void getfilename(string,string&);
void count_digit(string,int&);
void count_consonants(string, int&);

int main()
{
    string word, filename;
    char ch;
    int digits=0, numcons=0;
    ifstream input;
    getfilename("input",filename);
    input.open(filename.c_str());
    input >> word;
    while (!input.eof())
        {
            reformat(word);
            count_digit(word,digits);
            count_consonants(word,numcons);
            cout << word;
            skip_blanks(input);
            if (at_endl(input))
                {
                    cout << endl;
                }
            else
                cout << " ";
            input >> word;
        }
            cout << digits << endl;
            cout << numcons;
    return 0;
}

void getfilename(string filetype, string& filename)
{
    cout << "Enter the name of " << filetype << " file\n";
    cin >> filename;
}

bool at_endl(ifstream& input)
{
    return (input.peek() == '\n');
}

void skip_blanks(ifstream& input)
{
    char ch;
    while (input.peek() == ' ')
        input.get(ch);
}

void reformat(string& word)
{
    int wlength;
    wlength = word.length();
    word[0] = toupper(word[0]);
    for (int i=1; i<wlength; i++)
        word[i] = tolower(word[i]);
}

void count_digit(string word, int& digits)
{
    int wlength;
    wlength = word.length();
    for (int i=1; i<wlength; i++)
    {
        if (isdigit(word[i]))
            digits = digits + 1;
    }
}

void count_consonants(string word, int& numcons)
{
    int wlength;
    wlength = word.length();
    for (int i=1; i<wlength; i++)
    {
        word[i] = toupper(word[i]);
        if (word[i] >= 'A' || word[i] <= 'Z')
            {
                if (word[i] != 'A' && word[i] != 'E' && word[i] != 'I' && word[i] != 'O' && word[i] != 'U')
                    numcons = numcons + 1;
            }
    }
}


sample text file:

here5 is some3 TEST data4
to see2 if THE prOGRAm will
work. "what do8 you know!"
wiLL IT??

figured it out :-)..... i = 0 not i = 1 and >=A && <= Z
Topic archived. No new replies allowed.