Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This program converts a poem from a text file
- // into pig-latin.
- #include<iostream>
- #include<string>
- #include<fstream>
- #include<iomanip>
- using namespace std;
- // Function Prototypes
- string ConvertToPigLatin(string);
- //****************
- // Function Main *
- //****************
- int main()
- {
- cout << "\t \"Desiderata\" by Max Ehrmann\n";
- cout << "\t Translated into Pig-Latin\n\n\n\n";
- // Create string variable for input-file name
- const string input_file_name = "PigLatinIn.txt";
- // Create ifstream variable for opening and reading file
- ifstream desiderata_input;
- // Create variable for reading in the poem
- string inStr;
- // Open input file and call Conversion Function to convert input to piglatin
- // Print out converted word in loop
- desiderata_input.open(input_file_name);
- if(!desiderata_input.fail())
- {
- // Create counter for newline variable
- int newline_count = 0;
- cout << "Input file opened successfully.\n\n";
- while(!desiderata_input.eof())
- {
- // Read in input file to inStr variable
- desiderata_input >> inStr;
- // call function to convert word to piglatin
- inStr = ConvertToPigLatin(inStr);
- // output converted word
- cout << inStr;
- cout << " ";
- newline_count++;
- if(newline_count % 5 == 0)
- {
- cout << endl;
- }
- // loop back around while not at end of file
- } // end "while not at end of file" loop
- }else
- {
- cout << "Input file failed to open.\n\n";
- system("pause");
- return 0;
- } // end input validation "if/else"
- // Close the input file
- desiderata_input.close();
- cout << endl << endl;
- system("pause");
- return 0;
- }
- //***********************
- // Conversion Function *
- //***********************
- string ConvertToPigLatin(string inStr)
- {
- // Create string variable for converted word
- string outStr;
- // Append "-way" if word begins with a vowel
- if(inStr.find_first_of('a') == 0 || inStr.find_first_of('e') == 0 || inStr.find_first_of('i') == 0 || inStr.find_first_of('o') == 0 || inStr.find_first_of('u') == 0)
- {
- outStr = inStr.append("-way", 4);
- }else
- {
- string temp1;
- string temp2;
- outStr = inStr;
- for(int i = 0; i < outStr.length(); i++)
- {
- switch(outStr[i])
- {
- case 'a': temp1 = outStr.substr(0, i); temp2 = outStr.substr(i, i++);
- case 'e': temp1 = outStr.substr(0, i); temp2 = outStr.substr(i, i++);
- case 'i': temp1 = outStr.substr(0, i); temp2 = outStr.substr(i, i++);
- case 'o': temp1 = outStr.substr(0, i); temp2 = outStr.substr(i, i++);
- case 'u': temp1 = outStr.substr(0, i); temp2 = outStr.substr(i, i++);
- } // end switch statement
- } // end for loop
- outStr = temp2 + temp1;
- outStr.append("ay", 2);
- } // end if/else
- return outStr;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement