Here is my problem:
Write a program that reads in a text file and converts it to pig Latin. i.e. if the first letter is a consonant, move it to the end and add “ay” to the end, if it is a vowel add “way” to the end. Here’s an example: “end the war” becomes “endway hetay arway”.
Here is my code at the moment:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
bool isVowel(char ch);
string rotate(string pStr);
string pigLatinString(string pStr);
int main()
{
ifstream infile;
string newstr;
int length;
char achar;
string str;
string str1;
string str2;
string str3;
infile.open("c:\\PigLatin.txt");
cout << "Enter a string: ";
getline(cin, str1);
cout << endl;
cout << "Pig Latin String Is: " << endl;
cout << endl;
while (!infile.eof())
{
infile.get(achar);
if (achar != ' ')
{
str = str + achar;
}
else
{
cout << pigLatinString(str)<< " ";
str = "";
}
}
if (achar == '!')
{
length = static_cast<unsigned int>(str.length());
newstr = str.substr(0,(length-2));
cout << " " << pigLatinString(newstr) << "!";
}
if (achar == '?')
{
length = static_cast<unsigned int>(str.length());
newstr = str.substr(0,(length-2));
cout << " " << pigLatinString(newstr) << "?";
}
if (achar == '.')
{
length = static_cast<unsigned int>(str.length());
newstr = str.substr(0,(length-2));
cout << " " << pigLatinString(newstr) << ".";
}
cout << endl;
str = "";
cout << endl;
return 0;
}
bool isVowel(char ch)
{
switch (ch)
{
case 'A': case 'E':
case 'I': case 'O':
case 'U': case 'Y':
case 'a': case 'e':
case 'i': case 'o':
case 'u': case 'y': return true;
default: return false;
}
}
string rotate(string pStr)
{
string::size_type len = pStr.length();
string rStr;
rStr = pStr.substr(1, len - 1) + pStr[0];
return rStr;
}
string pigLatinString(string pStr)
{
string::size_type len;
bool foundVowel;
string::size_type counter;
if (isVowel(pStr[0]))
pStr = pStr + "-way";
else
{
pStr = pStr + '-';
pStr = rotate(pStr);
len = pStr.length();
foundVowel = false;
for (counter = 1; counter < len - 1; counter++)
if (isVowel(pStr[0]))
{
foundVowel = true;
break;
}
else
pStr = rotate(pStr);
if (!foundVowel)
pStr = pStr.substr(1,len) + "-way";
else
pStr = pStr + "ay";
}
return pStr;
}
My problem is im really blanking on how to read from the file and get my program to read it and output it in the command prompt. So if you can help me figure that out thanks!