Thread: Rand # Game - Segmentation fault (core dumped) Error - More to Follow(maybe)

  1. #1
    Registered User
    Join Date
    Jun 2013
    Posts
    70

    Rand # Game - Play Again Do While Loop Issue - Further Down

    Ok, so doing an assignment for a random number game where you guess and it says too high or too low until you get it right or run out of tries. Here is what I got so far:

    Code:
    //Cameron Taylor
    
    #include<stdio.h>
    #include<stdlib.h>
    #include<time.h>
    
    int main(){
    
        srand(time(0));
        int number = rand()%100+1;;
        int guess, numberGuess;
    
        printf("Im thinking of a number between 1-100. You only have 7 chances, so choose carefully.");
        printf("Now, Take a Guess: ");
        scanf("%d", guess); 
    
    while(guess!=number && numberGuess<=7){        
    
        if(guess>number)
        {
            printf("Too high,Guess again: ");
            scanf("%d", guess);
            numberGuess++;
        }
        if(guess<number)
        {
            printf("Too low,Guess again: ");
            scanf("%d", guess);
            numberGuess++;
        }
        if(guess==number)
        {
            printf("Congratulations!, You guessed the number");
        } 
    }
    return 0;}
    Getting "Segmentation fault (core dumped)" after inputting the first number, but it compiles correctly.

    _________________________________________________

    I later have to add "Do you want to play again (y/n)", so I will use this post to continue that later.
    Last edited by Cameron Taylor; 06-25-2013 at 09:46 PM. Reason: 1 down 1 to go

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Compile at a higher warning level. Does the compiler warn you about something?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2013
    Posts
    70
    Quote Originally Posted by laserlight View Post
    Compile at a higher warning level. Does the compiler warn you about something?
    I just use:

    make Exercise6

    It spits out:

    cc Exercise6.c -o Exercise6



    How do I increase the level to make it show more?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The options to increase warning level depends on the compiler, but I'm not sure what compiler is cc. You probably can run:
    Code:
    cc --version
    to find out. After that, find the documentation for that compiler, searching online if you must. If you cannot find it, then tell us what is it.

    Anyway, take a closer look at this line:
    Code:
    scanf("%d", guess);
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jun 2013
    Posts
    70
    Quote Originally Posted by laserlight View Post
    The options to increase warning level depends on the compiler, but I'm not sure what compiler is cc. You probably can run:
    Code:
    cc --version
    to find out. After that, find the documentation for that compiler, searching online if you must. If you cannot find it, then tell us what is it.
    It is just GCC - cc (GCC) 4.7.2 20121109 (Red Hat 4.7.2-8)

    I did -Wall, but I couldn't make heads or tails of what the info it gave me meant.

    Quote Originally Posted by laserlight View Post
    Anyway, take a closer look at this line:
    Code:
    scanf("%d", guess);
    Bah... "&" was missing. That made it work. Thanks.

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Ok so here's an example of the warning you fixed.

    bla.c:13:5: warning: format '%d' expects argument of type 'int *', but argument
    2 has type 'int' [-Wformat]
    gcc will check printf and friends' arguments to make sure that they're right. Key things to comprehend are what gcc thought the problem format conversion was (%d), the type it expects (int * - as generated by &guess), what you actually passed in (int - guess), and which argument is exactly the problem if you counted them. It just takes a bit of reading comprehension.

  7. #7
    Registered User
    Join Date
    Jun 2013
    Posts
    70
    Quote Originally Posted by whiteflags View Post
    Ok so here's an example of the warning you fixed.


    gcc will check printf and friends' arguments to make sure that they're right. Key things to comprehend are what gcc thought the problem format conversion was (%d), the type it expects (int * - as generated by &guess), what you actually passed in (int - guess), and which argument is exactly the problem if you counted them. It just takes a bit of reading comprehension.
    This is what my compiler said when I compiled with -Wall:

    Exercise6: In function `_fini':
    (.fini+0x0): multiple definition of `_fini'
    /usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../lib64/crti.o:(.fini+0x0): first defined here
    Exercise6: In function `data_start':
    (.data+0x0): multiple definition of `__data_start'
    /usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../lib64/crt1.o:(.data+0x0): first defined here
    Exercise6:(.rodata+0x8): multiple definition of `__dso_handle'
    /usr/lib/gcc/x86_64-redhat-linux/4.7.2/crtbegin.o:(.rodata+0x0): first defined here
    Exercise6:(.rodata+0x0): multiple definition of `_IO_stdin_used'
    /usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../lib64/crt1.o:(.rodata.cst4+0x0): first defined here
    Exercise6: In function `_start':
    (.text+0x0): multiple definition of `_start'
    /usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../lib64/crt1.o:(.text+0x0): first defined here
    Exercise6: In function `_init':
    (.init+0x0): multiple definition of `_init'
    /usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../lib64/crti.o:(.init+0x0): first defined here
    /usr/lib/gcc/x86_64-redhat-linux/4.7.2/crtend.o:(.tm_clone_table+0x0): multiple definition of `__TMC_END__'
    Exercise6:(.data+0x8): first defined here
    /usr/bin/ld: error in Exercise6(.eh_frame); no .eh_frame_hdr table will be created.
    collect2: error: ld returned 1 exit status

  8. #8
    Registered User
    Join Date
    Jun 2013
    Posts
    70
    Ok, thanks to the help i got the game to work, but I also need to make it ask if the user wants to play again. This is what I have so far in a "Do While" loop.

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<time.h>
    
    int main(){
    
        srand(time(0));
        int number = rand()%100+1;
        int guess, numberGuess;
        char playagain='y';
    
        printf("Im thinking of a number between 1-100. You only have 8 chances, so choose carefully.");
        printf("Now, Take a Guess: ");
        scanf("%d", &guess); 
        numberGuess++;
    
    while (playagain != 'n'){
    
        while(guess!=number && numberGuess<=7){        
    
            if(guess>number)
            {
                printf("Too high,Guess again: ");
                scanf("%d", &guess);
                numberGuess++;
            }
            if(guess<number)
            {
                printf("Too low,Guess again: ");
                scanf("%d", &guess);
                numberGuess++;
            }
            if(guess==number)
            {
                printf("Congratulations!, You guessed the number");
            } 
        }
    
        do {
            printf("Would you like to play again?");
            scanf("%c", &playagain);
        }
            while (playagain !='y' && playagain != 'n');
    }    
    return 0;}
    Though, this line:

    printf("Would you like to play again?");

    is repeating twice back to back and when I enter in "y" it doesn't re-execute the program. Though when I type in 'n' it does exit the program.

  9. #9
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I would suggest:

    scanf(" %c", &playagain);

    You have a ton of left over newlines in the input that are causing scanf to behave weird. %c is different in that it does not ignore white space in the input.

    I I would factor out the call to scanf to read the guess in to some place below all the ifs, too. No need to repeat it in two places.

  10. #10
    Registered User
    Join Date
    Jun 2013
    Posts
    70
    Quote Originally Posted by whiteflags View Post
    I would suggest:

    scanf(" %c", &playagain);

    You have a ton of left over newlines in the input that are causing scanf to behave weird. %c is different in that it does not ignore white space in the input.

    I I would factor out the call to scanf to read the guess in to some place below all the ifs, too. No need to repeat it in two places.
    I will have to look into that, but do you know why it doesn't repeat the program when I put 'y' in on the "Would you like to play again?" line?

  11. #11
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    because it reads the '\n' left after the previous scanf

    most scanf formats will skip whitespaces before startign parsing the input stream. Not so for %c format - it starts parsing input stream where the previous scanf stopped.
    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

  12. #12
    Registered User
    Join Date
    Jun 2013
    Posts
    70
    Quote Originally Posted by vart View Post
    because it reads the '\n' left after the previous scanf

    most scanf formats will skip whitespaces before startign parsing the input stream. Not so for %c format - it starts parsing input stream where the previous scanf stopped.
    I think I got it with the " %c" help, but now once my program repeats it just allows the first guess before going back to the "do" line. Like so:

    commiedic@localhost Project1$ ./Exercise6
    Im thinking of a number between 1-100. You only have 8 chances, so choose carefully.Now, Take a Guess: 1
    Too low,Guess again: 1
    Too low,Guess again: 1
    Too low,Guess again: 1
    Too low,Guess again: 1
    Too low,Guess again: 1
    Too low,Guess again: 1
    Too low,Guess again: 1
    Would you like to play again? (y/n)y
    Im thinking of a number between 1-100. You only have 8 chances, so choose carefully.Now, Take a Guess: 1
    Would you like to play again? (y/n)n
    UPDATED CODE:

    Code:
    //Cameron Taylor
    
    #include<stdio.h>
    #include<stdlib.h>
    #include<time.h>
    
    int main(){
    
        srand(time(0));
        int guess, numberGuess;
        char playagain;
    
    
    
    while (playagain != 'n'){
    
        int number = rand()%100+1;;
        printf("Im thinking of a number between 1-100. You only have 8 chances, so choose carefully.");
        printf("Now, Take a Guess: ");
        scanf(" %d", &guess); 
        numberGuess++;
    
        while(guess!=number && numberGuess<=7){        
    
            if(guess>number)
            {
                printf("Too high,Guess again: ");
                scanf("%d", &guess);
                numberGuess++;
            }
            if(guess<number)
            {
                printf("Too low,Guess again: ");
                scanf("%d", &guess);
                numberGuess++;
            }
            if(guess==number)
            {
                printf("Congratulations!, You guessed the number");
            } 
        }
    
        do {
            printf("Would you like to play again? (y/n)");
            scanf(" %c", &playagain);
        }
            while (playagain !='y' && playagain != 'n');
    }    
    return 0;}

  13. #13
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    What do you think a value of numberGuess will be when you take your guess after pressing y?
    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

  14. #14
    Registered User
    Join Date
    Jun 2013
    Posts
    70
    Quote Originally Posted by vart View Post
    What do you think a value of numberGuess will be when you take your guess after pressing y?
    OHHHHHHH I forgot to reset the count...

    Added numberGuess = 0 above int number = rand()%100+1;

    Thanks so much. 5 min late, but hopefully my instructor will still accept it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation Fault(core dumped) error
    By gamers18 in forum C Programming
    Replies: 5
    Last Post: 12-05-2012, 01:53 PM
  2. ERROR: segmentation fault (core dumped)
    By sunny543 in forum C Programming
    Replies: 2
    Last Post: 08-23-2012, 03:07 AM
  3. Replies: 2
    Last Post: 05-18-2012, 06:27 AM
  4. Help with 'Segmentation fault (core dumped)' error?
    By Von Fuzzball in forum C Programming
    Replies: 12
    Last Post: 05-03-2011, 02:29 PM
  5. Runtime error 'segmentation fault (core dumped)'
    By mikemccready in forum C Programming
    Replies: 2
    Last Post: 03-17-2003, 07:14 AM