Thread: What`s causing the console to crash when running this code?

  1. #1
    Registered User
    Join Date
    Feb 2017
    Posts
    2

    What`s causing the console to crash when running this code?

    Hi!
    I made a game, which is supposed to random a number in specified range and let you guess it 11 times.
    It keeps crashing and I can`t find the error in code. I`d be thankful to any help
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main()
    {
        int N = rand() % ((2000-0+1)+0) - 1000;
        int X;
        int turn = 0;
        printf("Game start.\n");
        while(turn < 11)
        {
            printf("Insert number: \n");
            scanf("%D", X);
            turn = turn + 1;
            if (X == N)
            {
                printf("Correct, the number was %d.\n press Enter, to exit.", N);
                scanf("");
                exit(0);
            }
            else
            if (X < N)
            {
                printf("Bollocks, too small.");
            }
            else
            if (X > N)
            {
                printf("Nice try, but too big.");
            }
            else
            {
                printf("You lost. The number was %d.\n Press Enter, to exit.", N);
             };
        };
    }

  2. #2
    Registered User
    Join Date
    May 2015
    Posts
    56
    Hello,

    You've got to seed the random number call.

    Have a look here at the last post.
    Generating random integer values within a range in C - Stack Overflow

    and why the ';' after the '}' ?

    Regards
    Last edited by mad-hatter; 02-17-2017 at 04:20 PM. Reason: spelling

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,994
    > scanf("%D", X);
    1. %D isn't a valid format.
    2. You forgot the &, as in &X.

    > scanf("");
    An empty scanf isn't going to do anything.
    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. #4
    Registered User
    Join Date
    Feb 2017
    Posts
    2
    Thanks. It was my spelling after all. I`m relatively new to this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. if statements causing program to crash
    By mantracker in forum C++ Programming
    Replies: 4
    Last Post: 11-10-2012, 11:45 PM
  2. Is my code causing my system to crash?
    By camel-man in forum C Programming
    Replies: 1
    Last Post: 04-14-2011, 05:54 PM
  3. fscanf causing a crash
    By dougwilliams in forum C Programming
    Replies: 6
    Last Post: 11-18-2007, 04:52 PM
  4. what is causing this seemingly simple app to crash?
    By Shadow12345 in forum C++ Programming
    Replies: 6
    Last Post: 12-06-2002, 08:36 PM
  5. allegro causing a crash
    By kooma in forum Game Programming
    Replies: 5
    Last Post: 04-06-2002, 02:01 PM

Tags for this Thread