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", ®i_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", ®i_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.