Results 1 to 10 of 10
My current minor dabble in programming waters is to try to write the "Fabulous Elf Game" (invented by a maths teacher, based around probability ratios, but nevertheless quite fun) in ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
- 02-10-2006 #1
declare variables within a loop with incrementing names
My current minor dabble in programming waters is to try to write the "Fabulous Elf Game" (invented by a maths teacher, based around probability ratios, but nevertheless quite fun) in C. As part of this I want to declare the variables
player_1_cash
player_2_cash
etc
for as many players as the user specifies.
The idea I'm starting from is something like this:
Basically, I'd like to know if it can be done, and, if so, whether I'm going about it the right way or not. Having in the past successfully declared arrays whose size depended on a user choice* I guessed this might be achievable in a similar manner, but all I know for sure is that what I've got isn't quite right, as gcc wont accept the % inCode:printf("Select number of players"); scanf("%d", &num_players); count=0; while (count < num_players) {count++; int player_%d_cash; count;} player_1_cash=100;ThanksCode:int player_%d_cash;
Toodle-oo
Giles
*something like
seems to be able to produce an array of size "arr_size"Code:printf("Select size of array:"); scanf("%d", &arr_size); int array[arr_size];
EDIT: syntax issue"Our greatest fear is not that we are powerless. Our greatest fear is Microsoft"
Registered linux user #391027
- 02-10-2006 #2
While I don't C specifically, the usual way to do this is, in fact, with arrays.
So let's say that you ask the user for how many players, and he tells you 4. Just create an array of size 4.
In C++, you might do it like this:
Code:std::cout << "Enter the Number of Players: "; std::cin >> numPlayers; int players[numPlayers];
- 02-11-2006 #3Linux Guru
- Join Date
- Oct 2001
- Location
- Täby, Sweden
- Posts
- 7,578
Well, it is quite natural that your "int player_%d_cash" syntax doesn't work. There is no such things as dynamic symbol names in C.
The normal way to do this would be using malloc. I, for one, would also use a struct to describe a player (to get a more extensible format). For example:
I don't know how proficient you are with C, but if there's anything you wonder about, just ask.Code:#include <stdio.h> #include <stdlib.h> struct player { int cash; } int main(void) { int i, np; struct player *p; printf("Select number of players: ") scanf("%d", &np); p = malloc(sizeof(*p) * np); for(i = 0; i < np; i++) { p[i].cash = 100; /* Or something */ } /* Play the game */ free(p); }
- 02-11-2006 #4DOH!
Originally Posted by Cabhan
yeah, that might work
Yes, using struct was certainly an idea I had for long term - perhaps thats subconciously why I didnt want to use an array (or perhaps I really was being thick). As yet, I really havent grasped the whole pointers and memory stuff, tho - can you suggest a good tutorial (perhaps just for this subtopic) somewhere? I've been using the howstuffworks C tutorial which is great, but that section has just left my head spinning, so my hope is that another style of explanation might make more sense to me.
Originally Posted by Dolda2000
Thanks to you both
Toodle-oo
Giles"Our greatest fear is not that we are powerless. Our greatest fear is Microsoft"
Registered linux user #391027
- 02-11-2006 #5Linux Guru
- Join Date
- Oct 2001
- Location
- Täby, Sweden
- Posts
- 7,578
Sorry, I know of no C tutorials at all. I've considered writing one, but it's just too much work for me right now. For now, I would recommend what I always recommend anyone trying to learn C: Learn assembler first. You probably won't ever write anything in assembler, but just the very knowledge of it is immensely helpful when writing C program.
I really do mean immensely helpful. It's not just something that helps you out on the side or anything when writing C programs, but it is at the very core of the process.
- 02-12-2006 #6Just Joined!
- Join Date
- Oct 2005
- Posts
- 31
Theres no dictionary book to explain how you look, WOAH!
Honestly though,
Hasnt someone written a dictionary library for C or C++? I know that in ruby or python you could just say:
RUBY EXAMPLE:
cash = {}
for i in (1..num_of_players)
cash["player#{i}"] = 100
end
PYTHON EXAMPLE:
cash = {}
for i in range(1,num_of_players+1):
cash['player%i'%i] = 100
Then the keys are the players and the values are the amount of cash they have.
print cash["player1"]
100
- 02-13-2006 #7Linux Guru
- Join Date
- Oct 2001
- Location
- Täby, Sweden
- Posts
- 7,578
Naturally. Many times over, even. I don't know about C++, but in C, there is at least the hashmap implementation in glib. There is even the envz* functions in standard glibc (glibc, though, not libc). See the envz_add manpage for more info.
Originally Posted by Loridan
- 02-13-2006 #8Linux Enthusiast
- Join Date
- Aug 2005
- Location
- Hell
- Posts
- 514
Yes, C++ has "map", and lots of things like that.
- 02-13-2006 #9Just Joined!
- Join Date
- Jan 2006
- Location
- India
- Posts
- 52
Hi Giles,
There are handful of information on understanding pointers in C,
You can start up with a book "Understadning Pointers" by Yeshawant Kanitkar.
- 02-13-2006 #10
Thanks rajeshk, I'll remember that book.
Toodle-oo
Giles"Our greatest fear is not that we are powerless. Our greatest fear is Microsoft"
Registered linux user #391027


Reply With Quote
