Pig latin Please help

Using this code i have to create a Pig Latin Converter program that will read in a file called "pigLatinFile.in" of English sentences, convert them into Pig Latin, and will output them back to a file called "pigLatinFile.out". Also the comma (,) and period(.) characters will be considered special punctuation characters, and even after conversion to Pig Latin, these characters will need to still appear at the end of each word. All other characters can be left where Malik's "rotate" function puts them in the word.If the first letter of the English word is capitalized, then the first letter of the word converted into Pig Latin should also be capitalized

#include <iostream>
#include <string>

using namespace std;

bool isVowel(char ch);
string rotate(string pStr);
string pigLatinString(string pStr);

int main()
{
string str;

cout << "Enter a string: ";
cin >> str;
cout << endl;

cout << "The pig Latin form of " << str << " is: "
<< pigLatinString(str) << 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])) //Step 1
pStr = pStr + "-yay";
else //Step 2
{
pStr = pStr + '-';
pStr = rotate(pStr); //Step 3

len = pStr.length(); //Step 3.a
foundVowel = false; //Step 3.b

for (counter = 1; counter < len - 1;
counter++) //Step 3.d
if (isVowel(pStr[0]))
{
foundVowel = true;
break;
}
else //Step 3.c
pStr = rotate(pStr);

if (!foundVowel) //Step 4
pStr = pStr.substr(1, len) + "-way";
else
pStr = pStr + "ay";
}

return pStr; //Step 5
}
I cant figure out where to put the in file at.
If you're using an IDE, it'll most likely need to go in the same folder "main.cpp" is in. If you're just running the executable, it should be in the folder where the executable is.
So where would i write in the code at on the program?
1
2
3
4
5
6
7
8
9
std::ifstream in("infile.txt");
std::string word;

while (in >> word) {
  //Convert
  //Save to .out file
}

in.close();

Read from the file instead of using cin
So like this? if so i am getting an error saying a function-definition is not allowed here before { token

bool isVowel(char ch)
{
switch
{
/////////////////////////////////////
////////////////////////////////////
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

bool isVowel(char ch);
string rotate(string pStr);
string pigLatinString(string pStr);

int main()
{
ifstream inData;
ofstream outData;

inData.open("plFilein");
outData.open("plFileout");
{
string str;

cout << inData << endl;

cout << "The pig Latin form of " << str << " is: "
<< pigLatinString(str) << endl;

return 0;
}


bool isVowel(char charater)
{
switch (charater)
{
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])) //Step 1
pStr = pStr + "-yay";
else //Step 2
{
pStr = pStr + '-';
pStr = rotate(pStr); //Step 3

len = pStr.length(); //Step 3.a
foundVowel = false; //Step 3.b

for (counter = 1; counter < len - 1;
counter++) //Step 3.d
if (isVowel(pStr[0]))
{
foundVowel = true;
break;
}
else //Step 3.c
pStr = rotate(pStr);

if (!foundVowel) //Step 4
pStr = pStr.substr(1, len) + "-way";
else
pStr = pStr + "ay";
}

cout << stringout << endl;
cin >> stringout;

stringin.close();
stringout.close();


return pStr;
}


1
2
3
4
5
6
bool isVowel(char ch)
{
switch
{
/////////////////////////////////////
//////////////////////////////////// 

Where is the rest of that? It's cut off, you don't end it anywhere.

Edit: Please, for the love of GOD use the code tags. It makes it SO much easier to read.
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
bool isVowel(char ch)
{
  switch
  {
    /////////////////////////////////////
    ////////////////////////////////////
#include <iostream>
#include <string>
#include <fstream>

    using namespace std;

    bool isVowel(char ch);
    string rotate(string pStr);
    string pigLatinString(string pStr);

    int main()
    {
      ifstream inData;
      ofstream outData;

      inData.open("plFilein");
      outData.open("plFileout");
      {
        string str;

        cout << inData << endl;

        cout << "The pig Latin form of " << str << " is: "
          << pigLatinString(str) << endl;

        return 0;
      }


      bool isVowel(char charater)
      {
        switch (charater)
        {
          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])) //Step 1
          pStr = pStr + "-yay";
        else //Step 2
        {
          pStr = pStr + '-';
          pStr = rotate(pStr); //Step 3

          len = pStr.length(); //Step 3.a
          foundVowel = false; //Step 3.b

          for (counter = 1; counter < len - 1;
               counter++) //Step 3.d
            if (isVowel(pStr[0]))
            {
              foundVowel = true;
              break;
            }
            else //Step 3.c
              pStr = rotate(pStr);

          if (!foundVowel) //Step 4
            pStr = pStr.substr(1, len) + "-way";
          else
            pStr = pStr + "ay";
        }

        cout << stringout << endl;
        cin >> stringout;

        stringin.close();
        stringout.close();


        return pStr;
      }
Last edited on
Line 37 there is a error saying a function-definition is not allowed here before { token
Right, I saw that. Your syntax isn't correct.

You can't have a function definition INSIDE main. Move it outside of main.
Last edited on
So i would move main below the rest of the code?
Get rid of the extra brace on line 24.
So i've got my program to work,but now i cant figure out how to get the converted pig latin to output to my output file...


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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <iostream>
#include <string>
#include <fstream>


using namespace std;

bool isVowel(char);
string rotate(string);
string plString(string);

int main()
{ 

	string str;
	ifstream infile;
	ofstream outfile;

	infile.open("input.txt");
	outfile.open("output.txt");

	infile >> str;
	while(infile)
	{
		cout << plString(str) << " ";
		infile >> str;
	}
	cout << endl;
	system("pause");
	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 str)
{
    string::size_type len = str.length();

    string str1;

    str1 = str.substr(1, len - 1) + str[0];

    return str1;
}

string plString(string str)
{
    string::size_type len;

    bool Vowel;

    string::size_type counter;

    if (isVowel(str[0]))                       //Step 1
        str = str + "-yay";
    else                                        //Step 2
    {
        str = str + '-';
        str = rotate(str);                    //Step 3

        len = str.length();                    //Step 3.a
        Vowel = false;                     //Step 3.b

        for (counter = 1; counter < len - 1;
                          counter++)            //Step 3.d
            if (isVowel(str[0]))
            {
               Vowel = true;
                break;
            }
            else                                //Step 3.c
                str = rotate(str);

        if (!Vowel)                        //Step 4
            str = str.substr(1, len) + "-way";
        else
            str = str + "ay";
    }

    return str;           
	

	


}
Use outfile << instead of cout <<
I changed that on line 28. When i look at the output file there is no spaces between the words
Why do you have two threads for the same problem?
http://www.cplusplus.com/forum/beginner/197566/

PLEASE DO NOT START MULTIPLE THREADS FOR THE SAME PROBLEM.


Topic archived. No new replies allowed.