Hello, I'm trying to make this piglatin program for my computer science class. I have the general code done but I am having trouble creating the loop for the words to be redefined correctly. The words are being taken from a file and are the following:
"This is a, bunch of words"
The output should be:
"Isthay isway away. unchbay ofway ordsway"
But I am getting:
"Isthay Isay A,ay Unchbay Ofay Ordsway"
I am pretty sure the problem is coming from the while loop but I have
no idea on how to correct it. Someone please help.
You need to compare first to each letter individually. You can cut the work in half by converting to a particular case (in a temporary variable if you want to maintain the original case for output):
1 2
char c = tolower(first);
if (c != 'a' && c != 'e' && c != 'i' && c != 'o' && c != 'u' )
Or you can compare it using a function from the C library:
if (!strchr("aeiou", tolower(first))) // if first is not a vowel
Or you can use a C++ string function:
1 2
string vowels{"aeiou"};
if (vowels.find(tolower(first)) == vowels.npos)
#include <iostream>
#include <cmath>
#include <iomanip>
#include <string>
#include <fstream>
usingnamespace std;
string normal(string word);
string vowel(string word);
string lowerCase(string x);
string punch(string word);
int main()
{
ofstream fout;
ifstream fin;
string x = "", y = "", word = "";
int numwords = 0, numlett = 0, numchar = 0;
char first;
fin.open("Text.txt");
if (fin.fail())
cout << "it failed" << endl;
while (fin >> word)
{
x += word + " ";
numwords++;
first = tolower(word.at(0));
if (first == 'a' || first == 'e' || first == 'i' || first == 'o' || first == 'u')
{
word = vowel(word);
y += word;
}
else
{
word = normal(word);
y += word;
}
for (int i = 0; i < word.length(); i++)
{
if (isalpha(word.at(i)))
numlett++;
}
for (int i = 0; i < word.length(); i++)
{
if (ispunct(word.at(i)))
numchar++;
}
}
//2. the number of letters in the text(after the conversion to pig latin)
//3. the number of all characters in the text(including punctuation but NOT whitespace)
cout << x << endl;
cout << y << endl;
cout << "The number of letters in the text is " << numlett << endl;
cout << "The number of characters in the text is " << numchar + numlett << endl;
fin.close();
}
string punch(string word)
{
word.erase(word.size() - 1);
return word;
}
string lowerCase(string x)
{
string CCC = "";
int xlength = x.length();
for (int i = 0; i < xlength; i++)
CCC += tolower(x.at(i));
return CCC;
}
string vowel(string word)
{
char last = word.at(word.length() - 1);
int flag2 = 0;
if (ispunct(last))
{
flag2++;
word = punch(word);
}
word = word + "way";
if (flag2 == 1)
{
word = word + last;
flag2--;
}
word += " ";
return word;
}
string normal(string word){
int x = word.length(), flag2 = 0, flag1 = 0;
char last;
string tempword = "";
if (isupper(word.at(0)))
{
flag1++;
word = lowerCase(word);
}
last = word.at(word.length() - 1);
if (ispunct(last))
{
flag2++;
word = punch(word);
}
int b = 100;
int a = word.find('a'), e = word.find('e'), i = word.find('i'), o = word.find('o'), u = word.find('u');
if (a < b && a != 0 && a != -1)
{
b = a;
//cout << "Found a " << endl;
}
if (e < b && e != 0 && e != -1)
{
b = e;
//cout << "Found e" << endl;
}
if (i < b && i != 0 && i != -1)
{
b = i;
//cout << "Found i" << endl;
}
if (o < b && o != 0 && o != -1)
{
b = o;
//cout << "Found o" << endl;
}
if (u < b && u != 0 && u != -1)
{
b = u;
// cout << "Found u" << endl;
}
for (int x = 0; x < b; x++)
tempword += word.at(x);
word.erase(0, b);
word = word + tempword;
if (flag1 == 1)
{
flag1--;
word.at(0) = toupper(word.at(0));
}
word = word + "ay";
if (flag2 == 1)
{
word = word + last;
flag2--;
}
word = word + " ";
return word;
}