1

I have a short program designed to count the number of consonants in a string by first testing to see if the character in the array is an alpha character (to skip any white space or punctuation). I keep getting a Debug Assertion Failed for my "if (isalpha(strChar))" line of code.

"strChar" is a variable that is assigned the char value in a for loop

Sorry if this is a remedial issue, but I'm not sure where I'm going wrong. Thanks in advance for any help!

#include <iostream>
#include <cctype>
using namespace std;
int ConsCount(char *string, const int size);


int main()
{
    const int SIZE = 81; //Size of array
    char iString[SIZE]; //variable to store the user inputted string

    cout << "Please enter a string of no more than " << SIZE - 1 << " characters:" << endl;
    cin.getline(iString, SIZE);

    cout << "The total number of consonants is " << ConsCount(iString, SIZE) << endl;
}

int ConsCount(char *string, const int size)
{
    int count;
    int totalCons = 0;
    char strChar;

    for (count = 0; count < size; count++)
    {
        strChar = string[count];
        if (isalpha(strChar))
            if (toupper(strChar) != 'A' || toupper(strChar) != 'E' || toupper(strChar) != 'I' || toupper(strChar) != 'O' || toupper(strChar) != 'U')
            totalCons++;
    }
    return totalCons;
}
  • 1
    What input are you giving the program? – NathanOliver Mar 27 '18 at 12:43
  • 1
    Please [edit] the question to 1. indicate the input you're providing, and 2. include the part of the error message which says *which* debug assertion failed. – Angew is no longer proud of SO Mar 27 '18 at 12:44
  • 5
    What happens if you enter only 3 characters? Why are you looping all the way up to 81? See an issue with this? And why aren't you using `std::string`, where these issues of string size is taken care of automatically? – PaulMcKenzie Mar 27 '18 at 12:45
  • You should never trust your user. Validate the input you are getting. – Mohit Mar 27 '18 at 12:45
  • 1
    `int isalpha( int ch );` takes an `int` so widening will occur. If `char` is signed and `strChar` is negative undefined behaviour will result. _"The behavior is undefined if the value of ch is not representable as unsigned char or is not equal to EOF."_ http://en.cppreference.com/w/cpp/string/byte/isalpha – Richard Critten Mar 27 '18 at 12:46
  • BTW, have you stepped through the program with a debugger to inspect what it's doing and at what point it fails? If so, you should include the findings in the question. If not, you should go do that now (and generally do this before asking on SO). – Angew is no longer proud of SO Mar 27 '18 at 12:47
  • Thank you everyone for the great feedback! I'm pretty new to programming, and this was my first post on here, so I appreciate all of the constructive feedback!! I will be sure to include better/more information in my question in the future. – James Beaudry Mar 27 '18 at 14:16

3 Answers3

1

I guess the problem is that you are always looping through 81 characters even though less were entered. That results in some random data fed to isalpha().

Anyway, I would change to code to use std::string instead of char iString[SIZE] to get the actual length of the input text.

juzzlin
  • 45,029
  • 5
  • 38
  • 50
0

The function ConsCount(char* string, const int size) should be like this:

int ConsCount(char *string, const int size)
{
    int consCount = 0;

    char* begin = string;
    for (char* itr = begin; *itr != '\0'; ++itr)
    {
        if (isalpha(*itr)) {
            char ch = toupper(*itr);

            switch(ch) {
                case 'A':
                case 'E':
                case 'I':
                case 'O':
                case 'U':
                    break; // If ch is any of 'A', 'E', 'I', 'O', 'U'
                default:
                    ++consCount;
            }
        }
    }

    return consCount;
}

As you can see I replaced the if statement with switch for better readability and using char* as a iterator to iterate the string. You can remove the unused parameter int size in your code.

And also I suggest you to use std::string for the safe-code. It also provides you a iterator class to iterate over the std::string.

Mohit
  • 1,225
  • 11
  • 28
-1
int ConsCount(char *string, const int size)
{
    int consCount = 0;

    char* begin = string;
    for (char* itr = begin; *itr != '\0'; ++itr)
    {
        if (isalpha(*itr)) {
            char ch = toupper(*itr);

            switch(ch) {
                case 'A':
                case 'E':
                case 'I':
                case 'O':
                case 'U':
                    break; // If ch is any of 'A', 'E', 'I', 'O', 'U'
                default:
                    ++consCount;
           }
        }
    }

    return consCount;

try this

}