returning a struct variable

The pigLatin[i] works fine in the word piglatin function but i can not get it to work in the latinReturn array structure outside the function. Here is my code... anything will 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
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

#include <iostream>
#include <string>
#include <cstring>
#include <cctype>
using namespace std;

struct Word
	{
		string piglatin;
		string english;
	};

Word piglatin(string latin, int &size);

int main() {

int sum=1; // how many words

string pig;

cout <<"Enter a sentence in piglatin to decode";
getline(cin,pig); // get user input

Word latinReturn[sum];
*latinReturn = piglatin(pig,sum); // call to function piglatin

 	 cout << endl << "test out 0:  " << latinReturn[0].piglatin;
	cout << endl << "test out 1:  " << latinReturn[1].piglatin;
	cout << endl << "test out 2:  " << latinReturn[2].piglatin;
	return 0;
}


Word piglatin( string latin, int &size)
{

	for (int i=0; i < latin.length(); i++) // find how many words
	{
	if(latin[i] == ' ')
		{
		size++;
		}
	}
cout<< "size: " <<size <<endl;

		int x=latin.length();
		string temp;
		char ending = latin[x-1];
		int count=0;
		Word pigLatin[size];
		//pigLatin = new Word[size];

		for(int i=0;i < latin.length(); i++)  //lower case
			{
				latin[i] = tolower(latin[i]);
			}

			cout <<endl << "test2:  " << latin;

			for(int i=0; i < latin.length(); i++)  //remove extra
			{
				if(isalpha(latin[i]) || isspace(latin[i]))
				{
					temp += latin[i];
				}
			}

			latin =temp; // make latin into temp
			latin +=ending; //ending char
			cout << "test3: " <<endl << latin;

			for(int i=0; i <latin.length(); i++) //change into struct array
				{
					if(isalpha(latin[i]))
					{
						pigLatin[count].piglatin += latin[i];
					}
					else if (isspace(latin[i])){
						count++;
					}
				}
				cout<<endl <<"test final:" <<pigLatin[1].piglatin;
				cout<<endl <<"test final:" <<pigLatin[0].piglatin;
				cout<< endl << " count is: " << count;
			return *(pigLatin);
}
Your lines 25-26 read essentially:
1
2
Word latinReturn[1];
latinReturn[0] = piglatin(pig,sum);

It is an error to dereference nonexistent elements on lines 29-30.

Your function returns one Word. That is no different from returning one int.

Line 51 is not standard C++. C++ does not support variable length arrays (VLA). C does and some compilers have extension for VLA in C++, but the standard way is to use C++ Standard Library Containers. For example, std::vector does store an array of elements, can be resized dynamically, can be returned from a function as a single object, and does dynamic deallocation for you too. Appropriately.
i see what youre saying but how would i change it to return the whole pigLatin array and not just 1 word of it.
Have a look at this thread, he had same problem.
http://www.cplusplus.com/forum/general/185621/
Topic archived. No new replies allowed.