I need help with this program.

I need the findCons function to return the number of consonants but it keeps giving me a weird number. I know there's a lot of other stuff going on but i'm just trying to figure out this particular function first.

#include <iostream>
#include <string>
#include <cstring>
using namespace std;

int findCons(char* c);
int findVows(char* c);

int main()
{

int choice;
char* c = "Hello World";



cout << "\t\t Menu \n\n"
<< "1. Count the number of vowels in the string.\n"
<< "2. Count the number of consonants in the string.\n"
<< "3. Count both the vowels and consonants in the string.\n"
<< "4. Enter another string.\n"
<< "5. Exit the program.\n";

cin >> choice;

if (choice == 1)
{
findVows(c);
}

else if (choice == 2)
{
findCons(c);
}

else if (choice == 3)
{
findCons(c);
findVows(c);
}
/*
else if (choice == 4)
{
cout << "Please enter another string." << endl;
getline(cin, c);
}

else if (choice == 5)
{
exit();
}
*/
return 0;
}

int findCons(char* c)
{
int consonants = 0;

for (int i = 0; i != '/0'; i++)
{
if (c[i] != 'a' || c[i] != 'A' || c[i] != 'e' || c[i] != 'E' || c[i] != 'i' || c[i] != 'I' || c[i] != 'o' || c[i] != 'O' || c[i] != 'u' || c[i] != 'U')
consonants++;
}
cout << "There are " << consonants + 1 << " consonants." << endl;
return consonants;
}

int findVows(char* c)
{
int vowels = 0;

for (int i = 0; i != '/0'; i++)
{
if (c[i] == 'a' || c[i] == 'A' || c[i] || 'e' || c[i] == 'E' || c[i] == 'i' || c[i] == 'I' || c[i] == 'o' || c[i] == 'O' || c[i] == 'u' || c[i] == 'U')
{
vowels++;
}
}
cout << "There are " << vowels + 1 << " vowels." << endl;
return vowels;
}
Last edited on
any chance your input has spaces, punctuation, or other such things? Your function will count those because they are not in the "not these" hard coded lists you provided.

also, your loop looks wrong.


for (int i = 0; i != '/0'; i++)

this is the same as

for(I = 0; I < 0; I++) //does nothing?

what you wanted was

for(I = 0; c[I] != 0; I++)
I think.


one way to make this a lot easier to deal with is to use toupper or tolower to only check 1/2 as much stuff.

one way to exclude other junk is to do something like

tolower...
if(c[I] > 'z' || c[I] < 'a') //if this is true, the letter is neither vowel nor consonant. do nothing...
...
if(c[I] == 'a' || ... =='u')
++

etc

Last edited on
Thank you for helping. I still keep getting the same answer every time which is 12081. I don't understand how it's getting that.
This is wrong: char* c = "Hello World";
A string literal is an array of const char

This is also wrong: getline(cin, c);
1. std::getline() is used in conjunction with std::string. Use std::cin.getline() instead.
2. We would need to declare an array of char and read into that.

You have #include <string>.
Strongly favour std::string over C-style null-terminated byte strings.

Version using std::string
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
#include <iostream>
#include <cctype>
#include <string>
#include <iomanip>
#include <algorithm>

int cnt_alpha( const std::string& str )
{
    int cnt = 0 ;
    for( char c : str ) if( std::isalpha(c) ) ++cnt ;
    return cnt ;
}

auto cnt_alpha2( const std::string& str ) // alternative version
{ return std::count_if( str.begin(), str.end(), []( char c ) { return std::isalpha(c); } ) ; }

int cnt_vowels( const std::string& str )
{
    static const std::string vowels = "AEIOUaeiou" ; // crude, but simple

    int cnt = 0 ;
    for( char c : str ) if( vowels.find(c) != std::string::npos ) ++cnt ;
    return cnt ;
}

auto cnt_vowels2( const std::string& str ) // alternative version
{
    static const std::string vowels = "AEIOUaeiou" ; // crude, but simple

    return std::count_if( str.begin(), str.end(),
                          []( char c ) { return vowels.find(c) != std::string::npos; } ) ;
}

int cnt_consonants( const std::string& str ) { return cnt_alpha(str) - cnt_vowels(str) ; }

void print_stats( const std::string& str )
{
    std::cout << "  c-string: " << std::quoted(str) << '\n'
              << "characters: " << str.size() << '\n'
              << "     alpha: " << cnt_alpha(str) << " (" << cnt_alpha2(str) << ")\n"
              << "    vowels: " << cnt_vowels(str) << " (" << cnt_vowels2(str) << ")\n"
              << "consonants: " << cnt_consonants(str) << '\n' ;
}

