hey, I promise i'm getting there!!

so my code now seems to be emptying the structures as they are passed between the functions, any thought on how to fix it? also if anyone knows of any good examples of passing by reference, that could apply to this code that would be great


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;
};

main

#include "sim-header.h"


using namespace std;


void setup(team, team, team, team, team, team, team, team);
void game(team,team);
void group (team, team, team, team);

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

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

	system("PAUSE");
	return 0;
}

void setup (struct team one, struct team two, struct team three, struct team four, struct team five, struct team six, struct team seven, struct 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");

}

void game(team a,team b){
	srand (time(NULL));
	
	cout<<"game start"<<endl;
	cout<<a.name<<" "<<a.atk<<" "<<a.def<<" "<<a.keeper<<" "<<a.striker<<endl;
	cout<<b.name<<" "<<b.atk<<" "<<b.def<<" "<<b.keeper<<" "<<b.striker<<endl;
	int aTrueAtk = (a.atk + rand() %25);
	int bTrueAtk = (b.atk + rand() %25);
	int aTrueDef = (a.def + rand() %25);
	int bTrueDef = (a.def + rand() %25);

	int shotsA = aTrueAtk - b.def;
	int shotsB = bTrueAtk - a.def;

	int n = 0;
	int scoreA = 0;
	int scoreB = 0;
	while (n<shotsA){
		int aTrueSSkill = (a.striker + rand() %25);
		int aTrueKSkill = (a.keeper + rand() %25);
		int bTrueSSkill = (b.striker + rand() %25);
		int bTrueKSkill = (b.keeper + rand() %25);
	
		if (aTrueSSkill > bTrueKSkill){
			cout<<a.name<<" scores!! "<<endl;
			scoreA = scoreA + 1;
			cout<<scoreA<<"-"<<scoreB<<endl;
			n++;
		}else{ if (aTrueSSkill < bTrueKSkill){
			cout<<b.name<< " saves it!! "<<endl;
			cout<<scoreA<<"-"<<scoreB<<endl;
			n++;
		}else{
			cout<< "Chance at a rebound!!!"<<endl;
			cout<<scoreA<< "-"<<scoreB<<endl;
			}
		}
	}
	n = 0;
	
	while (n<shotsB){
		int aTrueSSkill = (a.striker + rand() %25);
		int aTrueKSkill = (a.keeper + rand() %25);
		int bTrueSSkill = (b.striker + rand() %25);
		int bTrueKSkill = (b.keeper + rand() %25);

		if (bTrueSSkill > aTrueKSkill){
			cout<<b.name<<" scores!! "<<endl;
			scoreB = scoreB + 1;
			cout<<scoreA<<"-"<<scoreB<<endl;
			n++;
		}else{ if (bTrueSSkill < aTrueKSkill){
			cout<<a.name<< " saves it!! "<<endl;
			cout<<scoreA<<"-"<<scoreB<<endl;
			n++;
		}else{
			cout<< "Chance at a rebound!!!"<<endl;
			cout<<scoreA<<"-"<<scoreB<<endl;
			}
		}
	}
	if(scoreA>scoreB){
		cout<<a.name<<" wins"<<endl;
		a.score = (a.score + 3);
	}else{
		if(scoreA==scoreB){
			cout<<"it's a draw!"<<endl;
			a.score = (a.score + 1);
			b.score = (b.score + 1);

		}else{
			cout<<b.name<<" wins"<<endl;
			b.score = (b.score + 3);
		}
	}
}

//void group (team a, team b, team c, team d){
//	game(team a, team b);
//	game(team c, team d);
//	game(team a, team c);
//	game(team b, team d);
//	game(team b, team c);
//	game(team a, team d);
//
//}

On line 29 you are declaring structs for each variable.

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

It should be

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

Also if you want your structs to retain what happens to them in the functions then you need to pass them by reference.

void game(team & one, team & two);

You also might want to look up vectors. using vectors and loops would significantly cut back on the code you are writing. As an example here is how you could write an input method.

void GetTeamInfo(std::vector<team> & teams)
{
    int numTeams = 0;
    std::cout << "Please enter the number of teams: ";
    std::cin >> numTeams;
    for (int i = 0; i < numTeams; i++)
    {
        std::cout << "please enter the name of team "<<(t)<<endl;
	std::cin >> teams[i].name;
	std::cout << "please enter the attack score of "<<name<<" (0 - 100) " << endl;
	std::cin >> teams[i].atk;
	std::cout << "please enter the defence score of "<<name<<" (0 - 100) " << endl;
	std::cin >> teams[i].def;
	std::cout << "please enter the keeper skill score for "<<name<<" (0 - 100) " << endl;
	std::cin >> teams[i].keeper;
	std::cout << "please enter the striker skill score for "<<name<<" (0 - 100) " << endl;
	std::cin >> teams[i].striker;
    }
}

Edited 5 Years Ago by NathanOliver: n/a

Sorry The code should look like this

std::vector<team> GetTeamInfo()
{
    int numTeams = 0;
    std::cout << "Please enter the number of teams: ";
    std::cin >> numTeams;
    std::vector<team> teams(numTeams);
    for (int i = 0; i < numTeams; i++)
    {
        std::cout << "please enter the name of team "<<(t)<<endl;
	std::cin >> teams[i].name;
	std::cout << "please enter the attack score of "<<name<<" (0 - 100) " << endl;
	std::cin >> teams[i].atk;
	std::cout << "please enter the defence score of "<<name<<" (0 - 100) " << endl;
	std::cin >> teams[i].def;
	std::cout << "please enter the keeper skill score for "<<name<<" (0 - 100) " << endl;
	std::cin >> teams[i].keeper;
	std::cout << "please enter the striker skill score for "<<name<<" (0 - 100) " << endl;
	std::cin >> teams[i].striker;
    }
}

Edited 5 Years Ago by NathanOliver: n/a

This question has already been answered. Start a new discussion instead.