Thread: Syntax error at end of input

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    7

    Syntax error at end of input

    I'm getting a "Syntax error at end of input" (last line). but i can't find what's wrong.

    Code:
    int main() {
    int ett;
    FILE *filen;
    int val;
    char filnamn[40];
    enum filtyp {db,txt,bin};
    typedef enum filtyp filtyp;
    printf("Skriv filnamn: ");
    scanf("%s", &filnamn);
    int i = 0;
    while( filnamn[i] != '.') {
    i++;
    }
    
    if(filnamn[i+1] == 't' || filnamn[i+1] == 'T' ) { 
    	if((filen = fopen(filnamn, "r")) == NULL) {
            	     printf("file don't exist");
    	     	exit(1);
            	}
            	binary = 0;
    } else if (filnamn[i+1] == 'b' || filnamn[i+1] == 'B') {
    	if((filen = fopen(filnamn, "rb")) == NULL) {
                    printf("file don't exist");
                    exit(1);
                    }
                    binary = 1;
                    }
    
    readfil(filen);
    meny();
    close(filen);
    return 0;
    }

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Well, if you would indent your code properly, you'd see the missing brace...

    Example:

    Code:
    int main( void )
    {
    	if( condition )
    	{
    		foo( );
    	}
    	else if( some_other_condition )
    	{
    		while( true )
    		{
    			bar( );
    		}	
    	}
    }
    Or maybe:

    Code:
    int main( void ) {
    	if( condition ) {
    		foo( );
    	}
    	else if( some_other_condition ) {
    		while( true ) {
    			bar( );
    		}	
    	}
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    scanf("%s", &filnamn);
    Don't use an ampersand (&) there. Since filnamn is an array, saying "filnamn" (or equivalently, "&filnamn[0]") gives you a pointer to its first element, which is what you want. No need to get the address of that.

    Consider printing newlines after your "file does not exist" equivalent error messages.

    Also, "close" is a UNIX function for closing files opened with "open"; both of these functions use int file descriptors to refer to files. Since you're using the standard C fopen() which deals in FILE*s, you'll want to use the corresponding fclose() which also uses FILE*s.

    And I hope you're #including at the least stdlib.h (for exit) and stdio.h (for printf etc.).
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Ooh, good catch there dwks.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Thanks. I stared at it long enough that I was bound to catch something . . . .

    I guess I could also mention how much easier it would be to assign a variable
    Code:
    const char *mode;
    to either point to the string "r" or "rb", rather than have duplicate fopen() code like that.

    My last note is that declaring variables in the middle of blocks, as the OP has done with
    Code:
    int i = 0;
    is C99 only. You may want to avoid it, it's up to you.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Avoiding Global variables
    By csonx_p in forum Windows Programming
    Replies: 32
    Last Post: 05-19-2008, 12:17 AM
  2. Need some help with C program writing
    By The_PC_Gamer in forum C Programming
    Replies: 9
    Last Post: 02-12-2008, 09:12 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. How can I read until the end of an input line?
    By Bad_Scooter in forum C++ Programming
    Replies: 4
    Last Post: 07-19-2003, 09:29 PM
  5. Reading integers until end of input
    By nivo in forum C Programming
    Replies: 7
    Last Post: 10-20-2001, 04:18 PM