A Conversion Program

Hi there, I have to make a program for my C++ class and it's due tomorrow. Here is what the code should do:

Create a program that allows the user to enter a word. The program should display the word in pig latin form.Here are the conversions:

a.) When the word begins with a vowel(A, E, I, O, or U), add the string "-way" to the end of the word. Ex: ant becomes ant-way.

b.) When the word doesn't begin with a vowel, first add a dash to the end of the word. Then continue moving the first character in the word to the end of the word until the first letter is A, E, I, O, U, or Y. Then add the string "ay" to the end of the word. Ex: Chair becomes air-Chay.

c.) When the word doesn't contain the letter A, E, I, O, or Y, add the string "-way"to the end of the word. Ex: 56 becomes 56-way.
Similar to part A.

Here is my code so far:

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
#include <iostream>
#include <string>
using namespace std;
int main()
{
    string Word;
    string NewWord;
    string NewWord2;
   
    cout << "Enter a word: ";
    cin >> Word;
   
    for(int x = 0; x <= Word.length(); x++)
    {
            if(Word.substr(x, 1) == "a" || Word.substr(x, 1) == "e" || Word.substr(x, 1) == "i" || Word.substr(x, 1) == "o" || Word.substr(x, 1) == "u")
            {
                              NewWord = Word + "-way";
                              cout << NewWord << endl;
            }
    }
   
    for(int y = Word.length(); y = 0; y++)
    {
            if(Word.substr(1, y) != "a" || Word.substr(1, y) != "e" || Word.substr(1, y) != "i" || Word.substr(1, y) != "o" || Word.substr(1, y) != "u")
            {
                    NewWord2 = Word + "ay";
                    cout <<  "-" << NewWord2 << endl;
            }
    }
    system("PAUSE");
    return 0;
}
Last edited on
Can someone please help D:

Sorry for bumping this up but I really need help.
You have very explicit instructions.

first: check if the first letter is a vowel

if (mystring[0] == /*insert vowel here (would recommend function)*/ )

second: if that's false, check if the word has any vowels at all

1
2
3
for (int temp = 0; temp < mystring.size(); temp++)
 if (mystring[temp] == /*insert vowel here (would recommend function)*/ )
  //oh we must have a vowel, go to the step where we take care of that 


Third: if everything else if false, add -way.
i had a problem similair to this that i remember from a few years back. try reading the word entered into a char array and then assigning a pointer to that array, so you can then just continue adding values to the pointer to get the next letter until you find a vowel
@ascii How would you do that?

@ultifinitus Thanks for the help.
haha while i was working on it i realized it wouldnt work. the concept works, but i couldnt find a good way to read a string into a char array. new piece of advice though xD look up the string.find() method
What if I put the if statements under the same for loop? Like this:

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
#include <iostream>
#include <string>
using namespace std;
int main()
{
    string Word;
    string NewWord;
    string NewWord2;
   
    cout << "Enter a word: ";
    cin >> Word;
   
    for(int x = 0; x <= Word.length(); x++)
    {
            if(Word.substr(x, 1) == "a" || Word.substr(x, 1) == "e" || Word.substr(x, 1) == "i" || Word.substr(x, 1) == "o" || Word.substr(x, 1) == "u")
            {
                              NewWord = Word + "-way";
                              cout << NewWord << endl;
            }
if(Word.substr(1, y) != "a" && Word.substr(1, y) != "e" && Word.substr(1, y) != "i" && Word.substr(1, y) != "o" && Word.substr(1, y) != "u")
            {
                    NewWord2 = Word + "ay";
                    cout <<  "-" << NewWord2 << endl;
            }
    }
   
    system("PAUSE");
    return 0;
}
It kinda spazes out with the update.
Anyone know a better way?
try it my way ;)
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
#include <iostream>
#include <string>

using namespace std;

string translate(string text, int firstVowel)
{
	text += "-";
	while (firstVowel != 0)
	{
		text += text.substr(0, 1);
		text = text.substr(1);
		firstVowel--;
	}
	text += "way";
	return text;
}

int main()
{
	string word;
	cout << "enter the word to translate to piglatin: ";
	cin >> word;
	word += "aeiou";

	int vowels[] = { word.find("a"), word.find("e"), word.find("i"), word.find("o"), word.find("u") };

	word = word.substr(0, word.length() - 5);

	int greaterThan = 0;
	int firstVowel;

	for each (int i in vowels)
	{
		greaterThan = 0;
		for each(int y in vowels)
		{
			if (i < y)
			{
				greaterThan++;
				if (greaterThan == 4)
				{
					firstVowel = i;
				}
			}
		}
	}

	if (firstVowel == 0 || firstVowel >= word.length())
	{
		word += "-way";
	}
	else
	{
		word = translate(word, firstVowel);
	}

	cout << "The piglatin form is: " << word << endl;

	system("pause");
	return 0;
}
Last edited on
The code's giving me errors on lines 33, 34, and 48
@ascii: you're very good at giving code, how about some actual explanations for these poor poor people =)

