Server Help Forum Index Server Help
Community forums for Subgame, ASSS, and bots
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   StatisticsStatistics   RegisterRegister 
 ProfileProfile   Login to check your private messagesLogin to check your private messages   LoginLogin (SSL) 

Server Help | ASSS Wiki (0) | Shanky.com
% sings while obtaining information

 
Post new topic   Reply to topic Printable version
 View previous topic  Tab in teaxarea issue Post :: Post Recource files  View next topic  
Author Message
tcsoccerman
Server Help Squatter


Age:31
Gender:Gender:Male
Joined: Jan 15 2007
Posts: 694
Location: Atlantis
Offline

PostPosted: Mon Feb 26, 2007 10:07 am    Post subject: % sings while obtaining information Reply to topic Reply with quote

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
View users profile Send private message Add User to Ignore List Send email AIM Address
Smong
Server Help Squatter


Joined: 1043048991
Posts: 0x91E
Offline

PostPosted: Mon Feb 26, 2007 4:01 pm    Post subject: Reply to topic Reply with quote

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.
_________________
ss news
Back to top
View users profile Send private message Add User to Ignore List Visit posters website MSN Messenger
tcsoccerman
Server Help Squatter


Age:31
Gender:Gender:Male
Joined: Jan 15 2007
Posts: 694
Location: Atlantis
Offline

PostPosted: Mon Feb 26, 2007 4:07 pm    Post subject: Reply to topic Reply with quote

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
View users profile Send private message Add User to Ignore List Send email AIM Address
Smong
Server Help Squatter


Joined: 1043048991
Posts: 0x91E
Offline

PostPosted: Mon Feb 26, 2007 4:22 pm    Post subject: Reply to topic Reply with quote

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.
Back to top
View users profile Send private message Add User to Ignore List Visit posters website MSN Messenger
tcsoccerman
Server Help Squatter


Age:31
Gender:Gender:Male
Joined: Jan 15 2007
Posts: 694
Location: Atlantis
Offline

PostPosted: Mon Feb 26, 2007 4:28 pm    Post subject: Reply to topic Reply with quote

ty. looks good but i'm way too lazy now to get back on that
Back to top
View users profile Send private message Add User to Ignore List Send email AIM Address
Cyan~Fire
I'll count you!
I'll count you!


Age:36
Gender:Gender:Male
Joined: Jul 14 2003
Posts: 4608
Location: A Dream
Offline

PostPosted: Tue Feb 27, 2007 10:38 am    Post subject: Reply to topic Reply with quote

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
_________________
This help is informational only. No representation is made or warranty given as to its content. User assumes all risk of use. Cyan~Fire assumes no responsibility for any loss or delay resulting from such use.
Wise men STILL seek Him.
Back to top
View users profile Send private message Add User to Ignore List Visit posters website
tcsoccerman
Server Help Squatter


Age:31
Gender:Gender:Male
Joined: Jan 15 2007
Posts: 694
Location: Atlantis
Offline

PostPosted: Wed Feb 28, 2007 7:10 am    Post subject: Reply to topic Reply with quote

(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
View users profile Send private message Add User to Ignore List Send email AIM Address
Display posts from previous:   
Post new topic   Reply to topic    Server Help Forum Index -> Non-Subspace Related Coding All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum
View online users | View Statistics | View Ignored List


Software by php BB © php BB Group
Server Load: 647 page(s) served in previous 5 minutes.

phpBB Created this page in 0.723598 seconds : 32 queries executed (93.9%): GZIP compression disabled