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