Struct and malloc array

This is a discussion on Struct and malloc array within the C Programming forums, part of the General Programming Boards category; I want my code to be able to ask for user how many planets the user wants to add to ...

Struct and malloc array

I want my code to be able to ask for user how many planets the user wants to add to the array of planets, it asks the user how many in the beginning and store it as "numberofinputs" then use that to be the array size using malloc.

But it is giving me the error: request for member name( and dist, and decription) in something not in structure or union.

If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Warnings about gets() probably reflect the fact that gets() is unsafe (there is no way to prevent it from tromping memory if the user enters too many characters). Using gets() is considered bad practice, to the extent that it is deprecated in the latest C standard (i.e. scheduled for removal completely from C).

Look up the fgets() function for a safer alternative.

Originally Posted by christop

Edit: drat, beat by a few seconds again.

I wasn't aware there was a competition. But, if there is ... <evil chuckle>

If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

so i have made some changes to the code, the user now can enter all the information in one single line. but when i try to print out the info the user entered, it only displays: 0.00000 for all the entries..

There are some other issues there. You don't need your local variables apart from int i, you can scan directly to your struct. Also has_planet is always 0 so the test if(!has_planet) will always be true.