Thread: storing players names

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    19

    storing players names

    Hey i could use some help, im working on a snakes and ladders game project non visual. I am new to C programming I need to write the part of the program which asks the user(s) to enter amount of players in the game, then store that number so the program can ask those amount of players names. Then the players names to be stored to count the score during the game and giving the final score.

    so far i have this:
    Code:
    int i,j;
    	char name[20];
    
    	printf("enter number of players between 1-4:");
    	scanf("%d",&i);
    	printf("you entered %d", i);
    	
    	{printf("please enter your names"),i;}
    	scanf("%s",&name);
    	printf("Player names: %s", name);
    Would appreciate some help! thanks.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > char name[20];
    Try
    char names[4][20];

    Then later
    scanf( "%s", names[i] );
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    34
    you shouldn't use const char limit since it could lead to stack over flow if its more than 4 you should allocate it even though its would be less efficient but will help you getting away from problems and you shouldn't use scanf either in reading a string coz it would stop after reaching first space it will not read after that you can either read it by getchar or fgets

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    19
    Quote Originally Posted by Salem View Post
    > char name[20];
    Try
    char names[4][20];

    Then later
    scanf( "%s", names[i] );
    Thanks but that doesnt work, I want the use to be able to enter the amount of players between 1-4. Then the program to recognise that and ask the players to enter their names which can be stored. Sorry i am bit useless at programming

  5. #5
    Registered User
    Join Date
    Jan 2010
    Posts
    34
    Quote Originally Posted by billet View Post
    Thanks but that doesnt work, I want the use to be able to enter the amount of players between 1-4. Then the program to recognise that and ask the players to enter their names which can be stored. Sorry i am bit useless at programming
    you would have to use that in a loop reading each array
    for example
    for(int i = 0 ; i < sizeof(szName) / sizeof(szName[0];i++)
    so you could read each index. but you shouldn't use what salem said since it would lead to potential bug's in the code.you should have szName as ptr to ptr and allocate according to num of ppl you got then read using your own getchar or fgets.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You'll need to expand your array to make more space. Right now, you only have room for 20 characters.

    Try

    int names[8][25];

    That will give you space for 8 names, each name having up to 24 char's.

    Then you can loop through your names input. It's fine if you have only fewer than 8 names. Your player_num variable, will control all the output and game play. Don't use i for the player_num, though.

    Traditionally, i is a loop counter, and you want something much more descriptive for your other variable names. It just helps a lot.

    Code:
    #include <stdio.h>
    
    int main() {
      int i, player_num; 
      char names[8][25];
    
      printf("\n\n");  
    
      //user inputs value of player_num, here, as you have now
      
    
      for(i = 0; i < player_num; i++) {
        printf("Enter the player's name: ");
        scanf("%s", names[i]);  //enters name and creates a newline <enter key>
        getchar();                   //removes the newline from the keyboard buffer
      }
      printf("\n\n");
      for(i = 0; i < player_num; i++)
        printf("\n%s", names[i]);
      
      printf("\n\n\t\t\t     press enter when ready");
    
      getchar();   //holds the console window open until you press enter
      return 0;
    }
    For very small amounts of memory, as a beginner, it's much easier to just have a fixed number of names made available, than to start working with dynamically allocating memory. That can best be done, a bit later in your education.

  7. #7
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Quote Originally Posted by kira_coder View Post
    but you shouldn't use what salem said since it would lead to potential bug's in the code.you should have szName as ptr to ptr and allocate according to num of ppl you got ...
    If the max limit is well defined (OP said 1 to 4 players) then there's really no problem using a fixed size array.

    ... then read using your own getchar or fgets.
    Huh? What's wrong with using the ones from the standard library?

  8. #8
    Registered User
    Join Date
    Jan 2010
    Posts
    34
    Quote Originally Posted by _Mike View Post
    If the max limit is well defined (OP said 1 to 4 players) then there's really no problem using a fixed size array.


    Huh? What's wrong with using the ones from the standard library?
    scanf stops at first space so you cant read name such as you cant read name like 91 92 which ppl have to read there full name obviously also fgets and getchar() are stand library its not about max limit defined since user whos using application won't really care abt max lim unless you make a test not to go beyond that limit.

  9. #9
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Quote Originally Posted by kira_coder View Post
    obviously also fgets and getchar() are stand library
    I know, which is why I wondered why he should use "[his] own" and not those?
    its not about max limit defined since user whos using application won't really care abt max lim unless you make a test not to go beyond that limit.
    Well if you as a programmer says there's an upper limit it would be kinda dumb not to validate the input against it. Otherwise, why even have a limit in first place?

  10. #10
    Registered User
    Join Date
    Jan 2010
    Posts
    34
    well as for fgets because it does same thing as scanf but could read all line and add \n before NULL and for the limit I said it coz there was not for me coz ofcourse I already know that but because the guy asking the question coz as his is a beginner maybe it wouldn't come to his mind to test.

  11. #11
    Registered User
    Join Date
    Mar 2010
    Posts
    19
    When i try to run the programme i get an error saying "the variable player_num is being used without being initialized" what does that mean??

    Quote Originally Posted by Adak View Post
    You'll need to expand your array to make more space. Right now, you only have room for 20 characters.

    Try

    int names[8][25];

    That will give you space for 8 names, each name having up to 24 char's.

    Then you can loop through your names input. It's fine if you have only fewer than 8 names. Your player_num variable, will control all the output and game play. Don't use i for the player_num, though.

    Traditionally, i is a loop counter, and you want something much more descriptive for your other variable names. It just helps a lot.

    Code:
    #include <stdio.h>
    
    int main() {
      int i, player_num; 
      char names[8][25];
    
      printf("\n\n");  
    
      //user inputs value of player_num, here, as you have now
      
    
      for(i = 0; i < player_num; i++) {
        printf("Enter the player's name: ");
        scanf("%s", names[i]);  //enters name and creates a newline <enter key>
        getchar();                   //removes the newline from the keyboard buffer
      }
      printf("\n\n");
      for(i = 0; i < player_num; i++)
        printf("\n%s", names[i]);
      
      printf("\n\n\t\t\t     press enter when ready");
    
      getchar();   //holds the console window open until you press enter
      return 0;
    }
    For very small amounts of memory, as a beginner, it's much easier to just have a fixed number of names made available, than to start working with dynamically allocating memory. That can best be done, a bit later in your education.

  12. #12
    Registered User
    Join Date
    Mar 2010
    Posts
    19
    My bad ! it works now! much appreciated this forum is great! i owe you all a pat on the back cheers!

    Quote Originally Posted by billet View Post
    When i try to run the programme i get an error saying "the variable player_num is being used without being initialized" what does that mean??

  13. #13
    Registered User
    Join Date
    Mar 2010
    Posts
    19
    The program works fine just need to know how i can store the players names that are entered in to the memory so they can be used record the scores? thanks

    Code:
    int i, player_num; 
      char names[8][25];
    
      printf("\n\n");  
    
    start:
      printf("enter number of players between 1-4:"); //user inputs value of player_num
    	scanf("%d",&player_num);
    	if (player_num > 4)goto start;
    		
    	else;
    	
    
     for(i = 0; i < player_num; i++) {
        printf("Enter the player's name: ");
        scanf("%s", names[i]);  //enters name and creates a newline <enter key>
        getchar();                   //removes the newline from the keyboard buffer
      }
      printf("\n\n");
      for(i = 0; i < player_num; i++)
        printf("\n%s", names[i]);
      
      printf("\n\n\t\t\t m    press enter when ready");
    
      getchar();   //holds the console window open until user presses enter

  14. #14
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by billet View Post
    The program works fine just need to know how i can store the players names that are entered in to the memory so they can be used record the scores? thanks
    Later on, when you learn about structs, you'd make one struct per player, (which you might call one record per player, with each record having 2 or 3 fields in it).

    For now, if you just want to record scores, you could use a simpler array of scores: int scores[8]. Now player 0 (because real programmers start counting with zero, not one), will have score 0, and player[1] will have score[1], etc.


    Code:
      int i, player_num; 
      char names[8][25];
      int scores[8] = {0};
    
      printf("\n\n");  
    
    
    start:
      printf("enter number of players between 1-4:"); //user inputs value of player_num
      scanf("%d",&player_num);
      if (player_num > 4)goto start;
    		
    	// no else is needed 
    	
    
     for(i = 0; i < player_num; i++) {
        printf("Enter the player's name: ");
        scanf("%s", names[i]);  //enters name and creates a newline <enter key>
        getchar();                   //removes the newline from the keyboard buffer
      }
      printf("\n\n");
      for(i = 0; i < player_num; i++)
        printf("\nName: %s  Score: %d ", names[i], score[i]);
      
      printf("\n\n\t\t\t m    press enter when ready");
    
      getchar();   //holds the console window open until user presses enter
    Something like that. The index (the "i'), in the for loop, is the "glue" that keeps the player associated with the players score. Makes it easy as pie, eh?

  15. #15
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by kira_coder View Post
    scanf stops at first space
    there are other formats except %s, you know...

    like
    Code:
    char buffer[25];
    scanf("%24[^\n]",buffer);
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Storing names and email address.
    By arya6000 in forum C Programming
    Replies: 15
    Last Post: 11-19-2008, 03:47 AM
  2. runtime function names
    By lruc in forum Tech Board
    Replies: 2
    Last Post: 10-11-2008, 10:51 AM
  3. reading folder names..how is it done ?
    By roalme00 in forum C++ Programming
    Replies: 8
    Last Post: 01-11-2008, 10:34 AM
  4. Structure of a program
    By stef569 in forum C Programming
    Replies: 6
    Last Post: 12-03-2006, 12:13 AM
  5. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM