#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    3
    Rep Power
    0

    Storing Multiple Values and Outputting


    Hi Guys

    I'm a youth worker attempting to work with some young people on writing a c programme.... and getting lost, but it is fun!

    We have written a programme that asks for how many players and then depending on the number asks for players names.

    We then roll a dice and what we want to do is say Player 1 your go but for Player 1 substitute their name.

    Then on the next dice roll, say the second players name along with your roll.

    Here is our code as it is at the moment.

    Code:
    //
    //  main.c
    //  Not Yours
    //
    //  Created by Richard Brown on 12/10/2012.
    //  Copyright (c) 2012 Fishfingers Incorporated. All rights reserved.
    //
    
    #include <stdio.h>
    #include <string.h> /* memset */
    #include <unistd.h> /* close */
    
    int main(void)
    {
        int dice;
        char c;
        int numPlayers;
        char playerName[10][100];
        int loopcount;
        int rolls;
        
        numPlayers = 0;
        loopcount = 0;
        rolls = 0;
        dice=5;
        
        srand(time(NULL));
        
        printf("\n\nWelcome to this initial game by Fishfingers Incorporated!\n\nEnter the number of players: \n");
        scanf("%d", &numPlayers);
        
        while (loopcount < numPlayers) {
            printf("What is your name\n");
            scanf("%s", playerName);
            printf("Hello %s\n", playerName);
            
            loopcount++;
        }
        
        printf("\n\nYou can press Q to quit at anytime.\n\nIn order to roll again, press the big button called return or enter: \n\n");
        
        while (rolls < numPlayers*13+1) {
            int i = 0;
            
            printf("\n",playerName[10][100]);
            
            while (i < dice) {
                printf("%d,", 1 + rand() % 6);
                i++;
            
            }
            
            rolls++;
            
            // Convert to character value
            scanf("%c", &c);
            if (c == 'Q' || c == 'q')
                break;
        }
    }
    Thanks for any help.

    Rich
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,711
    Rep Power
    480
    fishfinger, take your copyright to the bottom of the sea with you.

    gpl
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,091
    Rep Power
    2222
    We work best when presented with an actual problem. Since many of us also work for a living, we only have small bits of time to spend here and so an actual problem allows us to make better use of that time.

    One suggestion:
    Code:
            // Convert to character value
            scanf("%c", &c);
            if (c == 'Q' || c == 'q')
                break;
    The way that scanf is written, all characters will be read in, including whitespace (ie, "non-data" characters such as spaces, tabs, newlines, control characters, etc) such as the newline that was entered when you pressed the Enter key to input that character. As a result, you will go through that loop an extra time for each legitimate roll, resulting in about twice as many rolls as you entered.

    Instead of "%c", use " %c". That leading space tells scanf that you expect whitespace to precede the meaningful character that you want to read into c and so it will skip any and all leading whitespace, or none if there is none. This practice will also eliminate a common problem posted here of scanf calls "being ignored".

    Also, if your development environment includes a debugger (eg, Visual Studio, Visual C++ 2010 Express Edition), start learning how to use it. It will prove very useful when your program doesn't seem to be working right because with it you can execute your program step by step and see what values the variables hold, etc.

    And thank you for using code tags on your very first post here. A rarity that is much appreciated.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    3
    Rep Power
    0
    Originally Posted by b49P23TIvg
    fishfinger, take your copyright to the bottom of the sea with you.
    Sorry didn't mean to offend. The copyright was meant to be a joke. We use xcode and it added all of that automatically. Sorry we just changed it for a joke. I'll remove it straight away.

    Rich
  8. #5
  9. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,367
    Rep Power
    1870
    However, taking progress made on one forum and passing it off as your starting point on another forum is bad voodoo.

    It looks like you're trying to get others to do your work a few lines at a time, then find another forum to add another few lines.
    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
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    3
    Rep Power
    0
    Originally Posted by salem
    However, taking progress and passing it off as your starting point on another forum is bad voodoo.

    It looks like you're trying to get others to do your work a few lines at a time, then find another forum to add another few lines.
    Hi Salem

    That is not what was intended. I thought you might have finished for the day and was asking the question here.

    I will delete this thread and return from whence I came.

    I am sorry to have offended folks.

    Rich

IMN logo majestic logo threadwatch logo seochat tools logo