Structs & Arrays

I have a function that gets information from the user via the keyboard.

I want to store that data (element, name, power) in an array of structs - how would I go about doing it?

and would I declare the structs/array in the main function?

Thanks,

mapper
mapperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jkrCommented:
There are several ways to do that - at first you'll have to create your structure:

#define MAX_ELEMENT 256 /* use appropriate values here */
#define MAX_NAME 256

/* creating a data type for your struct is safer and more convenient */
typedef struct _tagDATA
{
char acElement [ MAX_ELEMENT];
char acName    [ MAX_NAME];
int  nPower;
} DATA;


then, declare the array

#define MAX_DATA 256 /* use appropriate values here */

/* glogally declaring it makes it 'visible' for all functions in your program */
DATA Data [ MAX_DATA];

/* ... */

int main ( int argc, char** argv)
{
/* ... */
}

Feel free to ask if you need to know more!
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mapperAuthor Commented:
Jkr,

Okay, I think I have the define part - but, in the function:

void add_pokemon(char *element, char *name, char *power)
{
fflush(stdin);
printf("\nEnter the element type of the card?\n");
gets(element);
printf("What is the name of the pokemon?\n");
gets(name);
printf("Enter the power level:\n");
gets(power);
}

How would the input above get into the array?

Thanks,

mapper
0
jkrCommented:
You'll need something to remember the array position, e.g.

#define MAX_ELEMENT 256 /* use appropriate values here */
#define MAX_NAME 256
#define MAX_POWER 256

/* creating a data type for your struct is safer and more convenient */
typedef struct _tagDATA
{
char acElement [ MAX_ELEMENT];
char acName    [ MAX_NAME];
char  acPower  [ MAX_POWER];
} DATA;unsigned int unDataPos = 0;

/* ... */

and use it like this:

void add_pokemon(char *element, char *name, char *power)
{
fflush(stdin);
printf("\nEnter the element type of the card?\n");
gets(element);

strcpy ( Data [ unDataPos].acElement, element);

printf("What is the name of the pokemon?\n");
gets(name);

strcpy ( Data [ unDataPos].acName, name);

printf("Enter the power level:\n");
gets(power);

strcpy ( Data [ unDataPos].acPower, power);

if ( unDataPos < MAX_DATA)
{
 nDataPos++;
}
else
{
  /* handle error */
}

}
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

jkrCommented:
One would usually use a list for that, but as I assume you're starting up with C (and you're doing good), we'll leave that for later ;-)
0
mapperAuthor Commented:
Jkr,

Most excellent - I am working on this with my son and he has a crap load of cards - what do you think a safe value for:

#define MAX_ELEMENT 256 /* use appropriate values here */

would be?

Thanks,

mapper
0
jkrCommented:
Well, that's hard to say - but nowadays (everybody has a 'Cray' at the desktop ;-) ... I think 8192 should be enough (just mention typing them all...)
0
mapperAuthor Commented:
Jkr,

Great!  This will give us something to chew on - then next printing the cards - I just hope he doens't want to search for a specific card or element - that's beyond my meager talent - I was hoping that this would get him into something more than video games - he seems to be enjoying so far - and he keeps asking to do stuff that's beyond my skills - but, he's keeping his old man on the run!

:-)

Thanks,

mapper
0
mapperAuthor Commented:
jkr,

Guess what?  Sam wants to be able to print all of his cards - he wants to be able to take that to school w/o having to worry about someone stealing the real ones! Can you believe that?  He shows me a $20 bill and I ask him where he gets it and he says that a bunch of kids were bidding on a rare card a kid had with him, and some kid paid him $20 to stop bidding against him - I asked him - "Sam, did you have any money with you?"  The answer was "No dad"  I made him return the $20...

Any ways - I have a function:

void print_pokemon(void)
{
printf("\n\nYour Pokemon cards are:  %s\n", MAX_DATA);
}

MAX_DATA was the name of the array...

I can resubmit this as a new question - I just thought the previous posts would refresh your memory and lock out all others...

Thanks,

mapper
0
jkrCommented:
No problem (don't mind points ;-)

I'd be glad to help you - it's kinda 'refreshing'

Sorry for the delay (I'm living in Europe) - and am having a rare visitor (a very good 'old' friend). And as this is our first meeting since 7 years, I don't spend much time 'online' at the moment... but I'll get back at this tomorrow

Err - so much for the moment ;-)

int i;

for ( i = 0; i < unDataPos; i++)
{
printf ( "Element: %s\r\n", Data [ i].acElement);
printf ( "Element: %s\r\n", Data [ i].acName);
printf ( "Element: %s\r\n", Data [ i].acPower);
}

you can easily redirect the output to a file or a printer using

myprog.exe > LPT1:

(I hope that this is correct, no printer here to verify it ;-)
0
mapperAuthor Commented:
jkr,

Where in Europe?  Sounds like fun...  I hope you and your friend are having a good time - Sam decided to blow off his basketball game and go to a friend of his birthday party (did I mention his friend is a girl?).  Sam is 10 and  is starting to get caught up in his personal appearance  -  "Dad, help me with my hair, how do I look?"  It's kind of funny...  :-)  My wife and I, think we may need to start saving for paternity law suit just in case... ;-)  

Oh well,  I will wait for him to show some interest tomorrow... I could not get what I have to compile...  I am trying to modify some of my code I was playing around with while I was learning C...  I was trying to develop a program for a video store...  It was part of a book I was studying and I was got the basic stuff to work but I was trying to add to the code and could not get the extra stuff I wanted to do to work...  The basics were easy... But I guess that's the way it usually is...

Have fun and enjoy your time with your friend - moments like that are far and few in between...

I will yack with you later...

map

n2chiles@uswest.net
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.