Find the answer to your Linux question:
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.
  1. #1
    Linux User Giles's Avatar
    Join Date
    May 2005
    Location
    Gloucestershire and Cambridge, UK
    Posts
    283

    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:

    Code:
     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;
    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 % in
    Code:
    int player_%d_cash;
    Thanks
    Toodle-oo
    Giles

    *something like
    Code:
    printf("Select size of array:");
    scanf("%d", &arr_size);
    int array[arr_size];
    seems to be able to produce an array of size "arr_size"

    EDIT: syntax issue
    "Our greatest fear is not that we are powerless. Our greatest fear is Microsoft"
    Registered linux user #391027

  2. #2
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    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];

  3. #3
    Linux 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:
    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);
    }
    I don't know how proficient you are with C, but if there's anything you wonder about, just ask.

  4. #4
    Linux User Giles's Avatar
    Join Date
    May 2005
    Location
    Gloucestershire and Cambridge, UK
    Posts
    283
    Quote Originally Posted by Cabhan
    the usual way to do this is, in fact, with arrays.
    DOH!
    yeah, that might work

    Quote Originally Posted by Dolda2000
    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).
    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.

    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

  5. #5
    Linux 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.

  6. #6
    Just Joined!
    Join Date
    Oct 2005
    Posts
    31

    Talking 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

  7. #7
    Linux Guru
    Join Date
    Oct 2001
    Location
    Täby, Sweden
    Posts
    7,578
    Quote Originally Posted by Loridan
    Honestly though,

    Hasnt someone written a dictionary library for C or C++?
    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.

  8. #8
    Linux Enthusiast
    Join Date
    Aug 2005
    Location
    Hell
    Posts
    514
    Yes, C++ has "map", and lots of things like that.

  9. #9
    Just 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.

  10. #10
    Linux User Giles's Avatar
    Join Date
    May 2005
    Location
    Gloucestershire and Cambridge, UK
    Posts
    283
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •