Hi
I am writing a football tournament sim and cannot get the main of my program to deal with the structures which are defined in the header. Here is the relevent code:

header file

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


using namespace std;

struct team{
		string name;
		int atk;
		int def;
		int injurys;
		int kSkill;
		int sSkill;
		int lastResult;
	};

main cpp file

#include "cup header.h"
#include "structure.h"


using namespace std;

void main (){

teamstructure();
cout<<one.name;
system ("pause");
}

thanks for any help you can give, I can poet the rest of the code if you need it

Dan

Example:

#include <iostream>
#include <string>
using namespace std;
 
struct person
{
	string name;
	int age;
};

// you could aslo just do it straight away.
// That would be more easier if your just using one name/constructor
struct person2
{
	string name;
} somebody;


int main()
{
	////////////////////////////////////////////////////////////////
	person a, b; // here you declare to diffrent variables that comes with the information from person
	a.name = "Something1"; // random text
	b.name = "Something2"; // random text
	a.age = 30; // random age
	b.age = 20; // random age
	cout << a.name << ": " << a.age << endl; // print it out
	cout << b.name << ": " << b.age << endl;
	////////////////////////////////////////////////////////////////
	// The other way is instead of doing:
	// person2 ehm;
	// ehm.name
	////////////////////////////////////////////////////////////////
	// you all ready declared  a name at the end "} somebody;"
	// now what you would do is just only this:
	somebody.name = "Sam";
	cout << somebody.name << endl;

	return getchar();
}

Edited 5 Years Ago by Prosammer: n/a

In other words

#include "cup header.h"
#include "structure.h"


using namespace std;

void main (){

// teamstructure(); // this is wrong
team one; // this is right
one.name = "Something"; // give it a name;
cout<<one.name; 
system ("pause");
}

Example:

#include <iostream>
#include <string>
using namespace std;
 
struct person
{
	string name;
	int age;
};

// you could aslo just do it straight away.
// That would be more easier if your just using one name/constructor
struct person2
{
	string name;
} somebody;


int main()
{
	////////////////////////////////////////////////////////////////
	person a, b; // here you declare to diffrent variables that comes with the information from person
	a.name = "Something1"; // random text
	b.name = "Something2"; // random text
	a.age = 30; // random age
	b.age = 20; // random age
	cout << a.name << ": " << a.age << endl; // print it out
	cout << b.name << ": " << b.age << endl;
	////////////////////////////////////////////////////////////////
	// The other way is instead of doing:
	// person2 ehm;
	// ehm.name
	////////////////////////////////////////////////////////////////
	// you all ready declared  a name at the end "} somebody;"
	// now what you would do is just only this:
	somebody.name = "Sam";
	cout << somebody.name << endl;

	return getchar();
}

