When I compile this code:
#include <stdio.h>
void main() {
int n, i, total=0;
printf("Enter the number of employees");
scanf("%d", &n);
struct emprecord
{
int salary, total;
char name[50];
};
struct emprecord emp[50];
for (i=0; i<n; i++) {
printf("Enter the name of employee %d", i+1);
scanf("%s", &emp[i].name);
printf("Enter the salary of employee %d", i+1);
scanf("%d", &emp[i].salary);
total=total+emp[i].salary;
}
printf("Total salary is: %d", total);
}
I get the following errors and I am assuming all will be fixed once the first one is resolved:
C:\\Users\\shihab130489\\Documents\\Cpp18888r6u54ru.c(8) : error C2143:
syntax error : missing ';' before ''
C:\\Users\\shihab130489\\Documents\\Cpp18888r6u54ru.c(10) : error C2143:
syntax error : missing ';' before 'type'
C:\\Users\\shihab130489\\Documents\\Cpp18888r6u54ru.c(12) : error C2133: 'emp' : unknown size
C:\\Users\\shihab130489\\Documents\\Cpp18888r6u54ru.c(13) : error C2059: syntax error : 'for'
C:\\Users\\shihab130489\\Documents\\Cpp18888r6u54ru.c(13) : error C2143:
syntax error : missing '{' before '<'
C:\\Users\\shihab130489\\Documents\\Cpp18888r6u54ru.c(13) : error C2059: syntax error : '<'
C:\\Users\\shihab130489\\Documents\\Cpp18888r6u54ru.c(13) : error C2143:
syntax error : missing '{' before '++'
C:\\Users\\shihab130489\\Documents\\Cpp18888r6u54ru.c(13) : error C2059: syntax error : '++'
C:\\Users\\shihab130489\\Documents\\Cpp18888r6u54ru.c(13) : error C2059: syntax error : ')'
C:\\Users\\shihab130489\\Documents\\Cpp18888r6u54ru.c(20) : error C2143:
syntax error : missing ')' before 'string'
C:\\Users\\shihab130489\\Documents\\Cpp18888r6u54ru.c(20) : error C2143:
syntax error : missing '{' before 'string'
C:\\Users\\shihab130489\\Documents\\Cpp18888r6u54ru.c(20) : error C2059: syntax error : ''
C:\\Users\\shihab130489\\Documents\\Cpp18888r6u54ru.c(20) : error C2059: syntax error : ')'
C:\\Users\\shihab130489\\Documents\\Cpp18888r6u54ru.c(21) : error C2059: syntax error : '}'
Can someone please help with the first error? I am unable to understand what the issue is.
The Microsoft C compiler (prior to VS2013 , it seems) only accepts C89/C90, and that only allows type and variable definitions before any executable statements in a function. You are trying to declare the structures after some executable statements. That's valid in C++, and C99 and C11, but not in C90.
Hence:
#include <stdio.h>
int main(void)
{
int n, i, total = 0;
struct emprecord
{
int salary, total;
char name[50];
};
struct emprecord emp[50];
printf("Enter the number of employees");
if (scanf("%d", &n) != 1)
{
fprintf(stderr, "Did not read a number successfully\n");
return 1;
}
if (n <= 0 || n > 50)
{
fprintf(stderr, "Error: you entered %d but it should be in the range 1..50\n", n);
return 1;
}
for (i = 0; i < n; i++)
{
printf("Enter the name of employee %d", i+1);
if (scanf("%s", &emp[i].name) != 1)
break; // Sloppy but effective
printf("Enter the salary of employee %d", i+1);
if (scanf("%d", &emp[i].salary) != 1)
break; // Sloppy but effective
total += emp[i].salary;
}
printf("Total salary is: %d\n", total);
return 0;
}
It's cruel to make people enter names when you're only interested in the salary.
It is better to position the struct definition before main.
But, there is no error when I use gcc 4.5.1
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.