Salem
Posting Sage
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
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
}
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
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 ;
}
vijayan121
Posting Virtuoso
1,606 posts since Dec 2006
Reputation Points: 1,159
Solved Threads: 287
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.
WaltP
Posting Sage w/ dash of thyme
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944