hi
Is there a way to pass a structure into a function in C++? ie if I have a structure of data, can I pass it in and use all the members without having to pass them indevidually? I basically want to pass team data into a function and that function will use the data to simulate a football game and rather than write out a huge block of code to simulate a tournament I would like to have a game function I just pass team structures into.

Cheers for the help
Dan

Recommended Answers

All 10 Replies

You would pass a structure just like you would any other type, your function just needs to be written to accept it.

struct team
{
   string name;
   int skill;
   ...
}

void SimulateMatch(team t1, team t2)
{
   ...
}
Member Avatar for danb737

And if you know what it means to pass by reference, you will probably want to do that, in order to save the cost of a copy-destruction.

struct team
{
   string name;
   int skill;
   ...
}

void SimulateMatch(const team& t1, const team& t2)
{
   ...
}

thanks for the help, can't quite get it to work though, this is my code so far:

sim-header

#include <math.h>
#include <iostream>
#include <array>
#include <string>
#include <time.h>


using namespace std;

struct team{
	string name;
	int atk;
	int def;
	int striker;
	int keeper;
	int score;
	bool progress;
};

simulator

#include "sim-header.h"
#include "setup.h"

using namespace std;

void main(){
	team one = {" ",0,0,0,0,false};
	team two = {" ",0,0,0,0,false};
	team three = {" ",0,0,0,0,false};
	team four = {" ",0,0,0,0,false};
	team five = {" ",0,0,0,0,false};
	team six = {" ",0,0,0,0,false};
	team seven = {" ",0,0,0,0,false};
	team eight = {" ",0,0,0,0,false};

	cout<<"welcome to the world cup simulator"<<endl;
	setup(team one, team two, team three, team four, team five, team six, team seven, team eight);
	system("PAUSE");
}

void setup(team one, team two, team three, team four, team five, team six, team seven, team eight){
	system("PAUSE");
	int t = 1;
	int kskill = 0;
	int sskill = 0;
	int atk = 0;
	int def = 0;
	int teamnum = 0;
	string name;

	cout<<"please enter the number of teams: 2, 4 or 8"<<endl;
	cin>>teamnum;
	while (t<= teamnum){
		cout<<"please enter the name of team "<<(t)<<endl;
		cin>>name;
		cout<<"please enter the attack score of "<<name<<" (0 - 100) "<<endl;
		cin>>atk;
		cout<<"please enter the defence score of "<<name<<" (0 - 100) "<<endl;
		cin>>def;
		cout<<"please enter the keeper skill score for "<<name<<" (0 - 100) "<<endl;
		cin>>kskill;
		cout<<"please enter the striker skill score for "<<name<<" (0 - 100) "<<endl;
		cin>>sskill;

		if (t==1){
			one.name = name;
			one.atk = atk;
			one.def = def;
			one.keeper = kskill;
			one.striker = sskill;	
		}else{
			if (t==2){
				two.name = name;
				two.atk = atk;
				two.def = def;
				two.keeper = kskill;
				two.striker = sskill;
			}else{
				if (t==3){	
					three.name = name;
					three.atk = atk;
					three.def = def;
					three.keeper = kskill;
					three.striker = sskill;;
				}else{
					if (t==4){	
						four.name = name;
						four.atk = atk;
						four.def = def;
						four.keeper = kskill;
						four.striker = sskill;;
					}else{
						if (t==5){	
							five.name = name;
							five.atk = atk;
							five.def = def;
							five.keeper = kskill;
							five.striker = sskill;;
						}else{
							if (t==6){	
								six.name = name;
								six.atk = atk;
								six.def = def;
								six.keeper = kskill;
								six.striker = sskill;;
							}else{
								if (t==7){	
									seven.name = name;
									seven.atk = atk;
									seven.def = def;
									seven.keeper = kskill;
									seven.striker = sskill;;
								}else{
									if (t==8){	
										eight.name = name;
										eight.atk = atk;
										eight.def = def;
										eight.keeper = kskill;
										eight.striker = sskill;;

									}
								}
							}
						}
					}
				}
			}
		}t++;
	}
	system("PAUSE");
	cout<< one.name<<" atk "<<one.atk<<" def "<<one.def<<" injury "<<" keeper "<<one.keeper<<" striker "<<one.striker<<endl;
	cout<< two.name<<" atk "<<two.atk<<" def "<<two.def<<" injury "<<" keeper "<<two.keeper<<" striker "<<two.striker<<endl;
	cout<< three.name<<" atk "<<three.atk<<" def "<<three.def<<" injury "<<" keeper "<<three.keeper<<" striker "<<three.striker<<endl;
	cout<< four.name<<" atk "<<four.atk<<" def "<<four.def<<" injury "<<" keeper "<<four.keeper<<" striker "<<four.striker<<endl;
	cout<< five.name<<" atk "<<five.atk<<" def "<<five.def<<" injury "<<" keeper "<<five.keeper<<" striker "<<five.striker<<endl;
	cout<< six.name<<" atk "<<six.atk<<" def "<<six.def<<" injury "<<" keeper "<<six.keeper<<" striker "<<six.striker<<endl;
	cout<< seven.name<<" atk "<<seven.atk<<" def "<<seven.def<<" injury "<<" keeper "<<seven.keeper<<" striker "<<seven.striker<<endl;
	cout<< eight.name<<" atk "<<eight.atk<<" def "<<eight.def<<" injury "<<" keeper "<<eight.keeper<<" striker "<<eight.striker<<endl;

	system("PAUSE");

}

