M. Alex M. Alex - 26 days ago 5
C++ Question

How to get Cstring functions working?

I'm trying to make a function that counts the number of vowels and consonants in my cstring, as well as change my cstring to uppercase and lowercase but it doesn't seem to be working. Whenever I run the function to count the vowels and consonants it always returns with "0". What can I do to fix this? Thanks for the help.

#include <iostream>
#include <iomanip>
#include <cctype>
#include <cstring>
#include <cstdlib>
using namespace std;

int Count(const char[], int);
int Count2(const char[], int);
void UpperCase(const char[]);
void LowerCase(const char[]);

int main(int argc, const char * argv[]) {

char choice;
char text[100];
int vowel;
int consonant;

cout << "Input a line of text, up to 100 characters:\n";
cout << ">";
cin.getline(text, 100);

cout << " \n";

cout << "A) Count the number of vowels in the string \n";
cout << "B) Count the number of consonants in the string \n";
cout << "C) Convert the string to uppercase \n";
cout << "D) Convert the string to lowercase \n";
cout << "E) Display the current string \n";
cout << "F) Enter another string \n";
cout << " \n";
cout << "M) Display this menu \n";
cout << "X) Exit the program \n";
cout << " \n";

do
{

cout << "Enter your menu selection: ";
cin >> choice;
switch (choice){

case 'A':
case 'a':
Count(text, vowel);
cout << "Number of vowels: " << vowel << endl;
cout << " \n";
break;

case 'B':
case 'b':
Count2(text, consonant);
cout << "Number of consonants: " << consonant << endl;
cout << " \n";
break;

case 'C':
case 'c':
UpperCase(text);
cout << text;
cout << " \n";
break;

case 'D':
case 'd':
LowerCase(text);
cout << text;
cout << " \n";
break;

case 'E':
case 'e':
break;

case 'F':
case 'f':
break;

case 'M':
case 'm':
break;

case 'X':
case 'x':
cout << "Goodbye \n";
return 0;


}
}
while (choice != 0);









return 0;
}

int Count(const char text[], int a)
{
int i;
a = 0;
for(i = 0; text[i] != '\0'; i++)
{
if(text[i]=='a' || text[i]=='e' || text[i]=='i' || text[i]=='o' || text[i]=='u' || text[i]=='A' || text[i]=='E' ||text[i]=='I' || text[i]=='O' || text[i]=='U')
{
a--;
}


}

return a;
}

int Count2(const char text[], int b)
{
int i;
b = 0;
for(i = 0; text[i] != '\0'; i++)
{
if(text[i] != 'a' || text[i] != 'e' || text[i] != 'i' || text[i] != 'o' || text[i] != 'u' || text[i] != 'A' ||
text[i] != 'E' || text[i] != 'I' || text[i] != 'O' || text[i] != 'U')
{
b--;
}

}

return b;

}

void UpperCase(const char text[])
{
int i;
for(i = 0; text[i] != '\0'; i++) {
toupper(text[i]);
}
}

void LowerCase(const char text[])
{
int i;
for(i = 0; text[i] != '\0'; i++) {
tolower(text[i]);
}
}

Answer

Vowels and consonants will always be zero because you are passing them by value, hence their values will not change to fix this you should pass them by reference and so the functions prototypes will be

int Count(const char[], int&); //function heading int Count(const char text[], int& a)
int Count2(const char[], int&); //function heading int Count2(const char text[], int &b)

Also in the functions definition you are decrementing the values? They should be incremented.