Function to Return Multiple Datatypes

OK the main question is how to make a function that returns different data types.

A minor question is the warnings im getting for getch(); below.

Errors:
1>------ Build started: Project: projecttwo, Configuration: Debug Win32 ------
1>  projecttwo.cpp

1>c:\users\t_dub\documents\visual studio 2010\projects\projecttwo\projecttwo\projecttwo.cpp(28):
warning C4996: 'getch': The POSIX name for this item is deprecated. Instead, use the
ISO C++ conformant name: _getch. See online help for details.
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\conio.h(128) :
see declaration of 'getch'

1>c:\users\t_dub\documents\visual studio 2010\projects\projecttwo\projecttwo\projecttwo.cpp(119):
warning C4996: 'getch': The POSIX name for this item is deprecated. Instead, use the
ISO C++ conformant name: _getch. See online help for details.
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\conio.h(128) :
see declaration of 'getch'

1>c:\users\t_dub\documents\visual studio 2010\projects\projecttwo\projecttwo\projecttwo.cpp(120):
error C2561: 'gamePieceMover' : function must return a value
1>          c:\users\t_dub\documents\visual studio 2010\projects\projecttwo\projecttwo
\projecttwo.cpp(15) :
see declaration of 'gamePieceMover'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include <fstream>
#include <string>
#include <iostream>
#include <stdio.h>
#include <conio.h>
#include <iomanip>
using namespace std;

char horzLocation ='A';
int vertLocation = 2;
int invalidMoves = 0;
string direction;
int numOfSpaces;

int gamePieceMover
(char &horzL, int &vertL, int &invalidMoves, string direction, int numOfSpaces);

int main()
{
	int moveAttempt = 0;
	string line;
	ifstream intData;
	string startPoint;
	intData.open("C:/Users/T_Dub/Desktop/data.txt");

	if(!intData)
	{
		cout<<"Error opening file, the program will close.\n\n";
		getch();
		return 0;
	}

	while( !intData.eof() )
	{
		getline(intData, line);
        moveAttempt++;
	}
	intData.close();

	intData.open("C:/Users/T_Dub/Desktop/data.txt");
	while ( !( intData.eof() ) )
	{
		intData >> direction >> numOfSpaces;
		cout << direction << "  " << numOfSpaces << endl;
		cout << gamePieceMover (horzLocation, vertLocation, invalidMoves, direction,
                        numOfSpaces) << endl << endl;
	}

	getchar();
	intData.close();
	return 0;
}

int gamePieceMover(char &horzL, int &vertL, int &invalidMoves,
string direction, int numOfSpaces)
{
	

	cout << "Starting Point: " << horzL << vertL << endl;
	cout << "Direction Length: " << direction.length() << endl;
	
	if ( !( (direction.at(0) == 'L' && horzL - numOfSpaces < 'A')
           || (direction.at(0) == 'R' && horzL - numOfSpaces > 'H')
           || (direction.at(0) == 'U' && vertL + numOfSpaces > 8)
           || (direction.at(0) == 'D' && vertL - numOfSpaces < 1) ) )
	{
		cout << "Passed Valid 1st Direction Loop" << endl; //Test
		if (direction.length() == 2)
		{
			cout << "Passed Length Loop" << endl; //Test
			if ( (direction.at(1) == 'L' && horzL - numOfSpaces > 'A')
                        || (direction.at(1) == 'R' && horzL - numOfSpaces < 'H' ) )
			{
				cout << "Passed Valid 2nd Direction Loop" << endl;

				switch (direction.at(1)) //Second
				{
				case 'L':
					horzL -= numOfSpaces;
					break;
				
				case 'R':
					cout << "Passed R Switch" << endl;
					horzL += numOfSpaces;
					break;
				
				default:
					cout << "critical error.";
				}
			} else {
				cout << "Didn't Pass Valid 2nd Direction Loop" << endl;
			}
		} else {
			cout << "Didn't Pass Length Direction Loop" << endl;
		}
			
			switch (direction.at(0)) //First
			{
			case 'U':
				cout << "Passed U Switch" << endl;
				vertL += numOfSpaces;
				break;
			
			case 'D':
				vertL -= numOfSpaces;
				break;
			
			case 'L':
				horzL -= numOfSpaces;
				break;
			
			case 'R':
				horzL += numOfSpaces;
				break;
			
			default:
				cout << "critical error.";
			}
		} else {
			cout << "Didn't Pass Valid 1st Direction Loop" << endl;
			invalidMoves++;
	}
	
	cout << invalidMoves << endl;
	cout << horzL << vertL;
	getch();
	return;
}
Last edited on
Those aren't questions. You state that you have questions but then never ask them. Try to be specific and actually ask questions in the future.

That said, I know what you were trying to ask. Those warnings are pretty self-explanatory. They're just saying that getch() is deprecated (old) and to use _getch() in its place. Literally just replace everywhere you have "getch()" with "_getch()". As for the function return... first, ask yourself "why do I need to return multiple data types?". Does that function do too much? Is the code structured poorly? Sometimes the solution lies in eliminating the need to return multiple data types, rather than returning multiple data types. If you must return more than one data type (or variable of the same type) you could always use a struct to store the data.

1
2
3
4
5
struct ReturnStruct
{
    char* data;
    int size;
};


Then you just create an instance of that struct, initialize the data members then return it.
Template might be helpful.
Last edited on
ModShop I apologize for any confusion. The program is a checkers game and it has to look up the location (i.e 'A'5) in the same function. So, I will be using the struct.

How do I set up the function call and definition to pass these structs to the function? What I have below is wrong.

First Struct:
1
2
3
4
struct gamePiece {
  char horzL;
  int vertL;
} gamePieceLocation;

Second Struct:
1
2
3
4
5
struct gamePieceMI {
  string direction;
  int numOfSpaces;
  int invalidMoves;
} moveInfo;

Function Prototype:
struct gamePieceMover(gamePieceL gamePieceLocation, gamePieceMI moveInfo);

Function Call:
1
2
3
4
5
6
while ( !( intData.eof() ) )
	{
		intData >> moveInfo.direction >> moveInfo.numOfSpaces;
		cout << moveInfo.direction << "  " << moveInfo.numOfSpaces << endl;
		cout << gamePieceMover (moveInfo, gamePieceMoveInfo) << endl << endl;
	}
Last edited on
What's wrong with it?
Oh I figured it out. Thanks Mod
Topic archived. No new replies allowed.