Thread: error: expected identifier or â(â before âwhileâ

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    42

    error: expected identifier or â(â before âwhileâ

    I have used this bit of code before without problems, however, when using it now, i'm getting the error listed in the title...

    I don't see anything wrong with it.. any help?

    Code:
            //Prompting user for Input file//
            while(resp == 'Y' || resp == 'y'){
                    printf("Please enter the file you'd like to read from: ");
                    scanf("%s", fileInName);
                    if((fopen(fileInName, "r")) == NULL){
                            printf("Could not open requested file. Would you like to enter another file? [Y/N]: ");
                            scanf("%*c %c", &resp);
                    }
                    else {
                            inFile = fopen(fileInName, "r");
                            break;
                    }
                    if(resp == 'n' || resp == 'N')
                            exit(1);
    
            }

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    so what do you have before while?
    and why are you calling fopen twice to open same file?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    That code looks fine to me. What comes before it? Perhaps a missing ; or unmatched parens or curly braces.

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    90
    I don't immediately see any problem, but I'm willing to bet the error is actually somewhere above this snippet of code.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Since the error message says something about "before while", I'm guessing we need to see a bit more code leading up to this point.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Feb 2011
    Posts
    42
    This is everything that exists above that bit of code

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define MaxSize 30
    int main(){
    
            FILE *inFile;
            FILE *outFile;
    
            char resp = 'Y';
            char fileInName[20];
            char fileOutName[20];
    
            int k;
            int numRecords;
    
            //Define struct//
            struct employee {
                    char surname[10];
                    char given[10];
                    char dept[20];
                    float payRate;
                    char eyeColor[8];
            }

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Yeah, your struct needs a trailing ;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Feb 2011
    Posts
    42
    Just caught it, its my first time using structs and i didn't realize i needed a semi-colon after the struct definition. sorry for the trouble folks. :-/

  9. #9
    Registered User
    Join Date
    Feb 2011
    Posts
    42
    thanks for the help

  10. #10
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Code:
    struct employee {
                    char surname[10];
                    char given[10];
                    char dept[20];
                    float payRate;
                    char eyeColor[8];
            }  ;
    and defining struct in main means the definition of this struct is local to main function only.
    Most of the time, I can assure you don't want it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. expected identifier
    By ozu in forum C Programming
    Replies: 5
    Last Post: 08-21-2010, 06:32 PM
  2. Error: expected identifier or ‘(’ before ‘{’ token
    By jpcanaverde in forum C Programming
    Replies: 66
    Last Post: 06-08-2010, 12:53 PM
  3. error C2061: syntax error : identifier
    By maninboots in forum C++ Programming
    Replies: 4
    Last Post: 07-02-2009, 05:40 AM
  4. c identifier error
    By walataza in forum C Programming
    Replies: 11
    Last Post: 03-03-2008, 03:51 AM
  5. Compiler error error C2065: '_beginthreadex; : undeclared identifier
    By Roaring_Tiger in forum Windows Programming
    Replies: 3
    Last Post: 04-29-2003, 01:54 AM