I have been stuck on this program all day. I finally feel like I'm getting really close. I have to find the number of vowels and characters in a string. Then output them at the end. However, when I compile my program crashes. I have checked syntax and looked in my book all day. If anyone can help I would really appreciate it! because I have 5 more similar functions to write that manipulate c-strings. Thanks!
#include <iostream>
#include <string>
using namespace std;
int specialCounter(char *, int &);
int main()
{
const int SIZE = 51; //Array size
char userString[SIZE]; // To hold the string
char letter;
int numCons;
// Get the user's input string
cout << "First, Please enter a string (up to 50 characters): " << endl;
cin.getline(userString, SIZE);
// Display output
cout << "The number of vowels found is " << specialCounter(userString, numCons) << "." << endl;
cout << "The number of consonants found is " << numCons << "." << endl;
}
int specialCounter(char *strPtr, int &cons)
{
int vowels = 0;
cons = 0;
while (*strPtr != '/0')
{
if (*strPtr == 'a' || 'A' || 'e' || 'E' || 'i' || 'I' || 'o' || 'O' || 'u' || 'U')
{
vowels++; // if vowel is found, increment vowel counter
// go to the next character in the string
}
else
{
cons++; // if consonant is found, increment consonant counter
// go to the next character in the string
}
strPtr++;
}
return vowels;
}
*strPtr == 'a' || 'A' || 'e' || 'E' || 'i' || 'I' || 'o' || 'O' || 'u' || 'U'will always be true because'A'is not 0.boolcondition from that expression? Hint:*strPtr == 'a' || *strPtr == 'A' || *strPtr == 'e'...... After you're done typing all of that, throw it out, put all your test chars in a single string constant (const char vowels[] = "aAeEiIoOuU";) and change your expression toif (strchr(vowels, *strPtr))std::count_if, but it's probably disallowed. See my answer for the proper way to compare against multiple conditions.