0

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;

}
6
  • 7
    *strPtr == 'a' || 'A' || 'e' || 'E' || 'i' || 'I' || 'o' || 'O' || 'u' || 'U' will always be true because 'A' is not 0. Commented Feb 12, 2013 at 3:00
  • I'm not sure I understand because in the while I'm check for the '/0', the null terminator. Commented Feb 12, 2013 at 3:02
  • 1
    Are you looking for any specific bool condition 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 to if (strchr(vowels, *strPtr)) Commented Feb 12, 2013 at 3:02
  • Ohhh. Ok so I should not use the or || because its searching for all the vowels? Like instead use a switch statement for each char? Commented Feb 12, 2013 at 3:06
  • @user2063325, Personally, I would use std::count_if, but it's probably disallowed. See my answer for the proper way to compare against multiple conditions. Commented Feb 12, 2013 at 3:07

3 Answers 3

3

I'm going to assume you're limited to not using std::string or std::getline and that you have to assume the user inputs something less than 51 characters.

Your crash stems from:

while (*strPtr != '/0')

A null character is an escape code. '/0' is a multicharacter literal with an implementation-defined value. That means it's probably always true. Change it to:

while (*strPtr != '\0') //or while (strPtr)

Apart from that, you have a logic error with your vowel check. You have to check it against each vowel, like this:

if (*strPtr == 'a' || *strPtr == 'e') //etc.

You'll find it easier if you compare against the toupper or tolower version of each character to reduce the number of comparisons by a factor of 2.

Sign up to request clarification or add additional context in comments.

Comments

2
while (*strPtr != '/0')

Should be:

while (*strPtr != 0)

or

while (*strPtr != '\0');

Didn't your compiler give you a warning? If so, don't ignore warnings. If not, get a better compiler.

See also the comments about the error in your other comparison.

1 Comment

Yea it didn't give me a warning. :/ . I can't believe I had the slash going the wrong way. Thanks so much!
2

Other answers should fix your issue, may I suggest you write separate functions instead of one almighty function?

bool IsSpecialChar(char c)
{
 switch(c)
 {
    case 'a':
    case 'A':
    case 'e':
    case 'E':
    case 'i':
    case 'I':
    case 'o':
    case 'O':
    case 'u':
    case 'U':
    return true;
 }
   return false; 
}


int specialCounter(char *strPtr, int &cons)
{
  int vowels = 0;
  cons = 0;
  while (*strPtr != '\0')
  {
    IsSpecialChar(*strPtr) ? vowels++ : cons++;
    strPtr++;

  }
  return vowels;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.