Richard AR
Richard AR

Reputation: 59

my c program to print student details is not working

i build this simple c program that print details of the students . but for some odd reason the program is not working I really don't know how to fix this i've been trying to figure it out for a very long time.


#include <stdio.h>

int main()
{
   
   char name[20], section[20], department[20];
   
   int regi_number, phone_number;
  
   
   printf("enter your name: ");
   scanf(" %s", name);
   
    printf("enter your section: ");
   scanf(" %s", section);

    printf("enter your department: ");
   scanf(" %s", department);
   
    printf("enter your register number: ");
   scanf(" %d", regi_number);
   
    printf("enter your phone number: ");
   scanf(" %d", phone_number);
   
   
   printf("your name is %s", name);
   
   printf("your section is %s", section);
   
   printf("your department name is %s", department);
   
   printf("your register number is %d", regi_number);
   
   printf("your phone number is %d" phone_number);
   
   
   
}

Upvotes: 0

Views: 155

Answers (2)

David C. Rankin
David C. Rankin

Reputation: 84599

Continuing from my comment, and in addition to the good answer by @Beki, there are a number of additional issues you should address, and some you can optionally address.

The most important of all, is you cannot use any input function correctly, unless you Check The Return to determine whether the input succeeded or failed -- BEFORE -- you attempt to access the values stored in the variables filled by the input function. Otherwise, you invite Undefined Behavior. With scanf() (not recommended for user-input), you would use:

printf ("enter your register number: ");
if (scanf ("%d", &regi_number) != 1) {                   /* validate EVERY input */
    fputs ("error: invalid integer input.\n", stderr);   /* handle any error */
    return 1;
}

When taking string input with scanf() (again, not recommended), you must use the field-width modifier to protect your array bound, otherwise scanf() is no safer than gets() -- See: Why gets() is so dangerous it should never be used!

if (scanf ("19%s", name) != 1) {                       /* field-width protects array bounds */
    /* handle cancellation of input */
}

(note: the space in " %d" and " %s" is superfluous as both conversion specifiers discard leading whitespace. The only conversion specifiers that DO NOT discard leading whitespace are "%c", "%[..]" and "%n")

Use fgets() For ALL User-Input

To avoid the many scanf() pitfalls that trap all new C programmers, you are encouraged to use fgets() for all user-input, use strcspn() to trim the '\n' included by fgets() in the buffer, and if a conversion is needed use sscanf() to extract the needed information. The following example incorporates all suggestions and removes your reliance on Magic-Numbers by declaring a proper constant to use:

#include <stdio.h>
#include <string.h>

#define MAXC 20         /* if you need a constant, #define one (or more) */

int main (void) {
   
    char name[MAXC], section[MAXC], department[MAXC],
        buffer[MAXC];       /* additional array to use as a buffer for integer inputs */
    int regi_number, phone_number;
    
    fputs ("enter your name: ", stdout);            /* no conversion, fputs() is fine */
    if (fgets (name, MAXC, stdin) == NULL) {        /* read all input with fgets() */
        puts ("(user canceled input)");
        return 0;                                   /* cancelation is not an error */
    }
    name[strcspn (name, "\n")] = 0;                 /* trim \n from end of name */
    
    fputs ("enter your section: ", stdout);
    if (!fgets (section, MAXC, stdin)) {            /* read all input with fgets() */
        puts ("(user canceled input)");
        return 0;
    }
    section[strcspn (section, "\n")] = 0;           /* trim \n from end of name */
    
    fputs ("enter your department: ", stdout);
    if (!fgets (department, MAXC, stdin)) {         /* read all input with fgets() */
        puts ("(user canceled input)");
        return 0;
    }
    department[strcspn (department, "\n")] = 0;     /* trim \n from end of name */
    
    fputs ("enter your register number: ", stdout);
    if (!fgets (buffer, MAXC, stdin)) {             /* read all input with fgets() */
        puts ("(user canceled input)");
        return 0;
    }
    if (sscanf (buffer, "%d", &regi_number) != 1) { /* use sscanf to parse value */
        fputs ("error: invalid integer input.\n", stderr);
        return 1;                                   /* invalid input is an error */
    }
    
    fputs ("enter your phone number: ", stdout);
    if (!fgets (buffer, MAXC, stdin)) {             /* read all input with fgets() */
        puts ("(user canceled input)");
        return 0;
    }
    if (sscanf (buffer, "%d", &phone_number) != 1) {/* use sscanf to parse value */
        fputs ("error: invalid integer input.\n", stderr);
        return 1;
    }
    
    printf ("\nyour name is            : %s\n"
            "your section is         : %s\n"
            "your department name is : %s\n"
            "your register number is : %d\n"
            "your phone number is    : %d\n",
            name, section, department, regi_number, phone_number);
}

(note: only a single call to printf() is necessary)

Example Use/Output

$ ./bin/input_name_sect_dept
enter your name: Mickey Mouse
enter your section: Toons
enter your department: Toontown
enter your register number: 12345
enter your phone number: 5551212

your name is            : Mickey Mouse
your section is         : Toons
your department name is : Toontown
your register number is : 12345
your phone number is    : 5551212

Look things over and let me know if you have further questions.

Upvotes: 1

Beki
Beki

Reputation: 1756

If you want to scanf a number, you need to put & symbol before your variable name. Also, you forgot , in your printf("your phone number is %d" phone_number); line.

Here is the corrected version:

#include <stdio.h>

int main()
{
   
   char name[20], section[20], department[20];
   
   int regi_number, phone_number;
  
   
   printf("enter your name: ");
   scanf(" %s", name);
   
    printf("enter your section: ");
   scanf(" %s", section);

    printf("enter your department: ");
   scanf(" %s", department);
   
    printf("enter your register number: ");
   scanf(" %d", &regi_number);
   
    printf("enter your phone number: ");
   scanf(" %d", &phone_number);
   
   
   printf("your name is %s", name);
   
   printf("your section is %s", section);
   
   printf("your department name is %s", department);
   
   printf("your register number is %d", regi_number);
   
   printf("your phone number is %d", phone_number);
}

Here is a bit about scanf and numbers:

You must put & in front of the variable used in scanf. The reason why will become clear once you learn about pointers. It is easy to forget the & sign, and when you forget it your program will almost always crash when you run it.

Upvotes: 2

Related Questions