Advertisement
Guest User

Piglatin pls

a guest
Apr 15th, 2014
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.12 KB | None | 0 0
  1. // This program converts a poem from a text file
  2. // into pig-latin.
  3.  
  4. #include<iostream>
  5. #include<string>
  6. #include<fstream>
  7. #include<iomanip>
  8.  
  9. using namespace std;
  10.  
  11.  
  12. // Function Prototypes
  13. string ConvertToPigLatin(string);
  14.  
  15.  
  16.  
  17.  
  18. //****************
  19. // Function Main *
  20. //****************
  21.  
  22. int main()
  23. {
  24.    cout << "\t \"Desiderata\" by Max Ehrmann\n";
  25.    cout << "\t Translated into Pig-Latin\n\n\n\n";
  26.  
  27.    
  28.  
  29.    // Create string variable for input-file name
  30.    const string input_file_name = "PigLatinIn.txt";
  31.  
  32.    // Create ifstream variable for opening and reading file
  33.    ifstream desiderata_input;
  34.  
  35.    // Create variable for reading in the poem
  36.    string inStr;
  37.  
  38.    
  39.    // Open input file and call Conversion Function to convert input to piglatin
  40.    // Print out converted word in loop
  41.    desiderata_input.open(input_file_name);
  42.    if(!desiderata_input.fail())
  43.    {
  44.       // Create counter for newline variable
  45.          int newline_count = 0;
  46.       cout << "Input file opened successfully.\n\n";
  47.       while(!desiderata_input.eof())
  48.       {
  49.          
  50.  
  51.          // Read in input file to inStr variable
  52.          desiderata_input >> inStr;
  53.  
  54.          // call function to convert word to piglatin
  55.          inStr = ConvertToPigLatin(inStr);
  56.          
  57.          // output converted word
  58.          cout << inStr;
  59.          cout << " ";
  60.  
  61.          newline_count++;
  62.          if(newline_count % 5 == 0)
  63.          {
  64.             cout << endl;
  65.          }
  66.  
  67.          
  68.          
  69.          // loop back around while not at end of file
  70.       } // end "while not at end of file" loop
  71.    }else
  72.    {
  73.       cout << "Input file failed to open.\n\n";
  74.       system("pause");
  75.       return 0;
  76.    } // end input validation "if/else"
  77.  
  78.   // Close the input file
  79.   desiderata_input.close();
  80.  
  81.    cout << endl << endl;
  82.    system("pause");
  83.    return 0;
  84. }
  85.  
  86.  
  87.  
  88.  
  89. //***********************
  90. // Conversion Function  *
  91. //***********************
  92.  
  93. string ConvertToPigLatin(string inStr)
  94. {
  95.    // Create string variable for converted word
  96.    string outStr;
  97.  
  98.  
  99.  
  100.   // Append "-way" if word begins with a vowel
  101.   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)
  102.   {
  103.          
  104.       outStr = inStr.append("-way", 4);
  105.      
  106.  
  107.   }else
  108.   {
  109.      string temp1;
  110.      string temp2;
  111.  
  112.      outStr = inStr;
  113.  
  114.      for(int i = 0; i < outStr.length(); i++)
  115.      {
  116.        
  117.  
  118.         switch(outStr[i])
  119.         {
  120.         case 'a': temp1 = outStr.substr(0, i); temp2 = outStr.substr(i, i++);
  121.         case 'e': temp1 = outStr.substr(0, i); temp2 = outStr.substr(i, i++);
  122.         case 'i': temp1 = outStr.substr(0, i); temp2 = outStr.substr(i, i++);
  123.         case 'o': temp1 = outStr.substr(0, i); temp2 = outStr.substr(i, i++);
  124.         case 'u': temp1 = outStr.substr(0, i); temp2 = outStr.substr(i, i++);
  125.         } // end switch statement
  126.  
  127.  
  128.        
  129.      } // end for loop
  130.      
  131.      outStr = temp2 + temp1;
  132.      outStr.append("ay", 2);
  133.      
  134.      
  135.   } // end if/else
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142.  
  143.   return outStr;
  144.  
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement