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

    Join Date
    Dec 2013
    Posts
    2
    Rep Power
    0

    Cannot fix Segmentation Fault


    I've been working on this code for awhile and keep getting the words segmentation fault pop up after inputting my choice.
    The objective of the code is to play Rock, Paper, Scissors, Lizard, Spock. The user inputs their choice and the computer randomly chooses a choice and the if statements decide who wins.

    #include <stdio.h>


    int main() {
    int player_Choice, CPU_Choice, c;


    printf ("Please enter your choice: \n 0 = Rock \n 1 = Paper \n 2 = Scissors \n 3 = Lizard \n 4 = Spock \n");
    scanf("%d", player_Choice);

    for (c = 1; c <= 1; c++) {
    CPU_Choice = rand()%5 + 1;
    }

    if (player_Choice == 0 && CPU_Choice == 0 || player_Choice == 1 && CPU_Choice == 1 || player_Choice == 2 && CPU_Choice == 2 || player_Choice == 3 && CPU_Choice == 3 || player_Choice == 4 && CPU_Choice == 4)
    {
    printf("\nYou picked: %d\n", player_Choice);
    printf("\nCPU picked: %d\n", CPU_Choice);
    printf("\nTie!");
    }
    if (player_Choice == 0 && CPU_Choice == 4 || player_Choice == 0 && CPU_Choice == 3 || player_Choice == 1 && CPU_Choice == 0 || player_Choice == 1 && CPU_Choice == 4 || player_Choice == 2 && CPU_Choice == 1 || player_Choice == 2 && CPU_Choice == 0 || player_Choice == 3 && CPU_Choice == 2 || player_Choice == 3 && CPU_Choice == 1 || player_Choice == 4 && CPU_Choice == 3 || player_Choice == 4 && CPU_Choice == 2)
    {
    printf("\nYou picked: %d\n", player_Choice);
    printf("\nCPU picked: %d\n", CPU_Choice);
    printf("\nYou won!");
    }
    if (player_Choice == 4 && CPU_Choice == 0 || player_Choice == 3 && CPU_Choice == 0 || player_Choice == 0 && CPU_Choice == 1 || player_Choice == 4 && CPU_Choice == 1 || player_Choice == 0 && CPU_Choice == 2 || player_Choice == 1 && CPU_Choice == 2 || player_Choice == 1 && CPU_Choice == 3 || player_Choice == 2 && CPU_Choice == 3 || player_Choice == 2 && CPU_Choice == 4 || player_Choice == 3 && CPU_Choice == 4)
    {
    printf("\nYou picked: %d\n", player_Choice);
    printf("\nCPU picked: %d\n", CPU_Choice);
    printf("\nThe CPU won!!");

    }
    return 0;

    }

    Please note that the if statements and the printf are all one line of code.
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,392
    Rep Power
    1871
    First, you should use [code][/code] tags when posting code. This preserves the formatting, hopefully making it easier to read.

    Second, enable all the warnings you can on your compiler.
    Code:
    $ gcc -Wall  foo.c
    foo.c: In function ‘main’:
    foo.c:9:1: warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘int’ [-Wformat]
    foo.c:12:1: warning: implicit declaration of function ‘rand’ [-Wimplicit-function-declaration]
    foo.c:15:1: warning: suggest parentheses around ‘&&’ within ‘||’ [-Wparentheses]
    foo.c:15:1: warning: suggest parentheses around ‘&&’ within ‘||’ [-Wparentheses]
    foo.c:15:1: warning: suggest parentheses around ‘&&’ within ‘||’ [-Wparentheses]
    foo.c:15:1: warning: suggest parentheses around ‘&&’ within ‘||’ [-Wparentheses]
    foo.c:21:1: warning: suggest parentheses around ‘&&’ within ‘||’ [-Wparentheses]
    foo.c:21:1: warning: suggest parentheses around ‘&&’ within ‘||’ [-Wparentheses]
    foo.c:21:1: warning: suggest parentheses around ‘&&’ within ‘||’ [-Wparentheses]
    foo.c:21:1: warning: suggest parentheses around ‘&&’ within ‘||’ [-Wparentheses]
    foo.c:21:1: warning: suggest parentheses around ‘&&’ within ‘||’ [-Wparentheses]
    foo.c:21:1: warning: suggest parentheses around ‘&&’ within ‘||’ [-Wparentheses]
    foo.c:21:1: warning: suggest parentheses around ‘&&’ within ‘||’ [-Wparentheses]
    foo.c:21:1: warning: suggest parentheses around ‘&&’ within ‘||’ [-Wparentheses]
    foo.c:21:1: warning: suggest parentheses around ‘&&’ within ‘||’ [-Wparentheses]
    foo.c:27:1: warning: suggest parentheses around ‘&&’ within ‘||’ [-Wparentheses]
    foo.c:27:1: warning: suggest parentheses around ‘&&’ within ‘||’ [-Wparentheses]
    foo.c:27:1: warning: suggest parentheses around ‘&&’ within ‘||’ [-Wparentheses]
    foo.c:27:1: warning: suggest parentheses around ‘&&’ within ‘||’ [-Wparentheses]
    foo.c:27:1: warning: suggest parentheses around ‘&&’ within ‘||’ [-Wparentheses]
    foo.c:27:1: warning: suggest parentheses around ‘&&’ within ‘||’ [-Wparentheses]
    foo.c:27:1: warning: suggest parentheses around ‘&&’ within ‘||’ [-Wparentheses]
    foo.c:27:1: warning: suggest parentheses around ‘&&’ within ‘||’ [-Wparentheses]
    foo.c:27:1: warning: suggest parentheses around ‘&&’ within ‘||’ [-Wparentheses]
    foo.c:9:6: warning: ‘player_Choice’ is used uninitialized in this function [-Wuninitialized]
    The first one is critical for you, you forgot the & on your scanf variable.
    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
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,175
    Rep Power
    2222
    Unreadable mess! You are undoubtedly not getting any responses because nobody wants to try to figure out that unreadable mess.

    Serious software development is a team effort. That means that others will need to read your code and be able to make sense out of it. That makes readablility a very high priority. Learn it now! Even if you're the only one to read your own code, you still need to be able to read your own code. Too many times we get problems posted here because the poor fool couldn't even read his own code. Do not be that guy!

    Here is your code properly formatted and rendered much more readable:
    Code:
    #include <stdio.h>
    
    int main() 
    {
        int player_Choice, CPU_Choice, c;
        
        printf ("Please enter your choice: \n 0 = Rock \n 1 = Paper \n 2 = Scissors \n 3 = Lizard \n 4 = Spock \n");
        scanf("%d", player_Choice);
        
        for (c = 1; c <= 1; c++) 
        {
            CPU_Choice = rand()%5 + 1;
        }
        
        if (player_Choice == 0 && CPU_Choice == 0 || 
            player_Choice == 1 && CPU_Choice == 1 || 
            player_Choice == 2 && CPU_Choice == 2 || 
            player_Choice == 3 && CPU_Choice == 3 || 
            player_Choice == 4 && CPU_Choice == 4)
        {
            printf("\nYou picked: %d\n", player_Choice);
            printf("\nCPU picked: %d\n", CPU_Choice);
            printf("\nTie!");
        }
        
        if (player_Choice == 0 && CPU_Choice == 4 || 
            player_Choice == 0 && CPU_Choice == 3 || 
            player_Choice == 1 && CPU_Choice == 0 || 
            player_Choice == 1 && CPU_Choice == 4 || 
            player_Choice == 2 && CPU_Choice == 1 || 
            player_Choice == 2 && CPU_Choice == 0 || 
            player_Choice == 3 && CPU_Choice == 2 || 
            player_Choice == 3 && CPU_Choice == 1 || 
            player_Choice == 4 && CPU_Choice == 3 || 
            player_Choice == 4 && CPU_Choice == 2)
        {
            printf("\nYou picked: %d\n", player_Choice);
            printf("\nCPU picked: %d\n", CPU_Choice);
            printf("\nYou won!");
        }
        
        if (player_Choice == 4 && CPU_Choice == 0 || 
            player_Choice == 3 && CPU_Choice == 0 || 
            player_Choice == 0 && CPU_Choice == 1 || 
            player_Choice == 4 && CPU_Choice == 1 || 
            player_Choice == 0 && CPU_Choice == 2 || 
            player_Choice == 1 && CPU_Choice == 2 || 
            player_Choice == 1 && CPU_Choice == 3 || 
            player_Choice == 2 && CPU_Choice == 3 || 
            player_Choice == 2 && CPU_Choice == 4 || 
            player_Choice == 3 && CPU_Choice == 4)
        {
            printf("\nYou picked: %d\n", player_Choice);
            printf("\nCPU picked: %d\n", CPU_Choice);
            printf("\nThe CPU won!!");
        }
        
        return 0;
    }
    Please note the use of a proper indenting style and that it has been used consistently. Note also the consistent observance of the levels of indentation. Note also the use of code tags, which are needed to preserve your code's indentation when posting here (HTML strips out all extra white space, such as the leading spaces, unless you use code tags to tell it not to).

    Also, why are you ignoring the warnings that the compiler is throwing at you? Never ignore warnings! Here is what MinGW gcc on WinXP gives me when I try to compile your code (refer to the reformatted listing above to match the line numbers):
    C:TEST>gcc -Wall ranger1.c
    ranger1.c: In function `main':
    ranger1.c:9: warning: format argument is not a pointer (arg 2)
    ranger1.c:12: warning: implicit declaration of function `rand'
    ranger1.c:15: warning: suggest parentheses around && within ||
    ranger1.c:15: warning: suggest parentheses around && within ||
    ranger1.c:15: warning: suggest parentheses around && within ||
    ranger1.c:15: warning: suggest parentheses around && within ||
    ranger1.c:21: warning: suggest parentheses around && within ||
    ranger1.c:21: warning: suggest parentheses around && within ||
    ranger1.c:21: warning: suggest parentheses around && within ||
    ranger1.c:21: warning: suggest parentheses around && within ||
    ranger1.c:21: warning: suggest parentheses around && within ||
    ranger1.c:21: warning: suggest parentheses around && within ||
    ranger1.c:21: warning: suggest parentheses around && within ||
    ranger1.c:21: warning: suggest parentheses around && within ||
    ranger1.c:21: warning: suggest parentheses around && within ||
    ranger1.c:27: warning: suggest parentheses around && within ||
    ranger1.c:27: warning: suggest parentheses around && within ||
    ranger1.c:27: warning: suggest parentheses around && within ||
    ranger1.c:27: warning: suggest parentheses around && within ||
    ranger1.c:27: warning: suggest parentheses around && within ||
    ranger1.c:27: warning: suggest parentheses around && within ||
    ranger1.c:27: warning: suggest parentheses around && within ||
    ranger1.c:27: warning: suggest parentheses around && within ||
    ranger1.c:27: warning: suggest parentheses around && within ||

    C:TEST>
    Here is Line 9:
    Code:
    scanf("%d", player_Choice);
    The warning complains, "format argument is not a pointer (arg 2)". scanf needs a pointer to the variable, not its value. scanf needs to know where to put the value that it reads in. Instead of giving it the address of player_Choice, you give it the value of player_Choice which it will then use as an address. Since player_Choice is an uninitialized variable, it contains garbage, whatever random value just happens to have been written to that memory location before either by this program's start-up code or by another program. Since you are telling scanf to misinterpret that as an address, you are effectively using an uninitialized pointer, the most bone-headed variant of Russian Roulette known to Man. Virtually guaranteed to crash your program when the operating system terminates it in self-defense and with extreme prejudice.

    Give scanf the address of player_Choice.

    BTW, this was an object lesson in the essential need for you to never ignore warnings!

    ranger1.c:12: warning: implicit declaration of function `rand'

    You did not #include the header file for rand(). Correct that!

    As for the rest of the warnings, you wrote long complex conditions for the if statements and left it to the order of precedence of those relational operators, && and ||, to sort all that out. Here is what the compiler interprets you as having written:
    Code:
        if ( (player_Choice == 0 && CPU_Choice == 0) ) || 
            ( (player_Choice == 1) && (CPU_Choice == 1) ) || 
            ( (player_Choice == 2) && (CPU_Choice == 2) ) || 
            ( (player_Choice == 3) && (CPU_Choice == 3) ) || 
            ( (player_Choice == 4) && (CPU_Choice == 4) )
    Is that in fact what you intended? If not, then use parentheses to correct that.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    2
    Rep Power
    0
    Thank you for your help. I was able to correct those issues and now the code works great.

IMN logo majestic logo threadwatch logo seochat tools logo