It'sRainingMen
It'sRainingMen

Reputation: 183

C function program not working

#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

Upvotes: 0

Views: 120

Answers (7)

Vignesh K Viki
Vignesh K Viki

Reputation: 31

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.

Upvotes: 0

Hojin
Hojin

Reputation: 11

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);

Upvotes: 1

PU.
PU.

Reputation: 148

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.

Upvotes: 1

ajay kumar
ajay kumar

Reputation: 61

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);

Upvotes: 0

Sourav Ghosh
Sourav Ghosh

Reputation: 134286

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.

Upvotes: 2

Shwetha
Shwetha

Reputation: 150

Your scanf statement should be like this :

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

You are missing the &.

Upvotes: 2

bitcell
bitcell

Reputation: 941

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

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

scanf() is expecting a pointer to a variable.

Upvotes: 2

Related Questions