Strings and Pig Latin Translator-> Help Wanted

So I've been attempted to create a pig latin translator focusing mainly with length, strcpy, and strings in general. I have become really confused within creating it, but I think I have the basic structure down. I don't know how to move the first 2 or first letter to the back to create the translated word.
Help?
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
/* Programmering!

*/

#include <conio.h>
#include <iostream.h>
#include <string.h>


main()

{ 
	//Declare variables
		char word1[31]="";
		char newword[31]="";
		int length=0, i=0;

			cout<< "Enter the phrase or word you wish to translate." <<endl;
         cin.get(newword,31);
         cin.ignore(80,'\n');
         length=strlen(newword);  //Get the length!
         			//Now... To translate it
        
         do
         {
               if (newword[0]=='a'||'e'||'i'||'o'||'u')
   				{
					strcpy(newword,"way");
					
                }
               else
               {
						if (newword[0,1]=='ch'||'st'||'sh'||'ph'||'th'||'str'||'tr'||'fr'||'gr'||'qu')
						{
                     	strcpy (newword -- [1,2],[1+2]);
						strcpy (newword,"ay");

						}
						else
						{
							newword[0]
							strcpy (newword,"ay");
						}
                  }
   	} while(i<length-1)
      	{
      	newword[i]=newword[i+1];
         i++;
      	}

    
   

   	cout << newword <<endl;


   }
getch();
return 0;
One of the crazy things about C++ is that you can write code that compiles but does something completely different from what you think it does.....
if (newword[0]=='a'||'e'||'i'||'o'||'u')
This is the same as
if ((newword[0]=='a') || 'e' || 'i' || 'o' || 'u')
'e' is non-zero and thus true. An easy way to see if a character is part of a set is to use strchr():
if (strchr("aeiou", newword[0]))

if (newword[0,1]=='ch'||'st'||'sh'||'ph'||'th'||'str'||'tr'||'fr'||'gr'||'qu')
I'm not sure if this will even compile but it doesn't do what you think. newword[0,1] is the same as newword[(0,1)]. The comma operator evaluates its operands from left to right, throwing away the results as it goes. The value is the value of the right-most operand. So newword[0,1] is the same as newword[1].

strcpy (newword -- [1,2],[1+2]);
This doesn't compile does it? It basically says "decrement newword, then take the 3'rd item of the predecremented value. Throw that away and add 1+2."
Oh, I see, that makes sense.

I'm having trouble in programming the part that makes it so the values 0 and 1 get moves to the back, completely taken away from the front.
okay, okay, I've changed it but there are still some obvious kinks. Any hints on how to orchestrate getting the first two letters of the 'word' into the variable twoletter so I can stop causing errors?


also, my while is illegal, is there a better way to put that?


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
/* Pig latin....?

*/

#include <conio.h>
#include <iostream.h>
#include <string.h>


main()

{
	//Declare variables
		char word[31]="";
		char newword[31]="";
      char twoletter[3]="";
      char quit[2]="";
		int length=0, i=0;

       do
         {

			cout<< "Enter the phrase or word you wish to translate." <<endl;
         cin.get(word,31);
         cin.ignore(80,'\n');
         length=strlen(word);  //Get the length!
         			//Now... To translate it

            if (word[0]=='a'||'e'||'i'||'o'||'u')
   				{
					strcat(word,"way");
               strcpy(newword, word);
               }
               else
               {
               	strcat(twoletter, word[0]);
                  strcat(twoletter, word[1]);

						if (twoletter == 'bl'||'br'||'ch'||'cl'||'cr'||'dr'||'dw'||'fl'||'fr'||'gl'||'gr'||'kl'||'ph'||'pl'||'pr'||'sc'||'sh'||'sk'||'sl'||'sn'||'sm'||'sp'||'sq'||'st'||'sw'||'th'||'tr'||'tw'||'wh'||'wr')
						{
                  		strcat(word, twoletter);
                        strcat(word, "ay");
                        strcpy(newword, word);
						}
						else
						{
                     for(i<length-1; word[i]=word[i+1],i++)
							strcat (word,word[0]);
                     strcat (word, "ay" );
                     strcpy (newword, word);
						}
                  }

            cout<<newword<<endl;
				cout<< "To close this program, enter the word 'quit'."<< endl;
            cin >> quit;
            cin.ignore(80,'\n');
      }while (strcmp(quit,'q')==0);


   }


getch();
return 0;
Here's a short pig Latin translator.
code:
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
#include <iostream>
#include <string>
#include <sstream>
#include <algorithm>
#include <cctype>

int main()
{
	std::cout << "Please enter a sentence: " << std::flush;
	std::string sentence;
	std::getline(std::cin, sentence);


	std::stringstream ss(sentence);
	std::string word;

	while (ss >> word)
	{
		///remove non alphabetic characters
		word.erase(std::remove_if(word.begin(), word.end(), [](char inChar) { return !isalpha(inChar); }), word.end());

		///move first character in word to the end of the word
		word.push_back(word.front());
		word.erase(word.begin());

		///make the entire word uppercase
		std::transform(word.begin(), word.end(), word.begin(), toupper);

		///print word
		std::cout << word << "AY ";
	}


	std::cin.ignore(10000, '\n');
	return 0;
}
Topic archived. No new replies allowed.