I'm having a vowel count problem when I build the code.

#include <iostream>
#include <string.h>

using namespace std;

char str1[51];

int vowelA = 0, vowelE = 0, vowelI = 0, vowelO = 0, vowelU = 0;

int vCount()
{
    int ii; // Counter

    cout << "\nResult: " << endl;
    cout << "A: ";
    for (int ii = 0; ii <= vowelA; ii++)
    {
        cout << "*";
    }
    cout << endl;

    cout << "E: ";
    for (int ii = 0; ii <= vowelE; ii++)
    {
        cout << "*";
    }
    cout << endl;

    cout << "I: ";
    for (int ii = 0; ii <= vowelI; ii++)
    {
        cout << "*";
    }
    cout << endl;

    cout << "O: ";
    for (int ii = 0; ii <= vowelO; ii++)
    {
        cout << "*";
    }
    cout << endl;

    cout << "U: ";
    for (int ii = 0; ii <= vowelU; ii++)
    {
        cout << "*";
    }
    cout << endl;

    return 0;
}

int main()
{
    int nochar; // Number of characters in a word
    int i; // Counter

    cout<<"Enter a word: ";
    cin.getline(str1,51);

    _strlwr_s(str1);

    nochar = strlen(str1);

    for (int i = 0; i <= nochar; i++)
    {
        if (str1[i] == 'a')
        {
            vowelA++;
        }
        if (str1[i] == 'e')
        {
            vowelE++;
        }
        if (str1[i] == 'i')
        {
            vowelI++;
        }
        if (str1[i] == 'o')
        {
            vowelO++;
        }
        if (str1[i] == 'u')
        {
            vowelU++;
        }
    }

    vCount();

    system("pause");
    return 0;
}

When I typed in the word, the program counts how many vowels are there in a word incorrectly.
e41609cba18217bcb365a3accda39a5f

Change all the for loops in your vCount function to be like:
for (int ii = 0; ii < vowelA; ii++) // < not <=

The same for line 65. for (int i = 0; i < nochar; i++)

Edited 3 Years Ago by nullptr

This article has been dead for over six months. Start a new discussion instead.