0

I'm programming a text-based adventure game for my C Programming class, but for whatever reason, the code seems to break whenever I try to use "scanf" to get a player's input. Everything will print onto the console perfectly fine, but then when I include the line "scanf("%d", playerInput), nothing will print out and the program will run endlessly. Anyone know why this is the case? Here's my code:

#include <stdbool.h>
#include <stdio.h>

int main()
{
    printf("~ Doctor Crowley: Master of Death ~\n"); //Titlecard

    //Introductory narration =================================================================================================
    printf("\n- Narrator -\n");
    printf("You are Doctor Jonathan Crowley, an archeologist and wizard from the Arcane University in Aeternum.\n");
    printf("You awaken in your classroom on the first floor of the University. Last you remember, you were in your office\n");
    printf("on the sixth floor of the University.\n");

    // Player Actions (1) ========================================================================================================================
    printf("\n1. Stand up\n");
    printf("2. Look around\n");
    printf("-------------------------------------------\n");

    int playerInput; //player input variable
    printf("--> ");
    scanf("%d", &playerInput);

    return 0;
}
  • Line buffered output may be cached when it's not terminated by a newline. When you enter a value it should probably flush the buffers just before exiting. Either end a printf with `\n` or explicitly fflush(stdout); before calling scanf. – Cheatah Nov 25 '21 at 18:37
  • 1
    What happens if you type a number and press Enter? – Weather Vane Nov 25 '21 at 19:06

1 Answers1

0

First of all make sure to write '&' before any variable in scanf. If you don't write that, it will crash and force you to end the program. Then as you write in your code it just input number and will put that in variable playerInput, and nothing more. You didn't print anything after scanf, so nothing will happen after that. For example you can write printf("%d",playerInput); after that and it will print the number that is in playerInput.