The purpose of this assignment is to help gauge your skills in writing small pro
ID: 3855091 • Letter: T
Question
The purpose of this assignment is to help gauge your skills in writing small programs that involve vectors and/or c-string/string arrays. The program also contains functions and may perform input, output, files and file processing, flow of control, and/or calculations. Please note that this assignment indicates precisely what your program should accomplish, without a precise indication of how the program works. Part of your assignment is designing the techniques of how the program works.
PROGRAM SPECIFICATION
Write a program that reads an input file of sentences and converts each word to “Pig Latin.” In this program’s version, to convert a word to Pig Latin, you should remove the first letter and place that letter at the end of the word. Then you append the string “ay” to the word.
Here is an example:
English: I SLEPT MOST OF THE NIGHT
Pig Latin: IAY LEPTSAY OSTMAY FOAY HETAY IGHTNAY
Here is the txt file:
I SLEPT MOST OF THE NIGHT
THE CAT IN THE HAT IS BACK
EVERYONE LOVES PROGRAMMING
MY BEST DAYS ARE AHEAD OF ME
I REALLY LIKE PIG LATIN
I CAME, I SAW, I CONQUERED
THEY HEARD FIRSTHAND ACCOUNTS OF THE INCA EMPIRE
JUMP
-=
SPORTS ARE THE MOST WATCHED PROGRAMS ON T.V.
WHAT'S THE DEAL WITH AIRLINE PEANUTS?
This is the program I have. Currently the end of file wont work.
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
int main()
{
using std::cout;
using std::cin;
using std::endl;
using std::fstream;
std::string sentence,
userFile,
word,
pig;
int index = 0,
limit;
std::ifstream inFile;
cout << "What is the file name? " << endl;
getline(cin, userFile);
inFile.open(userFile);
if(!inFile)
{
cout << "Sorry, that file does not work." << endl;
cin.get();
}
else
{
while (!inFile.eof())
{
for(int i = 0; i <= limit; ++i)
{
getline(inFile, sentence);
cout << sentence << endl;
if(sentence[i] == ' ' || limit == i)
{
word = sentence.substr(index, (i - index));
word.erase(0,1);
pig = pig + word + sentence.substr(index, 1) + "ay ";
cout << endl;
index = i + 1;
}
}
}
}
cout << "The new file says: " << pig << endl;
inFile.close();
return 0;
}
Explanation / Answer
There is problem in the logic provided by yourself.
Also you can modularise the code as below.
CODE:
------
#include <iostream>
#include <string>
#include<fstream>
#include <sstream>
using namespace std;
// converts the input string to PigLatin way.
string to_piglatin(const string& input)
{
string firstChar = input.substr(0,1);
string restChar = input.substr(1, input.size()-1);
return restChar + firstChar + "ay";
}
int main(int argc, char* argv[])
{
string str;
ifstream myfile;
// input.txt file contains the input.
myfile.open("input.txt");
string line;
std::string word;
// check if the file is open
if (myfile.is_open()){
// read line by line
while ( getline (myfile,line) ){
// convert the line to linestream to split the line to words.
std::stringstream linestream(line);
// pass each word to the to_piglatin function.
while(linestream >> word){
cout << to_piglatin(word) << " ";
}
cout << endl;
}
myfile.close();
}
return 0;
}
input.txt
-------------------
I SLEPT MOST OF THE NIGHT
THE CAT IN THE HAT IS BACK
EVERYONE LOVES PROGRAMMING
MY BEST DAYS ARE AHEAD OF ME
I REALLY LIKE PIG LATIN
I CAME, I SAW, I CONQUERED
THEY HEARD FIRSTHAND ACCOUNTS OF THE INCA EMPIRE
JUMP
-=
SPORTS ARE THE MOST WATCHED PROGRAMS ON T.V.
WHAT'S THE DEAL WITH AIRLINE PEANUTS?
OUTPUT:
$g++ piglatin.cpp
$./a.out
Iay LEPTSay OSTMay FOay HETay IGHTNay
HETay ATCay NIay HETay ATHay SIay ACKBay
VERYONEEay OVESLay ROGRAMMINGPay
YMay ESTBay AYSDay REAay HEADAay FOay EMay
Iay EALLYRay IKELay IGPay ATINLay
Iay AME,Cay Iay AW,Say Iay ONQUEREDCay
HEYTay EARDHay IRSTHANDFay CCOUNTSAay FOay HETay NCAIay MPIREEay
UMPJay
=-ay
PORTSSay REAay HETay OSTMay ATCHEDWay ROGRAMSPay NOay .V.Tay
HAT'SWay HETay EALDay ITHWay IRLINEAay EANUTS?Pay
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.