Another extra number - C

Hello,

My input as:

2
2
one
3
two
4

But I have to enter another extra number to finish the program. Why?
How can I fix it?

Thanks

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
41
42
43
44
#include <stdio.h>
#include<string.h>
#define MAX_LIMIT 20 

struct student {
    char name[100];
    int ID;
    struct lesson {
        char name_lesson[100];
        int number_lesson;
    };
    struct lesson l[100];
    struct student_lesson {
        char n[100];
        int code;
    };
    struct student_lesson stl[100];
};
struct student st[100];

int main()
{
    ///number of lesson
    int n;
    scanf_s("%d\n", &n);
    ///number of student
    int m;
    scanf_s("%d\n", &m);
    /// Lessons details
    for (int i = 0; i < n; i++) {
        
        fgets(st[i].l[i].name_lesson,MAX_LIMIT,stdin);
        
        scanf_s("%d\n", &st[i].l[i].number_lesson);
    }

    for (size_t i = 0; i < n; i++)
    {
        printf_s("%s - %d\n", st[i].l[i].name_lesson, st[i].l[i].number_lesson);

    }
    
    return 0;
}
It depends on what you want.

How will the program know when you're done entering numbers? Put it this way, if I were putting the numbers in your program, how would I know when to stop?
The issue is with using \n with scanf_s and using fscan_f. Consider:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int main()
{
	///number of lesson
	int n;
	scanf_s("%d", &n);

	///number of student
	int m;
	scanf_s("%d", &m);

	/// Lessons details
	for (int i = 0; i < n; i++) {
		scanf_s("%19s", st[i].l[i].name_lesson);
		scanf_s("%d", &st[i].l[i].number_lesson);
	}

	for (size_t i = 0; i < n; i++)
	{
		printf_s("%s - %d\n", st[i].l[i].name_lesson, st[i].l[i].number_lesson);

	}

	return 0;
}

Topic archived. No new replies allowed.