Guys, this is kicking my butt. I'm having a rough time learning passing to functions. Right now here's the situation....
I want to enter the string array word[10] in the input() function and then work on word[10] in pigLatinConverter(). I also need the int hold - which is defined in input() - in pigLatinConverter() as well. If I put everything in one function it works perfectly; so there are no logical errors. It's just that I am seriously confused about passing variables / arrays. Help is deeply appreciated.
#include <iostream>
#include <string>
usingnamespace std;
int input();
void pigLatinConverter(int, string word[], 10);
void main()
{
input();
pigLatinConverter(int hold, string word[]);
}
int input()
{
int x,z; // used for looping
int hold; // used for figuring out where the string stopped
string word[10];
cout << "\n\nPlease enter a phrase up to 10 words to convert to Pig Latin (put a period at the end of your word and hit [ENTER] to stop): ";
for(x = 0 ; x < 10 ; x++)
{
cin >> word[x];
z=0;
while(word[x][z] != '\0') // This runs through each word that is typed in - if the word has a period at the end it breaks off the input loop
{
if(word[x][z] == '.')
{
word[x][z] = '\0'; // removes the period
hold = x; // hold can be used later - it marks how many words there are in the array
x = 10; // kills the loop
break;
} // End IF
word[x][z] = tolower(word[x][z]);
z++;
}//end WHILE Loop
} // End input FOR loop
return hold;
} // end of FUNCTION
void pigLatinConverter(int hold, string word, 10)
{
cout << "\n\n\n";
int x, y, z;
for(x = 0; x < hold+1; x++) // up to hold because hold marks where the user quit inputting
{
y=0;
while(word[x][y] != '\0') // send y out to the end of the word then marks the place - you need it in the ELSE statement
{
y++;
}//end WHILE
if(word[x][0] == 'a' || word[x][0] == 'e' || word[x][0] == 'i' || word[x][0] == 'o' || word[x][0] == 'u') // vowel detection
{
cout << word[x] << "-yay ";
}// end IF
else // The WHILE loop checks to see if the first letter is a consonant and runs the sort - this makes it work even for words with consonant clusters
{
while(word[x][0] != 'a' && word[x][0] != 'e' && word[x][0] != 'i' && word[x][0] != 'o' && word[x][0] != 'u')
{
for(z = 0; z < y-1; z++)
{
swap(word[x][z], word[x][z+1]); // moves the first letter to the end
} // end FOR
} // end WHILE
cout << word[x] << "ay" << " ";
} // end ELSE
} // end FOR
system("pause");
}//end of function
forget all those things, he should learn very basics:
1)how to declare a variable, how to use it after declaration.
2)scope of variables.
3)function declaration,definition, invocation.
//This conververts words to Pig Latin
#include <iostream>
#include <string>
usingnamespace std;
string word[10];
int input();
void pigLatinConverter(int, string word[], int);
void main()
{
pigLatinConverter(input(), word,10);
}
int input()
{
int x,z; // used for looping
int hold; // used for figuring out where the string stopped
cout << "\n\nPlease enter a phrase up to 10 words to convert to Pig Latin (put a period at the end of your word and hit [ENTER] to stop): ";
for(x = 0 ; x < 10 ; x++)
{
cin >> word[x];
z=0;
while(word[x][z] != '\0') // This runs through each word that is typed in - if the word has a period at the end it breaks off the input loop
{
if(word[x][z] == '.')
{
word[x][z] = '\0'; // removes the period
hold = x; // hold can be used later - it marks how many words there are in the array
x = 10; // kills the loop
break;
} // End IF
word[x][z] = tolower(word[x][z]);
z++;
}//End While Loop
} // End input FOR loop
return hold;
} // end of FUNCTION
void pigLatinConverter(int hold, string word[], int hold1)
{
cout << "\n\n\n";
int x, y, z;
for(x = 0; x < hold+1; x++) // up to hold because hold marks where the user quit inputting
{
y=0;
while(word[x][y] != '\0') // send y out to the end of the word then marks the place - you need it in the ELSE statement
{
y++;
}//end WHILE
if(word[x][0] == 'a' || word[x][0] == 'e' || word[x][0] == 'i' || word[x][0] == 'o' || word[x][0] == 'u') // vowel detection
{
cout << word[x] << "-yay ";
}// end IF
else // The WHILE loop checks to see if the first letter is a consonant and runs the sort - this makes it work even for words with consonant clusters
{
while(word[x][0] != 'a' && word[x][0] != 'e' && word[x][0] != 'i' && word[x][0] != 'o' && word[x][0] != 'u')
{
for(z = 0; z < y-1; z++)
{
swap(word[x][z], word[x][z+1]); // moves the first letter to the end
} // end FOR
} // end WHILE
cout << word[x] << "ay" << " ";
} // end ELSE
} // end FOR
system("pause");
}//end of function