Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

cannot get output of the program right

+4 votes
asked Oct 9, 2021 by Pranay Thakkar (160 points)


/* created a structure named personal*/

struct personal{

char name[20];

int day;

char month[10];

int year;

float salary;


int main()


int i,n,t;

    float totalsalary = 0;

char mon[10];

printf("Enter the number of employees\n");

scanf("%d", &n);

printf(" Input Values\n");

for(i=1; i<=n; i++)


scanf("%s %d %s %d %f", emp[i].name, &emp[i].day, emp[i].month, &emp[i].year, &emp[i].salary);


printf(" Output Values\n");

for(i=1; i<=n; i++)


    printf("%s %d %s %d %f\n", emp[i].name, emp[i].day, emp[i].month, emp[i].year, emp[i].salary);


    printf("enter the month for total salary\n");

    scanf("%s", mon);

    for(i=1; i<n; i++);


t = strcmp(emp[i].month, mon);



           totalsalary = totalsalary + emp[i].salary;



   printf("total salary for the month of %s is %f", mon, totalsalary);

return 0;


2 Answers

+1 vote
answered Oct 10, 2021 by Peter Minarik (55,660 points)

The problem is with your loop to calculate the total salary for a given month.

replace the incorrect

for (i = 1; i < n; i++);

with the correct

for (i = 1; i <= n; i++)

Notice that the end condition is different (i is less or equal to n), and there's no semicolon at the end of the line for the correct version.

There are further improvement opportunities.

Better input handling

Tell the user what they need to input. e.g. printf("Input Values (name, day, month, year, salary)\n"); Of course you can make it even better by specifying what you mean by month and day (name of the month/day or the index on a 1-31 or 1-12 scale). You can also add input validation if you really want to make things fail-safe.

Allocate the desired size of array

You have fixed your emp to 100 elements. Actually, you can set it to exactly n, as entered by the user, if you declare your emp array after the user has entered the desired number of employees.

printf("Enter the number of employees\n");
scanf("%d", &n);
struct personal emp[n];
0 votes
answered Oct 12, 2021 by Subham Agarwal (140 points)

scanf("%s %d %s %d %f", emp[i].name, &emp[i].day, emp[i].month, &emp[i].year, &emp[i].salary);

in this line you have to put  '&' before inputing all the values

commented Oct 12, 2021 by Peter Minarik (55,660 points)
edited Oct 12, 2021 by Peter Minarik
"in this line you have to put  '&' before inputing all the values"

Actually, no.

Arrays are memory addresses.

The values of arrays (e.g. char[20] or int[42]) are the same as their memory addresses. For this, you do not want to get the address (&) of them. Some compilers will ignore this, others get confused about it.

The address of (&) operator is needed for non-pointer types though, that's correct, but it was already correctly used by the original poster.

Assuming the following code, you can see the compiler warning:

#include <stdio.h>

struct Test
    int value;
    int array[10];

int main()
    struct Test test;
    char array[10] = { };
    scanf("%s", &array); // <<< warning for this line
    printf(": %s\n", array);
    printf(" array: %p\n", array);
    printf("&array: %p\n", &array);
    printf(" test.array: %p\n", test.array);
    printf("&test.array: %p\n", &test.array);
    return 0;


main.cpp:13:23: warning: format ‘%s’ expects argument of type ‘char*’, but argument 2 has type ‘char (*)[10]’ [-Wformat=]
     scanf("%s", &array);
: Hello
 array: 0x7fff2063ccae
&array: 0x7fff2063ccae
 test.array: 0x7fff2063cc84
&test.array: 0x7fff2063cc84
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.