Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am unable to count vowels and consonants. It just displays 0.

What I have tried:

C++
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

int main()
{
	string name;
	int vowels_aCount = 0, vowels_e = 0, vowels_i = 0, vowels_o = 0, vowels_u = 0, consonant = 0;

	cout << "********************************" << endl;
	cout << "*      Name " << setw(3) << " : "<< name << setw(17) << "*";
	getline(cin, name);
	cout << "********************************" << endl;

	cout << "*         a " << setw(3) << " : "
         << setw(8) << vowels_aCount << setw(10) << " * " << endl;

	for (int i = 0; name[i]; i++)
	{
		if (name[i] == 'a' || name[i] == 'A')
		{
			vowels_aCount++;
		}
	}

	cout << "*         e " << setw(3) << " : "
         << setw(8) << vowels_e << setw(10) << " * " << endl;

	for (int i = 0; name[i]; i++)
	{
		if (name[i] == 'e' || name[i] == 'E')
		{
			vowels_e++;
		}
	}

	cout << "*         i " << setw(3) << " : "
         << setw(8) << vowels_i << setw(10) << " * " << endl;

	for (int i = 0; name[i]; i++)
	{
		if (name[i] == 'i' || name[i] == 'I')
		{
			vowels_i++;
		}
	}

	cout << "*         o " << setw(3) << " : "
         << setw(8) << vowels_o << setw(10) << " * " << endl;

	for (int i = 0; name[i]; i++)
	{
		if (name[i] == 'o' || name[i] == 'O')
		{
			vowels_o++;
		}
	}

	cout << "*         u " << setw(3) << " : "
         << setw(8) << vowels_u << setw(10) <<" * " << endl;

	for (int i = 0; name[i]; i++)
	{
		if (name[i] == 'u' || name[i] == 'U')
		{
			vowels_u++;
		}
	}

	cout << "*  Consonant " << setw(2) << ": "
         << setw(8) << consonant << setw(10) << " * " << endl;

	for (int i = 0; name[i]; i++)
	{
		if (name[i] >= 'a' && name[i] <= 'z' ||
            name[i] >= 'A' && name[i] <= 'Z')
		{
			consonant++;
		}
	}

	cout << "********************************" << endl;
	return 0;
}
Posted
Updated 4-May-22 7:59am
v3
Comments
jeron1 4-May-22 13:16pm    
Looks like you are outputting the counts before you try and calculate the counts.
[no name] 4-May-22 14:25pm    
You could reduce all that down to a single line. Have a look at std::count_if
jeron1 4-May-22 18:33pm    
Also, you should take a look at your test for consonant, what if the letter is 'e' for example. It's between 'a' and 'z', and you're incrementing the consonant variable.
Avatar 2 2 4-May-22 23:13pm    
I'm a beginner so I'm not quite sure about how to calculate before cout. May I know how to calculate vowels ?
jeron1 5-May-22 11:28am    
Move the cout line after you've done your counts, not before.

As said by Greg, you display count before actually counting:
C++
cout << "*         a " << setw(3) << " : "
     << setw(8) << vowels_aCount << setw(10) << " * " << endl;

for (int i = 0; name[i]; i++)
{
    if (name[i] == 'a' || name[i] == 'A')
    {
        vowels_aCount++;
    }
}

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
You are displaying each count before you actually calculate it.

A few other things.

1. You are not excluding vowels when calculating the number of consonants.

2. This is very C-ish:

for (int i = 0; name[i]; i++)

The more idiomatic C++ way to write it is

for (int i = 0; i < name.size(); i++)

3. You don't need to use setw(n) before a string that is n characters long.

4. Maybe you're practicing formatting, in which case ignore what I'm about to say. But as you write more code, you'll gradually come to the conclusion that drawing boxes, underlines, and other embellishments in output is noise that should usually be omitted.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900