I can't figure out what I have wrong in teh following program. I am trying to count total characters, vowels, and consonants. I have even worked through it with a friend and neither of us can figure it out. The code follows:

#include <iostream>
#include <string>
#include <cstring>

using namespace std;

void stringout();
string input;

int main()
{

    cout << "Input a string of characters: " ;
    cin >> input;
    cout << "You entered: " << input << endl;

    stringout();


    return 0;
}

void stringout()
{
    char ch;
    int vowel=0, consonant=0;
    int length = input.length();

    for (int i = 0; i < length; i++)
    {
        if ((ch == 'a') || (ch == 'e') || (ch == 'i') || (ch == 'o') || (ch == 'u') ||
            (ch == 'A') || (ch == 'E') || (ch == 'I') || (ch == 'O') || (ch == 'U'))
            ++vowel;
    else
        ++consonant;
    }
    

    cout << "This has " << input.length() << " characters, " <<
        vowel << " vowels, and " << consonant << " consonants." << endl;
}

The program counts characters correctly, but it will not count vowels and counts every character as a consonant. I am thinking it has something to do with the ch variable not having any value so the program assumes that it every character is a consonant. I just can't figure out how to fix it.

Hopefully someone can help.

Your program doesn't work becuase you never assign a value to 'ch'. Try adding ch = input[i]; inside the for loop.

Oh and btw, use of global variables is a bad programming practice, avoid it. Also don't do redundant comparisions.

for(int i = 0; i < length; ++i)
{
    ch = tolower(input [i]);
    if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
        ++vowels;
  
   // remaining code here
}

since we are using std::string, why not

#include <iostream>
#include <string>
using namespace std;
int main()
{
  string str ;  cin >> str ; 
  const string vowels = "aeiouAEIOU" ;
  int n_vowels = 0 ;
  for( string::size_type pos = str.find_first_of(vowels) ; pos != string::npos ;
        pos = str.find_first_of(vowels,pos+1) )  ++n_vowels ;
  cout << "vowels: " << n_vowels  << "\nconsonants: " 
          << str.size()-n_vowels    << '\n' ;
  return 0 ;
}
commented: Yes a good approach but too much for a beginner..;) - ~s.o.s~ +15

since we are using std::string, why not

#include <iostream>
#include <string>
using namespace std;
int main()
{
  string str ;  cin >> str ; 
  const string vowels = "aeiouAEIOU" ;
  int n_vowels = 0 ;
  for( string::size_type pos = str.find_first_of(vowels) ; pos != string::npos ;
        pos = str.find_first_of(vowels,pos+1) )  ++n_vowels ;
  cout << "vowels: " << n_vowels  << "\nconsonants: " 
          << str.size()-n_vowels    << '\n' ;
  return 0 ;
}

Probably because it's beyond his current knowledge... ;) That's for someone far above his level of experience.

Your program doesn't work becuase you never assign a value to 'ch'. Try adding ch = input[i]; inside the for loop.

Oh and btw, use of global variables is a bad programming practice, avoid it. Also don't do redundant comparisions.

for(int i = 0; i < length; ++i)
{
    ch = tolower(input [i]);
    if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
        ++vowels;
 
   // remaining code here
}

Thanks so much for the help. It worked great. I can't believe I didn't know how to get a value into the ch variable. Oh well, live and learn.
I also appreciate the tip about global variables being a bad idea.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.