Return multiple values from a function.

Hello, I seem to have hit a stump on my assignment... I need to pass multiple values from one function to another and need a little help on how to do this. 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
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
#include <iostream>
#include <string>
#include <cctype>
#include <fstream>
using namespace std;

//Function Prototypes//
int charString(string, string, string);
string reverseString(string, string, string);
//------------------//

int main()
{
	
}

string charString()
{
	ifstream inFile;

	// Holds the strings for each line.
	string line1;
	string line2; 
	string line3; 
	 // Holds the lenth of each of the lines.
	int length1 = 0; 
	int length2 = 0; 
	int length3 = 0;

	
	inFile.open("testdata.txt"); //Opens test .txt file
	//Gets the full string line.
	getline(inFile, line1);
	getline(inFile, line2);	
	getline(inFile, line3);

	//Gets the strings length.
	
    for(int i = 0; i < line1.length(); i++) //Determines isalpha string length for line 1
    {
    	if(isalpha(line1[i]))
    	{
    		length1++;
		}
	}
	 for(int i = 0; i < line2.length(); i++) //Determines isalpha string length for line 2
    {
    	if(isalpha(line2[i]))
    	{
    		length2++;
		}
	}
	 for(int i = 0; i < line3.length(); i++) //Determines isalpha string length for line 3
    {
    	if(isalpha(line3[i]))
    	{
    		length3++;
		}
	}
	cout << length1 << endl;
	cout << length2 << endl;
	cout << length3 << endl;
	
	return line1, line2, line3;
}

string reverseString() //Trying to pass the values from lines 1-3 here...
{
	
	cout << "Here is the String the correct way." << endl;
	cout << line1 << endl << line2 << endl << line3 << endl;
	cout << "Here is the String backwards." << endl;
	cout << "Nothing here yet :/" << endl;
	
}   

Thanks in advance for any help. I basically need to take line1, line2, and line3 and return them to the reverseString function.
Also, I am not allowed to do anything like make my own classes. I have to stick to the basics and no higher level programming techniques since we have not learned them yet.
Who wrote the function prototypes?
You did pass of three values correctly in prototype on line 9. You just need to make definition on line 67 match it.
What do you mean who wrote the prototypes?

And thanks MiiNiPaa, how exactly would that look on line 67?
What do you mean who wrote the prototypes?
He is asking why line 9 and 67 are different and why you wrote it like that?
Prototypes: Your function declaration MUST match the definition. Lines 8 and 9 must match lines 17 and 67 respectively.

At present they don't match, you have:

8: int charString(string, string, string);
17: string charString()

9: string reverseString(string, string, string);
67: string reverseString()


In line 64 :
return line1, line2, line3;
This is illegal, you can only return one thing from a function.

Finally, I'm not sure what you're trying to accomplish, so I can't comment on the correctness of the solution, but here's your code with some small changes I believe are in keeping with your question and comments.

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

using namespace std;

//Function Prototypes//
void charString(string &, string &, string &);
void reverseString(string &, string &, string &);
void is_alpha_length(const string& line, int &length);
//------------------//

int main()
{
	string x, y, z;
	charString(x, y, z);
	reverseString(x, y, z);
}

// line1, line2 and line3 are passed by reference, so they'll be changed in  the function and you don't need to return anything
void charString(string &line1, string &line2, string &line3 ) 
{
	ifstream inFile;

    // Holds the length of each of the lines.
	int length1 = 0; 
	int length2 = 0; 
	int length3 = 0;

	
	inFile.open("testdata.txt"); //Opens test .txt file
	//Gets the full string line.
	getline(inFile, line1);
	getline(inFile, line2);	
	getline(inFile, line3);
	inFile.close(); // unless you still need it...

	//Gets the strings length.
	
	// use a function instead of repeating the same code thrice
	is_alpha_length(line1, length1);
	is_alpha_length(line2, length2);
	is_alpha_length(line3, length3);
	cout << length1 << endl;
	cout << length2 << endl;
	cout << length3 << endl;
}

void reverseString(string &line1, string &line2, string &line3) //Trying to pass the values from lines 1-3 here...
{
	
	cout << "Here is the String the correct way." << endl;
	cout << line1 << endl << line2 << endl << line3 << endl;
	cout << "Here is the String backwards." << endl;
	cout << "Nothing here yet :/" << endl;
	
}   

// original loop:
//for(int i = 0; i < line1.length(); i++) //Determines isalpha string length for line 1
//{
//	if(isalpha(line1[i]))
//	{
//		length1++;
//	}
//}

inline void is_alpha_length(const string& line, int &length)
{
        // a simpler way for this kind of loop
	for (auto i : line) if (isalpha(i)) ++length;
}
That makes sense tipaye, thank you for your help.
Topic archived. No new replies allowed.