English to Pig Latin Program errors

Apr 25, 2020 at 5:27pm
Hello! I am trying to write a program that inputs a short phrase from the user and outputs the Pig Latin conversion of the inputted phrase. I have looked up the error codes on Google, this site, Stackoverflow, Chegg, reread my textbook, and used the suggested fixes in Visual Studios. Every time I fix one error more pop up so now I'm so confused and don't understand how to get the program to work properly. Any help you can provide is much appreciated. Thanks for you time!

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
    /*
  English to Pig Latin Program
  Write a program that converts a sentence input by the user into pig Latin. 
  You can assume that the sentence contains no punctuation. The rules for pig 
  latin are as follows:
  •For words that begin with consonants, move the leading consonant to the end 
   of the word and add "ay". Thus, "ball" becomes "allbay"; "button" becomes 
   "uttonbay"; and so forth.
  •For words that begin with vowels, add "way" to the end. Thus "all" becomes 
   "hallway"; "one" becomes "oneway"; and so forth.
  */
  #include <iostream>
  #include <string>

  using namespace std;
  //Declarations
  const int stringSize = 100;
  string englishPhrase[];
  string PigLatin(string englishPhrase[]);
  string str;

  //English to Pig Latin conversion function
  string PigLatin(string englishPhrase[])
  {
  	  PigLatin(englishPhrase->c_str[stringSize]);
	  int len = englishPhrase.length(); //Length of phrase english phrase 
          entered
	  string replaceString = englishPhrase->c_str.substr(1, len - 1) +     
         englishPhrase[0];
	  bool isVowel = false;
	  if (englishPhrase[0] == "A,E,I,O,U,Y,a,e,i,o,u,y")
         englishPhrase += 'way';
	  else
	  {
		  englishPhrase = replaceString;
	  }

	  for (counter = 1; counter < (len - 1); counter++)
		  if (englishPhrase[0] == "A,E,I,O,U,Y,a,e,i,o,u,y")
		  {
			  isVowel = true;
			  return 0;
		  }
		  else
		  {
			  englishPhrase = replaceString.c_str;
		  }
	  if (!isVowel)
	  {
		  englishPhrase = PigLatin.substr(1, len) + "way";
	  }
	  else
	  {
		  englishPhrase += 'ay';
		  return englishPhrase->c_str;
	  }
  }
  int main()
  {
	  string englishPhrase[stringSize];
	  int i;
	
	  cout << "Enter a short sentence to translate into Pig Latin (enter Q 
          to quit): " << endl;
	  getline(cin, englishPhrase[stringSize]);
	  //Loop for user to quit if not output pig latin conversion of the 
          phrase
	  for (char i = 0; i < stringSize; i++)
	  {
		  while ((englishPhrase[i] != "Q") || (englishPhrase[i] != 
                  "q"))
		  {
			  cout << "The sentence entered is " << englishPhrase 
                          << endl;
			  PigLatin(englishPhrase->c_str[stringSize]);
		  }
		
	  }

	  system("Pause");
	  return 0;
  }
Apr 25, 2020 at 5:56pm
First thing, your PigLatin function returns the converted string. However, in line 75, you discard that returned string and you do not print it out. Nothing will happen there.

Secondly, your PigLatin function takes a string POINTER, not a string. All you need is string englishPhrase, not string englishPhrase[].

Secondly, I suggest you take the original sentence and convert it to lowercase characters, then perform logic on them. You can use std::tolower():
http://www.cplusplus.com/reference/locale/tolower/

Once the string is all in lowercase, all you have to do is determine if the first letter of the string is a vowel or consonant. You could use std::find() with an array of vowels to do so:
http://www.cplusplus.com/reference/algorithm/find/


Or you can just create a function that determines if a character is a vowel:
1
2
3
4
5
6
bool is_vowel(char c)
{

return 
    c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' ? true : false;
}


You can then use this function to determine if it is a vowel by passing in the first character of the string, using englishPhrase[0]. If it is a NOT vowel, you can push the first character to the end and add "ay";

1
2
3
4
5
6
if(!is_vowel(englishPhrase[0]))
{
    englishPhrase.push_back(*englishPhrase.begin());
    englishPhrase.erase(englishPhrase.begin());
    englishPhrase.append("ay");
}


It is similar for the vowels. You can figure out how to implement that.
Apr 25, 2020 at 6:05pm
Look at this snippet:

1
2
3
4
5
6
  using namespace std;
  //Declarations
  const int stringSize = 100;
  string englishPhrase[];
  string PigLatin(string englishPhrase[]);
  string str;


First do you really intend that englishPhrase be an array of string? Remember a C++ string is a dynamic container that will grow to accommodate almost any string size. It looks like you haven't yet made up your mind, in some places you're treating englishPhrase as a string in other places you're treating it like an array of string.

Second in C++ array sizes must be compile time constants and can't be of zero size, and when declaring an array it must have a positive size.

Now to this snippet: englishPhrase += 'ay';

Do you know the difference between a character constant 'a' and a string constant "a"? Do you know that a character constant can only have a single character?

Next: if (englishPhrase[0] == "A,E,I,O,U,Y,a,e,i,o,u,y")
Here you're treating the variable like an array element. Do you really expect that element (a string) to be equal to the string "A,E,I,O,U,Y,a,e,i,o,u,y"?

Next: PigLatin(englishPhrase->c_str[stringSize]);
Here you're treating the variable as a single string, although c_str() is a std::string member function, not some sort of container.

Too many other problems to bother with for now. You really need to learn to compile much more often, after typing as little as one line, and much sooner. Don't type your complete program in one sitting without compiling many, many times.

Edit: You should also get rid of those horrible global variables, you already have at least one variable being define in one function and passed by a parameter into another function along with being globally defined.

Last edited on Apr 25, 2020 at 6:06pm
Apr 25, 2020 at 8:27pm
TheToaster, I really appreciate your insight and links to other posts for reference. I didn't think about converting the sentence into all lower case then using logic to find the vowel/consonant. Thanks again for your help and time!

jlb, I know there where a ton of errors and I appreciate your insight and suggestions on how to fix them. Thank you for your time and help!
Topic archived. No new replies allowed.