Why do I get this error?

This is a discussion on Why do I get this error? within the C Programming forums, part of the General Programming Boards category; Code: /* employee.c: manipulates Employees */ #include "employee.h" #include <stdio.h> #include <string.h> #define MAXEMPS 10 struct Employee{ char last[16]; char ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    1

    Question Why do I get this error?

    Code:
    /* employee.c: manipulates Employees */
    #include "employee.h"
    #include <stdio.h>
    #include <string.h>
    
    #define MAXEMPS 10
    struct Employee{
           char last[16];
           char first[11];
           char title[5];
           int home_runs;
    
    };
    static struct Employee players[MAXEMPS];
    static int numEmp=0;
    int addEmployee(void){
       
         if( numEmp < MAXEMPS ){
              printf("Enter last name: "); fflush(stdout);
              gets(players[numEmp].last);
                   if(strlen(players[numEmp].last)==0)
                     return -1;
               printf("Enter first name: "); fflush(stdout);
               gets(players[numEmp].first);
               printf("Enter title: "); fflush(stdout);
               gets(players[numEmp].title);
               printf("Enter home runs: "); fflush(stdout);
               scanf("%d", &players[numEmp].home_runs);
               getchar();
            int index=numEmp++;
               return index;
        }else{
             return -1;
        }
    
    }

    When I compile the above file I get the following error
    Can you please explain me why this is happening.Why does it say that declarations(local, int index) is not allowed.

    C:\C Language\ThinkingInC\Chapter06\functions\employee_ modularized>bcc32 lab6.c
    employee.c
    Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
    lab6.c:
    employee.c:
    Error E2140 employee.c 30: Declaration is not allowed here in function addEmploy
    ee
    *** 1 errors in Compile ***

  2. #2
    Registered User GL.Sam's Avatar
    Join Date
    Aug 2009
    Posts
    88
    ANSI C is not like C++, where you can declare anything anywhere. Preferable declare an item at the start of function block.

    C99, hovewer, is less strict.
    The only good is knowledge and the only evil is ignorance.
    ~Socrates

  3. #3
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    602
    Simply just return numEmp++ rather than declaring int index. Also I stongly encourage you to upgrade to VisualStudio C++, 2008 at least. It may not hurt to also include stdlib.h as well.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,594
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  3. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 09:33 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 12:10 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21