Need Help Writing a Pig Latin Translator from reading in a file

I have a file that contains a bunch of sentences in it in English, and I need to be able to ifstream all of that and take every word and cout to the console everything converted in piglatin. Can somebody lead me in the right direction?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <fstream>
#include <iostream>
#include <string>

// Everything we will be using during the program
using std::ifstream;
using std::cout;
using std::cerr;
using std::string;
using std::cin;

int main()
{

    // Process of opening the text document
    ifstream infile;
    infile.open("ASSGN8-A.txt");

    // Checking to see if the file is open or not.
    if (!infile)
    {
        cerr << "ERROR. FILE NOT OPEN"; // Error report
    }

    string text;

    while (infile >> text)
    {

    }

    return 0;
}

string toPigLatin()
{

}

Ok, so you are currently reading each word in your text file. So now you need to make it so you can pass a string to your toPigLatin function, figure out how you're going to convert it to pig latin and add a return statement, call the function form in your loop, and add an output to print to the screen. Check here: http://www.cplusplus.com/reference/string/string/ for the functions you'll probably use to change the word to pig latin.

Also, your missing your infile.close() statement and theirs no need to run the rest of the program if the file fails to open so you would have an else statement after you if with the rest of the program logic and the close statement.

1 more, your missing function prototype.

Simple changes commented:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
//function prototype
string toPigLatin(string);

int main()
{

	// Process of opening the text document
	ifstream infile;
	infile.open("ASSGN8-A.txt");

	// Checking to see if the file is open or not.
	if (!infile)
	{
		cerr << "ERROR. FILE NOT OPEN"; // Error report
	}
	//no need to run program logic if file fails to open
	else {

		string text;
		string pigLatin;

		while (infile >> text)
		{
			//append returned string to piglatin string
		}
		//output result
		cout << pigLatin << endl;

		//close file
		infile.close();
	}

	return 0;
}

//need to pass is word to convert
string toPigLatin(string text)
{

	//Figure out how to convert string to pig latin

	//need to return string
	return text;
}
Last edited on
Topic archived. No new replies allowed.