-6

I decided to write as my first C program a slot-machine. However, I have ran into some difficulties.

scanf("%d", decision);
if (decision == 1)

It seems that whenever this part is used, the whole program crashes. Why?

EDIT: Answered. I had forgotten to include an ampersand before the "decision". Thanks everyone!

roschach
  • 8,390
  • 14
  • 74
  • 124
Silenite
  • 1
  • 1
  • 3
    @chevybow This question is both too broad and off-topic here. The suggestion to start with a simpler task and go through a book is a constructive suggestion. – Eugene Sh. May 17 '18 at 17:24
  • One obvious mistake: you have to use "==" for equality checks, not "=" – Ctx May 17 '18 at 17:25
  • 1
    To insert code, copy and paste the code into the body of your question, highlight it, and press the button with the curly-braces. – Christian Gibbons May 17 '18 at 17:25
  • @ChristianGibbons Ctrl-K is usually the shortcut key. – Eugene Sh. May 17 '18 at 17:25
  • And use `scanf("%d", &decision);`, maybe the missing & was the reason for your crashes – Ctx May 17 '18 at 17:25
  • 2
    If your problems are solved for now, you might consider to delete this question, since it will probably not be useful for future readers – Ctx May 17 '18 at 17:28

1 Answers1

2

Your problem comes from a simple error: you forgot the & to pass the address of decision instead of its value:

scanf("%d", &decision);

Note that this kind of error can be avoided by increasing the warning level of the compiler: gcc -Wall, clang -Weverything or cl /W4

Note also that you should check the return value of scanf(). It should return 1 if the number was correctly converted into decision. Otherwise using decision may have undefined behavior.

chqrlie
  • 131,814
  • 10
  • 121
  • 189