Jump to content
xisto Community
kvarnerexpress

Struct And Pointer Arithmetic/notation.

Recommended Posts

My book that I am reading gave us an assignment that I dont fully understand.

Quote:

Create a program that includes the following:

A structure called planet that includes a name, a distance from the sun and the diameter of the planet.


This I understand.
Code:

struct Planet {	string planName;	double planDistSun;	double planDiam;};



Quote:

Create an array of 3 planet structures.


Does my book mean something like this? What type of array should I use? Could I use a char array?
Code:

Planet Mars;Planet Earth;Planet Jupiter;char planetAry[3];planetAry[0]=Mars;planetAry[1]=Earth;planetAry[2]=Jupiter;


Quote:

Include a function to enter the information for each planet. Use pointer notation to assign the values.


How would I do this? I understand that I can do something like:

Mars.planDiam=50000;
Mars.planDistSun=9000000

but how would I allow the user to enter a name for the planet then make a structure object though. I could just preassign the planets but thats not how he wants it.

Quote:

A function to show all the info for each planet. Use pointer arithmetic to move from one array position to the next.


I dont know how I would go about doing this? Any ideas would be helpful.
kvarnerexpress

Share this post


Link to post
Share on other sites

My book that I am reading gave us an assignment that I dont fully understand.

 

Quote:

 

Does my book mean something like this? What type of array should I use? Could I use a char array?

Code:

 

Planet Mars;Planet Earth;Planet Jupiter;char planetAry[3];planetAry[0]=Mars;planetAry[1]=Earth;planetAry[2]=Jupiter;

I am afraid this is the incorrect way of going about the problem. You cannot assign a variable of an user defined datatype to a variable of a built in data type, which is exactly what you're trying to do (well, technically you can, but that's only when we come to objects and object-oriented programming). Now, creating an array of planets is not very difficult - here's what you have to do:

 

Planet array[3];

That is all, should work :P

 

Include a function to enter the information for each planet. Use pointer notation to assign the values.

 


You have to write a separate function that reads data and assigns it to the Planet variable. The general format would probably be:

 

void readPlanet(Planet* x){............your statements go in here..........}

and while using this function in the main module, you could try

 

readPlanet(&array)

Similarly, write a function called writePlanet which does exactly the opposite :P If you are concerned about how to use pointer arithmatic in the context of this function, then you can read the code for writePlanet below (I am hoping it will work, but I haven't tested it yet).

 

writePlanet(Planet* x){int i=0;while(i<3){cout << "\n Planet name " << x[i].planName;cout << "\n Planet's distance from the sun" << x[i].planSun;cout << "\n Planet's diameter " << x[i].planDiam;}return;}

You could make the function compatible for different array sizes, by making the array size a parameter, and using that parameter in place of "3". Good luck and hope this helps :D

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.