int main()
{
    std::string str = "Hello World!" ;

    print_stats(str) ;

    std::cout << "\nenter a string: " ;
    std::getline( std::cin, str ) ; // read into the string
    print_stats(str) ;
}


Version using C-style null-terminated byte strings
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 <iostream>
#include <cctype>
#include <cstring>
#include <iomanip>

int cnt_alpha( const char* cstr )
{
    int cnt = 0 ;

    if( cstr != nullptr )
        for( int i = 0 ; cstr[i] != 0 ; ++i ) if( std::isalpha(cstr[i]) ) ++cnt ;

    return cnt ;
}

int cnt_vowels( const char* cstr )
{
    static const char vowels[] = "AEIOUaeiou" ; // crude, but simple

    int cnt = 0 ;

    if( cstr != nullptr )
    {
        for( int i = 0 ; cstr[i] != 0 ; ++i )
            if( std::strchr( vowels, cstr[i] ) != nullptr ) ++cnt ;
    }

    return cnt ;
}

int cnt_consonants( const char* cstr ) { return cnt_alpha(cstr) - cnt_vowels(cstr) ; }

void print_stats( const char* cstr )
{
   if( cstr != nullptr )
   {
        std::cout << "  c-string: " << std::quoted(cstr) << '\n'
                  << "characters: " << std::strlen(cstr) << '\n'
                  << "     alpha: " << cnt_alpha(cstr) << '\n'
                  << "    vowels: " << cnt_vowels(cstr) << '\n'
                  << "consonants: " << cnt_consonants(cstr) << '\n' ;
   }
}

int main()
{
    const int MAX_SZ = 100 ;
    char cstr[MAX_SZ] = "Hello World!" ; // array to hold up to MAX_SZ characters

    print_stats(cstr) ;

    std::cout << "\nenter a string (max " << MAX_SZ-1 << " characters): " ;
    std::cin.getline( cstr, MAX_SZ ) ; // read into the array
    print_stats(cstr) ;
}
what does it look like after your fixes?
also its a little cleaner to do cin.getline(variable, maxsize);
Last edited on
I tried this

[code]int findCons(char* c)
{
int consonants = 0;

for (int i = 0; i != '/0'; i++)
{
tolower(c[i]);

if (c[i] > 'a' || c[i] < 'z' && c[i] != 'a' || c[i] != 'e' || c[i] != 'i' || c[i] != 'o' || c[i] != 'u')

consonants++;
}
cout << "There are " << consonants + 1 << " consonants." << endl;
return consonants;
}
Last edited on
You didn't fix the loop.

it still skips the loop because '\0' == 0

that has to be c[I] != 0 instead.

why it does not return 0 and print 1 is a mystery though.

you also need the fixes mentioned above, the allocation of your hello world string which I didn't notice, and the getline, which I mentioned, and read a c-string (char array) or string.



for (int i = 0; i != '/0'; i++)

This is the same as

for(I = 0; I < 0; I++) //does nothing?

Ah, but it isn't. Look closer. OP has '/0', not '\0'. He is mistakenly using a multi-byte character constant. The value of '/0' is 12080.

Multi-byte character constants are a little-used way to form integers from ASCII characters:
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>

using std::cout;
using std::hex;

int main()
{
    cout << hex;
    cout << (int)'/' <<'\n';
    cout << (int)'0' <<'\n';
    cout << (int)'/0' << '\n';
}
2f
30
2f30
I still keep getting the same answer every time which is 12081.

Here is your code again.
1
2
3
4
5
6
7
8
int consonants = 0;

for (int i = 0; i != '/0'; i++)
{
if (c[i] != 'a' || c[i] != 'A' || c[i] != 'e' || c[i] != 'E' || c[i] != 'i' || c[i] != 'I' || c[i] != 'o' || c[i] != 'O' || c[i] != 'u' || c[i] != 'U')
consonants++;
}
cout << "There are " << consonants + 1 << " consonants." << endl;


The for loop is equivalent to for (int i = 0; i != 12080; i++). So the loop executes 12080 times.

The condition at line 5 is always true. Every character doesn't equal 'a' or doesn't equal 'A'. So you always increment consonants. Since the loop exeutes 12080 times, you increment consonants to 12080.

Line 8 prints consonants+1, which is 12081.
sharp eyes, nice!
I fixed it. Thank you!
Topic archived. No new replies allowed.