Program that counts syllables and displays them on a table

I am a beginner and I am learning C++ for the first time. I am having difficulty with some homework, the program is asking me to count the syllables of a word and then display them on a table with the words on one side and the number of syllables on the other. The problem suggests to count the number of vowels and use that as the number of syllables. However, that doesn't take into account words that have two vowels together to make one syllable such as in 'Hairy' or words that end in 'e' such as in 'Hare'.

I know I might not have coded the table in the most efficient way, but this is what made the most sense to me. I've written two different programs to get me started. The first one I tried to figure out the algorithm that would count the syllables in my word, that will be under code1. The second program is the table and that one will be under code2.

For the first part in code1, I feel that the way I made it deduct a syllable if there are two vowels together wasn't the most efficient way to do so, but it worked for me. The problem I am facing in this part is that I do not know how I would go about checking if the last character in my word is an 'e' and therefore deducting a syllable such as in 'Hare'. Another situation I would run into if I do decide to go that route is for the word 'The'. It would count 'the' as having no syllables.

For the second part of my code, code2, I am having trouble implementing char into my table. When the user inputs the first char, it skips over the other 3 char variables and goes straight into my table and messes everything up. It works if I change all the word variables into strings, but I need them to be char in order to count the syllables.

I plan to input the process from my first program into my second program, kind of like how I did for the first row under the body comment once I figure it all out.

code1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;

int main()
{
	char word;
	int count = 0;

	cout << "Please enter the word: ";
	cin.get(word);

	cout << "Vowels: ";
	while (word != '\n')
	{
		cin.get(word);
		switch (toupper(word))
		{
		case 'A': cout << word << ", ";
			count++;
			break;
		case 'E': cout << word << ", ";
			count++;
			break;
		case 'I': cout << word << ", ";
			count++;
			break;
		case 'O': cout << word << ", ";
			count++;
			break;
		case 'U': cout << word << ", ";
			count++;
			break;
		case 'Y': cout << word << ", ";
			count++;
			break;
		}
	}
	if (cin.get(word))
	{
		word == 'AI';
		word == 'AU';
		word == 'EA';
		word == 'EE';
		word == 'IE';
		word == 'OA';
		word == 'EO';
		word == 'OI';
		word == 'OO';
		word == 'OU';
		word == 'UI';
		count--;
	}
		
	cout << "\nThere is a total of " << count << " syllables in your word." << endl;
	
	system("pause");

	return 0;
}


code2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include "stdafx.h"
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

int main()
{
	const int COLUMNS = 3;
	int count = 0;
	char word1;
	char word2;
	char word3;
	char word4;

	// User Inputted Words

	cout << "Please enter the first word to see how many syllables it contains." << endl;
	cin >> word1;
	cout << endl;
	cout << "Please enter the second word to see how many syllables it contains." << endl;
	cin >> word2;
	cout << endl;
	cout << "Please enter the third word to see how many syllables it contains." << endl;
	cin >> word3;
	cout << endl;
	cout << "Please enter the fourth word to see how many syllables it contains." << endl;
	cin >> word4;
	cout << endl << endl;

	// Header

	for (int columns_header = 0; columns_header <= COLUMNS; columns_header++)
	{
		if (columns_header == 0)
		{
			cout << setw(10) << "Words";
		}
		else if (columns_header == 1)
		{
			cout << setw(10) << "Vowels";
		}
		else if (columns_header == 2)
		{
			cout << setw(13) << "Syllables";
		}
	}

	cout << endl;

	for (int columns_header = 0; columns_header <= COLUMNS; columns_header++)
	{
		if (columns_header == 0)
		{
			cout << " ___________";
		}
		else if (columns_header == 1)
		{
			cout << "____________";
		}
		else if (columns_header == 2)
		{
			cout << "_____________";
		}
	}
	cout << endl << endl;

	// Body
	for (int row1 = 0; row1 <= COLUMNS; row1++)
	{
		if (row1 == 0)
		{
			cout << setw(10) << word1;
		}
		else if (row1 == 1)
		{
			cin.get(word1);
			switch (toupper(word1))
			{
			case 'A': cout << word1 << ", ";
				count++;
				break;
			case 'E': cout << word1 << ", ";
				count++;
				break;
			case 'I': cout << word1 << ", ";
				count++;
				break;
			case 'O': cout << word1 << ", ";
				count++;
				break;
			case 'U': cout << word1 << ", ";
				count++;
				break;
			case 'Y': cout << word1 << ", ";
				count++;
				break;
			}
		}
		else if (row1 == 2)
		{
			cout << setw(10) << count;
		}
	}
	cout << endl;

	for (int row2 = 0; row2 <= COLUMNS; row2++)
	{
		if (row2 == 0)
		{
			cout << setw(10) << word2;
		}
		
	}

	cout << endl;

	for (int row3 = 0; row3 <= COLUMNS; row3++)
	{
		if (row3 == 0)
		{
			cout << setw(10) << word3;
		}
	}
	cout << endl;

	for (int row4 = 0; row4 <= COLUMNS; row4++)
	{
		if (row4 == 0)
		{
			cout << setw(10) << word4;
		}
	}
	cout << endl << endl;

	system("pause");

	return 0;
}


