0

I am a newbie with C and I am trying to run a program from my book which shows how we deal with arrays of structures.

    #include<stdio.h>
    #include<conio.h>

struct employee
{
    int empno;
    char name[30];
    int basic;
    int hra;
};

void main()
{
    struct employee e[50];
    int i, j, n; 
    int net[50];
    float avg;

    printf("Enter the number of employees: ");
    scanf("%d", &n);
    printf("Enter Emp. No. \tName:\tBasic\tHRA of each employee in the order.\n");
    for(i=0; i<n; i++)
    {

        scanf("%d", &e[i].empno);
        gets(e[i].name);
        scanf("%d", &e[i].basic);
        scanf("%d", &e[i].hra);

    net[i]=e[i].basic + e[i].hra ;
    avg = avg + net[i];
    }

    avg = avg/n;

    printf("Emp. No \t Name-Netpay: ");
    for(i=0; i<n; i++)
    {
        if(net[i]>avg)
        {
            printf("\t",e[i].empno);
            printf("\t", e[i].name);
            printf("\t", net[i]);
        }    } }

I also have further modules which goes on to compute the average and prints those elements whose salary + hr is more than average. However the code pasted above does not work as intended.

Now, if I enter the number of employees - Let's say 1, it only allows me to enter the empno and name and exits the loop. I am expecting it to complete at least one cycle through the loop with the value 1.

Any suggestions on this would be highly appreciated and I apologize if I am messing up anywhere. Thanks.

  • What's your question? – LihO Oct 10 '13 at 16:25
  • 2
    Code does not compile (missing a closing brace), `#include ` is missing, `main()` should return `int`, the variable `j` is unused, and reading user input should be done using `fgets()` instead of `scanf()` (also, `gets()` should **always** avoided since it's insecure). Throw this book far away. –  Oct 10 '13 at 16:26
  • @H2CO3 - I did not copy it here but it's a part of my program which I am trying to compile. –  Oct 10 '13 at 16:27
  • @Vikash Then you need to understand that `gets()` reads an entire line, and that may not be what you want. In which format do you expect the input to be? –  Oct 10 '13 at 16:29
  • @H2CO3 I do apologize for the inconvenience - Please allow me to edit the question and paste the whole code for all of you. –  Oct 10 '13 at 16:34
  • I think you are using the book "Let us C". It is outdated for the most part. Throw it away – Umer Farooq Oct 10 '13 at 16:34
  • Do you get any error message? – Refael Oct 10 '13 at 16:35
  • @Vikash it was not necessary to add those extra lines :). But when you ask a question, you should provide a minimalist example of code that does not behave as you intend to. So we can compile it, fix the problem and answer. – Maxime Chéramy Oct 10 '13 at 16:42
  • @UmerFarooq Yeah this is an old book though :( –  Oct 10 '13 at 16:47
  • @H2CO3 - I was using the gets to input a string. –  Oct 10 '13 at 17:03
  • @Vikash Yes, saw that. You should have used `fgets()` instead, since `gets()` is dangerous (it doesn't let you specify a buffer size). –  Oct 10 '13 at 17:12

1 Answers1

1

You need to flush the line from the input before using gets (which is deprecated btw):

#include <stdio.h>

struct employee
{
    int empno;
    char name[30];
    int basic;
    int hra; 
};

int main()
{
    struct employee e[50];
    int i, j, n; 
    int net[50];
    float avg;

    printf("Enter the number of employees: ");
    scanf("%d", &n);
    printf("Enter Emp. No. \tName:\tBasic\tHRA of each employee in the order.\n");
    for(i=0; i<n; i++)
    {

        scanf("%d", &e[i].empno);
        char c;
        while ((c = getchar()) != EOF && c != '\n');

        gets(e[i].name);
        scanf("%d", &e[i].basic);
        scanf("%d", &e[i].hra);

        net[i]=e[i].basic + e[i].hra ;
        avg = avg + net[i];
    }
    return 0;
}

This is because scanf does not read the end of line (\n) but gets will and return immediately. scanf will read the name instead. Basically, it's a mess then :).

Maxime Chéramy
  • 17,761
  • 8
  • 54
  • 75
  • From what I heard, gets isn't even in c11 standard anymore. [LINK](http://stackoverflow.com/questions/12893774/what-is-gets-equivalent-in-c11) – zubergu Oct 10 '13 at 16:45
  • 1
    @zubergu yes. It should not be used for security matters. However, the problem faced here should be the same with `gets_s` (c11) or `fgets`. – Maxime Chéramy Oct 10 '13 at 16:47
  • @Maxime And anyway, the whole line should be inputted at once, then parsed by functions that *actually work* (scan-crappy-f is not such a function). –  Oct 10 '13 at 16:55
  • @Maxime: Thanks for the explanation. –  Oct 10 '13 at 18:03