Program closes before taking input.

Why does my program close before taking the input for "k" and then displaying it.

I am writing a code for a menu based program so I need to take input from user after he has entered the information so I can have 1.Print names 2.Exit
while doing this I realized my program didnt take the input and just skipped the part where it is supposed to take value of l from user. So trying to debug it I deleted stuff and came down to this simple program and realized it still wont work any idea why?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
  


#include <stdio.h>
struct student
{
    char name[50];
    char lname[50];
    float marks;
} s[15];


int main ()
{ int i, j,k;

    printf("Please enter the number of students you are going to enter the information for.\n");
    scanf ("%d", &j);
   
        printf ("Please enter the information for students as asked.\n");
        
        
        for (i = 0; i < j; i++)
        {
            scanf ("%s %s %f\n", s[i].name, s[i].lname, &s[i].marks);
            
        }
        
    
    
    
    printf("Please enter a number\n");
        scanf ("%d", k);
       
        printf("your number was %d",k);
    
    
   return 0; 
}

Last edited on
Line 32
 
    scanf ("%d", k);
should be
 
    scanf ("%d", &k);

When using scanf , always make use of it's return value - to see that it has worked.

Look at the example here:

http://en.cppreference.com/w/c/io/fscanf
Topic archived. No new replies allowed.