I've been stuck on this question for a while... I got this question in my Assignment:
Write a LetterCount function (and the main() function to test it) that takes as input a line of text and then it counts and returns the number of vowels and number of non-vowel letters. The line of text is read in the main() function and passed to the LetterCount function, which returns the count of vowels and non-vowel letters. The main() function reads the line of text and prints the counts. Note that the LetterCount function should exclude the count of symbols and spaces.
So my attempt on this worked for counting Vowels only.. I am not really sure if I need to use void because it asks for 2 returns or if I could do nonVowels and Vowels in 1 'int' Function.
Here's my code so far, it only prints the correct number of Vowels:
In this case, no.
If the function returns the count of vowels, the count of non-vowels can be computed in main.
(count of non-vowels == count of total number of characters in the line - count of characters that are vowels)
#include<iostream>
#include<cstring>
// using namespace std; // best avoided
int VowelCount( constchar line[] ); // const: make it const-correct
int main()
{
constint MAX_CHARS = 200 ; // avoid magic numbers
char line[MAX_CHARS];
// postpone the declaration of variables till we have a value to initialise them
// int countVowels ;
// int countNonVowels;
std::cout << "Enter a line of text: ";
std::cin.get( line, MAX_CHARS );
// http://en.cppreference.com/w/cpp/io/basic_istream/gcountconstint nchars = std::cin.gcount() ; // the actual number of characters read into line
constint countVowels = VowelCount(line);
// every character other than these countVowels characters is a non-vowel
constint countNonVowels = nchars - countVowels ;
std::cout << " The number of vowel letters is: " << countVowels << '\n'
<< "The number of non-vowel letters is: " << countNonVowels << '\n' ;
// return 0; // there is an implicit return 0 ; at the end of main
}
int VowelCount( constchar line[] )
{
if( line == nullptr ) return 0 ; // sanity check
int countVowels = 0;
constint length = std::strlen(line);
for( int counter = 0; counter < length; ++counter )
{
switch( line[counter] )
{
case'a': case'A': case'e': case'E':
case'i': case'I': case'o': case'O':
case'u': case'U':
++countVowels ;
}
}
return countVowels;
}