For the table, I inserted another column for vowels just to do a little extra, but I might remove it once the program is done. Also the '#include "stdafx.h"' is there because I am using visualstudio. Any suggestions on how I might go about fixing these problems?
Thank you
Last edited on
@Jorge626, this looks like an unbelievably difficult problem in linguistics, let alone in c++.

My take on it is that the number of syllables is (approximately) the "number of separately sounded vowels".

I would count vowels in a word first and then:
- subtract the number that can be combined with an adjacent vowel (e.g "oi" in "loin", but NOT "io" as in "Studio") - you will have to look at all ordered pairings;
- subtract unsounded 'e' for words that end in isolated 'e' or "es".

Note that, for these purposes, 'y' is also a vowel ("synonym" has 3 syllables). In Wales, 'w' would also be a vowel.

It's far from perfect. How many syllables in "syllable" - si-la-bull? What do you do about apostrophes used in place of a dropped letter: "didn't" etc? How many syllables are there in the word "fire"? (Just try saying it!)
Last edited on
Thank you ! You helped me a lot in sorting out the first part of the program. I finally have it working for the part where it counts the syllables (for most words at least). It's still far from perfect. But it gets the job done for most words I input into it. However, I am still having difficulty on the second part of the program when I try inputting it into my table. It's not letting me use the char variable, it just gets the first input and then skips the other 3 inputs and goes straight into the table, leaving a single letter in the first column of each row. Any suggestions on how I might go about fixing this problem?
@Jorge626,
You aren't reading words at all - a char is just a single letter.

You need to read a word into (ideally) a std::string and then loop through its characters to count vowels. Do that first before worrying about vowel combinations or unsounded e's.

In your first code, == is a test for equality, giving a bool result. Single = means assignment. But I can't work out what you were trying to do here.

Okay, I changed it back to a string. I'm having difficulty now trying to check for vowel combinations and unsounded e's. This is the code I have now to check for vowels.

1
2
3
4
5
6
7
for (int vowels = 0; vowels < word1.length(); vowels++)
{
	if ((word1[vowels] == ('a')) || (word1[vowels] == ('e')) || (word1[vowels] == ('i')) || (word1[vowels] == ('o')) || (word1[vowels] == ('u')) || (word1[vowels] == ('y')))
	{
		vowels1++;						
	}
}


I've tried using string find , find_last_of, find_last_not_of , back, and a few others but nothing is working. Should I try making another for loop? Or is it fine to do it in the same loop? Any other suggestions on how I might check for unsounded e's or vowel combinations?
Last edited on
Never mind, I figured it all out! Thank you for your help, I really appreciate it!
Topic archived. No new replies allowed.