Server Help

Non-Subspace Related Coding - % sings while obtaining information

tcsoccerman - 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]
Smong - Mon Feb 26, 2007 4:01 pm
Post subject:
Hmm, ok you want an array of team structures I think. Something like:
Code: Show/Hide
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? biggrin.gif 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.
tcsoccerman - 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
Smong - Mon Feb 26, 2007 4:22 pm
Post subject:
Hmm, assuming you can't delete teams I would use a counter.
Code: Show/Hide
int currentteam;
currentteam = 0;
//input loop
while(currentteam < 10 && ... /* whatever else, like quit conditions */)
{
...
scanf("%31s", teamdata[currentteam].name);
...
// lastly increase currentteam by 1 to move to the next slot
currentteam++;
}
This will only work up to 10 teams. If you want more make sure to increase both the size of the teamdata array and also the conditon in the while loop.
tcsoccerman - Mon Feb 26, 2007 4:28 pm
Post subject:
ty. looks good but i'm way too lazy now to get back on that
Cyan~Fire - Tue Feb 27, 2007 10:38 am
Post subject:
Code: Show/Hide
if (!strcmp (choice[7], "")) /*see if data for choice is blank*/

I don't know if you know how strings work (have you read that tutorial I mentioned?), but I'll explain just in case. A string is just an array of integer values (ASCII) terminated by a 0, or null character (usually represented by '\0'). So, a much faster and simpler way of finding out whether a string is "empty" is (*choice[7] == '\0'). This will be true of the string is empty. All it does it check whether the first character in the array (the dereference, * operator, does this) is the null character.

That's probably not the easiest to understand, but read it over a few times and you should get it. icon_smile.gif
tcsoccerman - 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.
All times are -5 GMT
View topic
Powered by phpBB 2.0 .0.11 © 2001 phpBB Group