0

Ok so here is my code

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


int main () {

    char names[10][20];
    int year[10], i, pos;
    float mo[10], max=-1;

    for (i=0; i<10; i++) {
        do {
            printf("Please enter your name:\n");
            gets(names[i]);
            if (strlen(names[i]) > 19)
                printf("The name can contain a maximum of 19 characters!\n");
        } while (strlen(names[i]) > 19);
        do {
            printf("Please enter your year of study:\n");
            scanf("%d", &year[i]);
            if (year[i]<1 || year[i]>5)
                printf("Incorrect value! Accepted values are 1-5\n");
        } while (year[i]<1 || year[i]>5);
        do {
            printf("Please enter your average score:\n");
            scanf("%f", &mo[i]);
            if (mo[i]<5 || mo[i]>10)
                printf("Incorrect value! Accepted values are 5-10\n");

        } while (mo[i]<5 || mo[i]>10);
        if (mo[i]>max)
                max=mo[i];
                pos=i;
    }
    printf("The student with the highest score is %s with %f. His/Her year of study is %d\n", names[pos], max, year[pos]);



}

I know that i should avoid gets but this was the recomended by my teacher. I also tried

scanf ("%[^\n]%*c", names[i]);

But i get the same problem. It reads the first name, but all the others not.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
GR G.
  • 1
  • 3
    gets gets bypassed? :) Anyway, don't use `gets` ever. It was removed from the language being dangerous. – Eugene Sh. Nov 21 '17 at 18:08
  • Try using `fgets` with a buffer limit and flushing `stdin` after getting the input from the user. – Nina Satragno Nov 21 '17 at 18:14
  • how to do that? I am not famliar with fgets. Is fgets(names[i],20,stdin) the correct way? It does not seem to work. In fact i get the same problem. It reads the first time and then it gets bypassed. And programm continues – GR G. Nov 21 '17 at 18:19

0 Answers0