0

This is a coursework help *Just so its clear im playing by the rules*

A question on my coursework is to "Put a structure within a structure and show how this can be addressed".

I understand regular structures, and *hopefully* this code is correct. But im having trouble working out how to declare this in main, without using global variables.
My c++ lecturer upon sight of global variables will instantly fail the coursework.

So....if someone could point me, or show me how to declare the "Structure within the structure" in main, much appretiated.
Also, if addressing it is any different, then knowing this would be great as i can then look it up.

#include<iostream>
#include<string>
using namespace std;
//Creating Structure within a structure
struct NPC
{
	string name;
	int ident;
	struct NPC_det
	{
		int health;
		int attack;
		int def;
	}//end 2nd struct
}//end 1st struct
///////////
int main()
{
	//Declarations
	NPC Metro; NPC Overwatch; NPC Combine;
	NPC_det Metro; NPC_det Overwatch; NPC Combine;
	Metro.name = "Blank";
	Metro.ident = 0;
	
	cout<<"-----Nested Structures-----"<<endl;
	cout<<endl;

}

As you can see, i have tried declaring the second structure.

Ta Muchly

2
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by Suicidal_tool
1

just as you did with structure menber int ident you have to create an object of type NPC_det

struct NPC
{
	string name;
	int ident;
	struct NPC_det
	{
		int health;
		int attack;
		int def;
	} det;//end 2nd struct
}//end 1st struct
int main()
{
    NPC n;
    n.det.health = 1;
}
Votes + Comments
Thanks for the quick reply!
0

Much appretiated Ancient Dragon, i even see how i can address the 2nd structure:

cin>>n.det.health;
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.