kvarnerexpress 0 Report post Posted November 2, 2005 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=9000000but 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
mama_soap 0 Report post Posted November 2, 2005 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 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 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 Share this post Link to post Share on other sites