Help Passing Arrays to Functions

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.

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
#include <iostream>
#include <string>


using namespace 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

	



Last edited on
is this a joke?? There are glaring errors in the code, which are really basic and evident!!!
^^^^really?
yes!!!
Please take a look here(last part - Arrays as parameters):

http://www.cplusplus.com/doc/tutorial/arrays/
Last edited on
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.
closed account (z05DSL3A)
anirudh sn wrote:
is this a joke?? There are glaring errors in the code, which are really basic and evident!!!
It would be nice if you helped him with the 'glaring error' then. Don't forget this is the beginner forum.

How To Answer Questions in a Helpful Way
http://www.cplusplus.com/forum/beginner/1/#msg6681


-------=========#=========-------

TheCarpetPissers,

I don't have time to explain it but see if this helps:
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>

void input(std::string * const wordArray, int const arrayCount);
void pigLatinConverter(std::string const * const wordArray, int const arrayCount);


int main()   // <----  int not void
{
    int const numberOfWords = 10;
    std::string words[numberOfWords];

	input(words, numberOfWords);
	pigLatinConverter(words, numberOfWords);
}


void input(std::string * const wordArray, int const arrayCount)
{
    for(int index = 0; index < arrayCount; index++)
    {
        wordArray[index] = "Hello";
    }
} 

void pigLatinConverter(std::string const * const wordArray, int const arrayCount)
{
    for(int index = 0; index < arrayCount; index++)
    {
        std::cout << wordArray[index] << std::endl;
    }
}
Don't worry to much about const
Last edited on
Thanks man. I've been learning c++ on the side for about 5 weeks now so I appreciate the help.

I got the code working, but think it's kind of cheating because I declared the string as a global variable.

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
//This conververts words to Pig Latin

#include <iostream>
#include <string>
using namespace 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 





Topic archived. No new replies allowed.