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

Recommended Answers

All 2 Replies

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;
}
commented: Thanks for the quick reply! +0

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

cin>>n.det.health;
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.