IMHO giving code won't do nearly as much good as explaining (explicitly or implicitly) how to do something.
okay explanation time then :)
obviously the beginning is simple im just getting the word from the user and storing it in the variable "word". then in the array vowels i store the location of every single vowel in the word. (note that i added aeiou to the string before the search because if the string.find() method cant find something it gives me an error, and it doesnt make a difference in the end because i remove the aeiou after i conduct the search)

then it gets a little more complicated with a bunch of nested loops. basically the idea behind these loops was to find which of the vowels occurs first because we just want the location of the first vowel. i basically just create a foreach loop within a foreach loop. and then test each vowels location against every other vowels location. for every vowel the vowel being test is greater than it increments the greatherThan variable. if the greaterThan variable reaches 4 it means the vowel being tested has the lowest value out of all of the other vowels thus meaning it occurs first. i assign this value to the variable firstVowel.

now all thats left is to add the extensions to the word. the first two are simple, i just need to add -way if the first letter is a vowel or there are no vowels. if the first letter is a vowel than firstVowel will equal 0, and then i just have to add -way. if there are no vowels than firstVowel will be greater than the strings length, and then once again i just have to add way.

if the first vowel is in the middle of the word it becomes a little bit trickier so i gave it its own function. the function itself is simple. it has two arguments: the word itself, and where the first vowel in this word occurs. when the variable firstVowel is equal to 0 it means that the first letter is now a vowel. so i just need to keep adding the first letter to the end of the word using substrings. i use the substr method like follows:

add the first letter to the end of the word so that for instance Chair becomes:
chair-c
then remove the first letter by making the word a substring from the SECOND letter to the end of the word so it becomes:
hair-c

everytime the above process is executed it subtracts one from the firstVowel variable. it finally returns the new word. then it just gets outputted to the screen.

hopefully this makes more sense now, ask me if you have any questions and ill be happy to answer them :-)
ascii++ (&& tldr) =)
I made the code for you.. with comments that explain what happen in that part.. i think that i will help you..

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
//Conversion word into pig latin form
#include <iostream>
#include <string>

using namespace std;

int main()
{
	string word;
	int temp; //temp is to save the vowel's positin in the word


	cout<<"Enter a word: ";
	cin>>word;

    //See if fist letter of word starts with vowel
	if(word[0]=='a' || word[0]=='i' || word[0]=='o' || word[0]=='u' || word[0]=='e')
    {
        word=word + "-way"; //put '-way' to the end of the word
        cout<<word<<endl;//printing the word
    }
    //if not vowel then program goes here
    else
    {   //See if the first letter of word is char
        if(isalpha(word[0]))
        {
            word=word + "-";//fist put '-' to the end of the word
            //Searcing where is vowel
            for(int i=0;i<word.length();i++)
            {
                if(word[i]=='a' || word[i]=='i' || word[i]=='o' || word[i]=='u' || word[i]=='e')
                {
                    temp=i;//take the index where it found the vowel
                    break; //stops the loop
                }
            }
            int i=0;
            //moving the first char to the end of the word until temp is reached
            while(i<temp)
            {
                word=word + word[i]; //putting the first char to the end
                word[i]=' ';//putting space when the first char is taken
                i++; //increasing i
            }
            word=word + "ay";
            //printing the word from 'temp' position
            //it is like that because from 0 to 'temp' all that
            //are spaces
            for(i=temp;i<word.length();i++) cout<<word[i];
            cout<<endl;//printing newline
        }
        //If fist letter is not char then see if first letter is number
        else if(isalnum(word[0]))
        {
            word=word + "-way"; //put '-way' to the end of word
            cout<<word<<endl; //printing the word
        }

    }

	return 0;
}
Last edited on
haha both of our versions are exactly 62 lines so its just up to efficiency to see whos is better -_-

and seriously i put like 10 minutes into that dont tell me you didnt read it!!!
Last edited on
@ascii my code is very good for beginners,they can easily understand.. and i think that in your code doesn't have the exaple with: Chair -> air-Chay.. so excuse me if i am wrong :-)
it does work, smd.

http://img810.imageshack.us/i/suckit.png/

however yours probably is more other-coder-reading-it friendly
Last edited on
@ascii: I read it =)
Topic archived. No new replies allowed.