this is the list of errors flagged up:
------ Build started: Project: worldcup-version2, Configuration: Debug Win32 ------
simulator.cpp
c:\users\daniel\documents\visual studio 2010\projects\worldcup-version2\worldcup-version2\simulator.cpp(17): error C2275: 'team' : illegal use of this type as an expression
c:\users\daniel\documents\visual studio 2010\projects\worldcup-version2\worldcup-version2\sim-header.h(10) : see declaration of 'team'
c:\users\daniel\documents\visual studio 2010\projects\worldcup-version2\worldcup-version2\simulator.cpp(17): error C2146: syntax error : missing ')' before identifier 'one'
c:\users\daniel\documents\visual studio 2010\projects\worldcup-version2\worldcup-version2\simulator.cpp(17): error C2059: syntax error : ')'
c:\users\daniel\documents\visual studio 2010\projects\worldcup-version2\worldcup-version2\simulator.cpp(17): error C3861: 'setup': identifier not found
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


any help would be much appreciated

ignore the setup.h included, i thought that might fix it but it didn't do anything

Try using an array team Team[8] to make you coding easier and more compact. You won't need that godawful nested IF structure.

Try using an array team Team[8] to make you coding easier and more compact. You won't need that godawful nested IF structure.

so to do that i would call members like this?

[1].atk?
to get the first teams atk score?

Team[0].atk is the first team

the only problem i'm still having is that the function identifiers are not being found. anyone have any thoughts?

Your main function doesn't know that the setup function exists because it's declared/defined below it. You'll have to use what's called a function prototype. Simply, it's the function header placed above the main func with a semicolon at the end.

void setup(team one, team two, team three, team four, team five, team six, team seven, team eight);

int main()
{
   ...
}

It's also good practice to use

int main()

instead of

void main()

. For one, it's the correct way to write C/C++ main functions. Also, it will help you avoid being berrated on DW everytime you post your main function here =)

Member Avatar for danb737

Line 17 of your main.cpp setup(team one, team two, team three, team four, team five, team six, team seven, team eight); . When you call a function you don't write the type of the parameters passed. You only write the parameters themselves. So you should call setup(one, two, three, four, five, six, seven, eight); .

Also be careful with the instantiation of your teams. team one = {" ",0,0,0,0,false}; . There are 5 int in your struct followed by a bool. What happens here is that the bool value is assigned to your score (the last int) and the progress is not set at all.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.