is that not more or less what I have done in the header file? (not trying to sound cheeky i just want to understand why it's different)

all the values are assingned in the function teamstructure, shown below

#include "cup header.h"

using namespace std;

void teamstructure(){

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


	cout<<"welcome to the world cup simulator"<<endl;



	int t = 1;
	int injury = 0;
	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.injurys = 0;
			one.kSkill = kskill;
			one.sSkill = sskill;	
		}else{
			if (t==2){
				two.name = name;
				two.atk = atk;
				two.def = def;
				two.injurys = 0;
				two.kSkill = kskill;
				two.sSkill = sskill;
			}else{
				if (t==3){	
					three.name = name;
					three.atk = atk;
					three.def = def;
					three.injurys = 0;
					three.kSkill = kskill;
					three.sSkill = sskill;;
				}else{
					if (t==4){	
						four.name = name;
						four.atk = atk;
						four.def = def;
						four.injurys = 0;
						four.kSkill = kskill;
						four.sSkill = sskill;;
					}else{
						if (t==5){	
							five.name = name;
							five.atk = atk;
							five.def = def;
							five.injurys = 0;
							five.kSkill = kskill;
							five.sSkill = sskill;;
						}else{
							if (t==6){	
								six.name = name;
								six.atk = atk;
								six.def = def;
								six.injurys = 0;
								six.kSkill = kskill;
								six.sSkill = sskill;;
							}else{
								if (t==7){	
									seven.name = name;
									seven.atk = atk;
									seven.def = def;
									seven.injurys = 0;
									seven.kSkill = kskill;
									seven.sSkill = sskill;;
								}else{
									if (t==8){	
										eight.name = name;
										eight.atk = atk;
										eight.def = def;
										eight.injurys = 0;
										eight.kSkill = kskill;
										eight.sSkill = sskill;;

									}
								}
							}
						}
					}
				}
			}
		}t++;
	}

	cout<< one.name<<" atk "<<one.atk<<" def "<<one.def<<" injury "<<one.injurys<<" keeper "<<one.kSkill<<" striker "<<one.sSkill<<endl;
	cout<< two.name<<" atk "<<two.atk<<" def "<<two.def<<" injury "<<two.injurys<<" keeper "<<two.kSkill<<" striker "<<two.sSkill<<endl;
	cout<< three.name<<" atk "<<three.atk<<" def "<<three.def<<" injury "<<three.injurys<<" keeper "<<three.kSkill<<" striker "<<three.sSkill<<endl;
	cout<< four.name<<" atk "<<four.atk<<" def "<<four.def<<" injury "<<four.injurys<<" keeper "<<four.kSkill<<" striker "<<four.sSkill<<endl;
	cout<< five.name<<" atk "<<five.atk<<" def "<<five.def<<" injury "<<five.injurys<<" keeper "<<five.kSkill<<" striker "<<five.sSkill<<endl;
	cout<< six.name<<" atk "<<six.atk<<" def "<<six.def<<" injury "<<six.injurys<<" keeper "<<six.kSkill<<" striker "<<six.sSkill<<endl;
	cout<< seven.name<<" atk "<<seven.atk<<" def "<<seven.def<<" injury "<<seven.injurys<<" keeper "<<seven.kSkill<<" striker "<<seven.sSkill<<endl;
	cout<< eight.name<<" atk "<<eight.atk<<" def "<<eight.def<<" injury "<<eight.injurys<<" keeper "<<eight.kSkill<<" striker "<<eight.sSkill<<endl;

}

would this not be enough? how can I make main see the structures

like what u did there

team one

just give it a name in main

team OKAY

and all the members will be showed in a list

so like this?

#include "cup header.h"
#include "structure.h"


using namespace std;

void main (){


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

teamstructure();
//cout<<one.name;
system ("pause");
}

nonono just like this

team one, two, three; // earier way
team four; // slow way 
team five;
team six;
// etc..
team eleven;

Edited 5 Years Ago by Prosammer: n/a

nonono just like this

team one, two, three; // earier way
team four; // slow way 
team five;
team six;
// etc..
team eleven;

so i just add that to my main and don't change the teamstructure function at all and the data put in in teamstructure will be accessible in main?

If you wanted to store all your data's into your struct

then you can to it this way:

// this is a example

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

struct Player
{
	int age;
	string name;
	int DisplayAge();
	void DisplayName();
};

void Player::DisplayName()
{
	cout << name << endl;
}

int Player::DisplayAge()
{
	cout << age << endl;
	return 0;
}

void main()
{
	Player player;
	player.name = "Something";
	player.age = 1300;
	player.DisplayName();
	player.DisplayAge();
	getchar();
}

I notice u made a global function named "teamstructure" with all your variables there inside from the struct team.. that won't work
because since that teamstructure is global function u can't get the data from outide unless you include it to your struct "team"
there after you can print the team members name by using cout << one.name;

Edited 5 Years Ago by Prosammer: n/a

but is there a way I can then use the data in other functions? for example i want to simulate games using data that the user inputs with the teamstructure code, but the game will actually run in a separate cpp file, is there a way I can basically get at the variables across all my code without the problem of multiple decelerations? (if you have already answered this sorry, i'm pretty new to this)

Okay teamstructure() is a function that takes no parameters and returns void correct? If this is the case and you want to be able to pass the teams around or to access them in main() then you have a couple of options. You can declare the teams globaly like

team one;
team two;
team three;
//...

int main() // I used a int here.  void main() is not standard
{
   teamstructure();
   std::cout << one.name;
   cin.get()  // use this to pause the program instead of system("pause")
   return 0;  // you might have to clear the input stream before for it to work 
}

Alternatively you could store the teams in a vector or an array and then pass that to the various functions

#include <vector>
void teamstructure(std::vector<team> & teams);  // using a reference so that changes stick

int main()
{
    std::vector<team> teams(8); // makes a vector holding 8 blank teams
    teamstructure(teams);
    std::cout << teams[0].name;  // displays the name of the first element in the vector
    //...
}

There other things you could do as well but you need to chose how you want it to look.

Edited 5 Years Ago by NathanOliver: n/a

This article has been dead for over six months. Start a new discussion instead.