0

I made the following program to input the credentials of a student and then print them. But when I'm done with inputting the records of two students (the maximum number of records inputs allowed), it is not printing them. Here's the program. Can anybody point out the mistake? Thank you.

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

struct student
{
    char name[20];
    char mobile_no[10];
    char class[5];
};

int main()
{
    static struct student s[2];
    int m=0,n=0,i;
    char c;
    printf("Enter the name , mobile_no and class of the students\n");
    while((scanf("%c",&s[m].name[n]))!= EOF)
    {
        for(n=0; n<=19; n++)
            scanf("%c",&s[m].name[n]);
        for(n=0; n<=9; n++)
            scanf("%c",&s[m].mobile_no[n]);
        for(n=0; n<=4; n++)
            scanf("%c",&s[m].class[n]);
        scanf("%c",&c);  //scans for the newline character \n
        n = 0;
        m++;
    }

    for(i=0 ; i<m ; i++)
    {
        printf("%s%3s%3s\n",s[i].name,s[i].mobile_no,s[i].class); //prints the structure
    }
}
Toby
  • 9,696
  • 16
  • 68
  • 132
Anant Vikram Singh
  • 109
  • 1
  • 3
  • 14
  • 1
    "it is not printing them" What does it print, then? What do you enter and what output do you expect? What do you do if a name is less than 19 characters etc.? – Gerhardh Aug 11 '17 at 10:29
  • 1
    Placing empty lines and proper indentation into the code might drastically increase the number of people willing to read that code. – Gerhardh Aug 11 '17 at 10:32
  • The mechanism used to read the data is weird. The user must type a character before the start of the name (the `scanf()` in the `while()` reads that), then type 19 characters for the name, the first of which will overwrite the one read previously; then, without any space, the user must type 9 characters of phone number; and then, without any space, 4 characters of class and the code reads one more character which is presumed to be a newline. The code doesn't ensure that any of the strings are null terminated. This is the wrong way to do it on many grounds, not least of which is the counting. – Jonathan Leffler Aug 11 '17 at 14:41

1 Answers1

-1

I do not do home works but you're lucky. Remember you cant enter longer than declared in the struct strings - 1. Do vetting, freeing etc yourself.

struct student
{
    char name[20];
    char mobile_no[10];
    char _class[5];
};

int read_students(struct student *s) 
{
    int m = 0;
    printf("Enter the name , mobile_no and class of the students. Name == exit for exit\n");
    while (1)
    {
        char tmp[20];
        printf("Name: ");
        scanf("%s", tmp);
        if (!strcmp(tmp, "exit")) break;
        if ((s = (struct student *)realloc(s, sizeof(struct student) * (m + 1))) == NULL) {m = -1; break;}
        strcpy(s[m].name, tmp);
        printf("Mobile: ");
        scanf("%s", s[m].mobile_no);
        printf("Class: ");
        scanf("%s", s[m]._class);

        m++;
    }

    if (m != -1)
    {   
        printf("Number of students: %d\n", m);

        for (int i = 0; i<m; i++)
        {
            printf("%s %3s %3s\n", s[i].name, s[i].mobile_no, s[i]._class);//prints the structure
         }
    }
    return m;
}

and in the main function:

struct student *s = NULL;
int numberofstudentds;

numberofstudentds = read_students(s);
0___________
  • 60,014
  • 4
  • 34
  • 74
  • You suggest to remove the whole while loop? – Gerhardh Aug 11 '17 at 10:30
  • @Gerhardh I do not suggest - those loops are something most ridiculous I have ever seen in my life – 0___________ Aug 11 '17 at 10:33
  • Isn't the answer to replace the complete `while` loop inclusive the 3 `for` loops by those 3 lines containing `scanf`? – Gerhardh Aug 11 '17 at 10:34
  • 1
    If the first block is removed, `m++;` is missing as well. ;) – Gerhardh Aug 11 '17 at 10:37
  • `Access violation writing location` – kocica Aug 11 '17 at 10:42
  • 4
    This is an example of an Answer which shouldn't be present on SO. You give no explanation. – Michi Aug 11 '17 at 10:54
  • @Michi and this an example of the very similar comment – 0___________ Aug 11 '17 at 11:05
  • Instead of **scanf("%s", tmp); if (!strcmp(tmp, "exit")) break;**. we can use only **if(scanf("%s", tmp ) == 0) break;**. this will avoid exit check using strcmp – Eswaran Pandi Aug 11 '17 at 13:36
  • @Eswaran Pandi .... why it is so good? Except that you have to enter Ctrl-Z for example. And it is hardware & OS dependant? – 0___________ Aug 11 '17 at 13:45
  • The use of `realloc()` is probably beyond what the OP understands as yet. The code does not test the return value from `scanf()`. If `realloc()` fails, you leak the previously allocated memory. The code leaks all the memory it did allocate because it only assigns to the local variable `s` without modifying the variable in the main program. I'm not convinced about the addition of `exit` as a termination. I'm not convinced about the sub-prompting. The code in the Q is bad. This is not great code either. And it isn't properly explained. – Jonathan Leffler Aug 11 '17 at 14:47
  • @ Jonathan Leffler i am not writing program for him. And I am not going to .... it is just general idea. Vettig freeing. checking and decorating was not the question – 0___________ Aug 11 '17 at 14:51
  • Don't cast the value returned from `alloc()`-type (eg `realloc()`) functions; https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc – Toby Aug 11 '17 at 14:51
  • 10+ years old rule. Do not use **implicit function definitions**. Compiled under C++ so cast was needed. @Toby if you do not know from C99 there are no **implicit function definitions**. This rule means nothing as if you do not include `stdlib` you will get another warning. If someone is ignoring them - it does not matter as one will ignore all of them – 0___________ Aug 11 '17 at 14:55
  • IIRC it's from C11 rather than C99? I'm not sure on that though. But there are other reasons not to cast, which are also detailed in the linked Q. Furthermore C should be compiled as C, C++ is a different language - if compiling as C++ is OK for you, that's fine, but please don't spread the habits that allows on to newbies. – Toby Aug 11 '17 at 15:01