0
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>


int main()
{
   int ActChoice=0;
   //do
   //{
          printf("What Activity were you doing?");
          printf("\n1. Running" );
          printf("\n2. Swimming");
          printf("\n3. Cycling" );
          scanf("\n%d",ActChoice);

          /*if(ActChoice == 1)
          {
               RunEdit();
          }
          else if(ActChoice == 2)
          {
               SwimEdit();
          }
          else if(ActChoice == 3)
          {
               CyclEdit();
          }*/

   //}
  // while(1==1);
     getch();
}

Here i have a very simple piece of code designed to choose a desired function, however, on running this program, it crashes after i input "ActChoice" .

I don't know whether this is a mistake i have in my code here, or the code further down, But it seems to break at the scan.

Edit: I forgot the & , i'm actually retarded

7 Answers 7

2

Change scanf("\n%d",ActChoice);

to:scanf("\n%d",&ActChoice);

scanf() is expecting a pointer to a variable.

Sign up to request clarification or add additional context in comments.

Comments

2

Your scanf statement should be like this :

scanf("\n%d",&ActChoice);

You are missing the &.

Comments

2

Change scanf("\n%d",ActChoice); to scanf("\n%d",&ActChoice); scanf() needs the address of the variable where it has to store the input data.

Please check the man page of scanf() for details.

Comments

1

Change scanf("\n%d",ActChoice); to scanf("\n%d",&ActChoice); because the value sent to scanf is address of ActChoice. The second argument (&ActChoice) specifies the variable into which the typed response will be placed. In this case the response will be placed into the memory location associated with the variable ActChoice.

Comments

1

You need an ampersand in front of the name of the integer variable ActChoice when receiving input via scanf because it requires an address. Your code should look like the following:

scanf("\n%d", &ActChoice);

Comments

0

your program doesn't crash.It just doesn't know where to store the input you are giving.Just add a & before ActChoice in the scanf statement to resolve it.

your scanf statement should look like this,

scanf("\n%d",&ActChoice);

3 Comments

What do you mean by your program doesn't crash?
@SouravGhosh He specified in his question that his program crashes.It doesn't actually crash it just closes down.(I am new to c bro correct me if im wrong)
yes, it does crash. Reason is trying to access memory region which the process is not permitted to access. HInt: ActChoice = 0;
0

Scanf in c program expects the address where the data to be stored . In your case you have given

scanf("\n%d",ActChoice);

ActChoice = 0 so the scanf will try to store the value in location zero which is actually a program memory, so i would lead to segfault. this same program may work in Harvard architecture where the program memory is different from data memory.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.