As noted in my comments, you have two primary problems. The scanf ("%d/n", ...)
format string is just wrong unless the user is actually entering a "/n"
after each input and second, your loop variable is uninitialized in each for
loop.
But even more troubling is you fail to validate the return of scanf
on each call, so a single mistyped character can cause your integer conversion to fail silently and you proceed forward as if nothing is wrong. For example, if reaching for the 3
your user accidentally hits 'e'
, then a matching-failure occurs, character extraction from stdin
stops at that point, and if you are looping collecting integer input, you have just spun off into an infinite loop.
Always Validate Every Input
It takes nothing more than simple validation to save yourself from yourself. To validate you input you can do:
printf("How many students would you like to register: ");
if (scanf ("%d", &numOfStds) != 1) {
fputs ("error: invalid integer input - numOfStds.\n", stderr);
return 1;
}
and for another example:
for(int i = 0; i < numOfStds; i++){
printf ("Please enter student %d's number: ", i+1);
if (scanf("%d", &stdNum) != 1) {
fputs ("error: invalid integer input - stdNum.\n", stderr);
return 1;
}
students[i] = stdNum;
}
Putting it together in a full example, and then outputting the collected values you can do:
#include <stdio.h>
int main (void) {
int numOfStds;
int numOfCrs;
int stdNum;
printf("How many students would you like to register: ");
if (scanf ("%d", &numOfStds) != 1) {
fputs ("error: invalid integer input - numOfStds.\n", stderr);
return 1;
}
printf("How many courses are in the program?: ");
if (scanf ("%d", &numOfCrs) != 1) {
fputs ("error: invalid integer input - numOfCrs.\n", stderr);
return 1;
}
int students[numOfStds];
char courses[numOfCrs][8];
for(int i = 0; i < numOfStds; i++){
printf ("Please enter student %d's number: ", i+1);
if (scanf("%d", &stdNum) != 1) {
fputs ("error: invalid integer input - stdNum.\n", stderr);
return 1;
}
students[i] = stdNum;
}
for(int i = 0; i < numOfCrs; i++){
printf("Please enter course %d's code (must be 7 characters long): ",
i+1);
if (scanf ("%7s", courses[i]) != 1) {
fputs ("error: invalid integer input - numOfCrs.\n", stderr);
return 1;
}
}
/* example output */
// int registry[numOfStds][numOfCrs];
puts ("\nStudents:");
for (int i = 0; i < numOfStds; i++)
printf ("student: %d\n", students[i]);
puts ("\nCourses:");
for (int i = 0; i < numOfCrs; i++)
printf ("course: %s\n", courses[i]);
}
(note: above the protection of your array bounds with if (scanf ("%7s", courses[i]) != 1)
, if you fail to include the 7
as a field-width modifier, your user can enter as many characters as they wish resulting in Undefined Behavior)
Example Use/Output
$ ./bin/numofstds
How many students would you like to register: 3
How many courses are in the program?: 2
Please enter student 1's number: 301
Please enter student 2's number: 302
Please enter student 3's number: 303
Please enter course 1's code (must be 7 characters long): abcdefg
Please enter course 2's code (must be 7 characters long): bcdefgh
Students:
student: 301
student: 302
student: 303
Courses:
course: abcdefg
course: bcdefgh
Look things over and let me know if you have further questions.