Author |
Message |
tcsoccerman Server Help Squatter
Age:32 Gender: Joined: Jan 15 2007 Posts: 694 Location: Atlantis Offline
|
Posted: Mon Feb 26, 2007 10:07 am Post subject: % sings while obtaining information |
|
|
|
|
This is very advanced(i think):
I'm trying to make a program which tracks team stats. It has 10 slots for 10 teams. at the main screen if they type create[#] it will create a team inn slot #. Is there a way to have only one create[#] function which does something like this
printf("first, what do you want you're team name to be?\n");
scanf("%s", (&team%s.name, choice[7]).
choice is a char that you use at main screen.
team#.name is a structure
any help would be great. any questions can be answerd[/code] |
|
Back to top |
|
|
Smong Server Help Squatter
Joined: 1043048991 Posts: 0x91E Offline
|
Posted: Mon Feb 26, 2007 4:01 pm Post subject: |
|
|
|
|
Hmm, ok you want an array of team structures I think. Something like:
struct
{
char name[32];
} teamdata[10];
...
int team;
printf("team #? ");
team = -1;
scanf("%d", &team);
if (team >= 0 && team < 10)
{
printf("ok what will you name it? ");
scanf("%31s", teamdata[team].name);
teamdata[31] = 0;
} |
Ok some important safety stuff here:
- I'm initialising team to -1, so if the user enters a string instead of a number you can detect this, since its value will remain at -1, which is not a valid team number.
- I then do the check to see if the team number is valid, otherwise when we access the teamdata array you might access bad memory.
- In the last scanf I am using %31s as the format string. This limits the input to 31 characters max. If you check the teamdata struct you can see I made the name 32 chars max, you need 1 spare char to end the string (think of it like an invisible full stop, the computer needs it not us humans).
- teamdata[team].name, I don't know if this looks complex to you or not. What's happening here is I am accessing the teamdata array at index team. The ".name" means I want to access the field that is called "name". Remember I made "name" a char array, we are sticking a string in here, so there is no need to prefix "&". Ok confused now? Other data types (mostly numbers) you need the & prefix, like when we got the team number earlier on.
- Finally, the magic code to end a string is zero, yep just 0. _________________ ss news |
|
Back to top |
|
|
tcsoccerman Server Help Squatter
Age:32 Gender: Joined: Jan 15 2007 Posts: 694 Location: Atlantis Offline
|
Posted: Mon Feb 26, 2007 4:07 pm Post subject: |
|
|
|
|
hmmm. a few new things like the if statement is a little different but most of it made sense. ty. also, does this check to see if there is already data in slot 1, and then go on until it finds a blank slot. this is what i had for that. (i had char choice not int team(they type create or team name already saved))
ex:what would you like to do.
input:createslot1
if (!strcmp (choice[7], "")) /*see if data for choice is blank*/
{
create(1, other arguements)
}
yes it's very crappy cause lots of values point to nothing and it's just not organized. gotta go write on paper again. lol |
|
Back to top |
|
|
Smong Server Help Squatter
Joined: 1043048991 Posts: 0x91E Offline
|
|
Back to top |
|
|
tcsoccerman Server Help Squatter
Age:32 Gender: Joined: Jan 15 2007 Posts: 694 Location: Atlantis Offline
|
Posted: Mon Feb 26, 2007 4:28 pm Post subject: |
|
|
|
|
ty. looks good but i'm way too lazy now to get back on that |
|
Back to top |
|
|
Cyan~Fire I'll count you!
Age:36 Gender: Joined: Jul 14 2003 Posts: 4608 Location: A Dream Offline
|
|
Back to top |
|
|
tcsoccerman Server Help Squatter
Age:32 Gender: Joined: Jan 15 2007 Posts: 694 Location: Atlantis Offline
|
Posted: Wed Feb 28, 2007 7:10 am Post subject: |
|
|
|
|
(know)Every word. I bought a book called Sams teach yourself "C" in 21 days and already on day 8, now a little confusing with pointers and malloc. Thank you for staying thurough wit hyou're posts though. It shows chararistics of a programmer i think. |
|
Back to top |
|
|
|