Thread: Compiler error

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    17

    Compiler error

    Parse error, expecting ''," or 'SEP'

    on this line of code:
    Code:
    if((days-i)>=7)
    		{ printf("%d %d %d %d %d %d %d\n",i++,i++,i++,i++,i++,i++,i++);
    		} else {
    			for((days-i)>0,i++){
    				printf("%d",i);
    			}
    		}
    I'm new to C, and I don't see what I'm missing...
    If you need to see more of the code, let me know.

    Thank you

  2. #2
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    You can't have multiple i++ on the same line. The result is undefined.
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >for((days-i)>0,i++)
    The grammar for a for loop is
    Code:
    for ( init(opt); condition(opt); increment(opt) )
      statement;
    >The result is undefined.
    If the code doesn't compile, undefined behavior is a moot point.

    [edit]
    By the way, if you're new to C then I don't recommend trying to pack everything so tightly. You'll make mistakes and then not be able to fix them from lack of experience with how C does things.
    [/edit]
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    17
    [QUOTE=Prelude]>for((days-i)>0,i++)
    The grammar for a for loop is
    [code]
    for ( init(opt); condition(opt); increment(opt) )
    statement;

    hmm, I'm a little confused... I don't have any init(opt) for that line, so am I supposed to put
    Code:
    for ( ; days-i > 0 ; i++)
    And I fixed the problem with more than one i++, but I am still getting the same error... My new code looks like this:

    Code:
    for (int i=1, i<=days)
    	{	if(days-i>=7)
    		{ for (int x=0,x<7, x++){
    			printf("%d ",i++);
    		} else {
    			for (days-i>0,i++){
    				printf("%d",i);
    			}
    		printf("\n");
    		}
    	}

  5. #5
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    You are still getting the same error because your for loop is still wrong. Here is an example of how a for loop works:

    Code:
    int i;
    
    for(i = 0; i < 10; i++)
      printf("i = %d\n",i);

  6. #6
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    for(int i = 1; i <= days; )

    for(int x = 0; x < 7; x++)

    for( ;days - i > 0; i++)

    ; not ,
    and you CANNOT skip a ; even when there's nothing for it to 'separate'.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  7. #7
    Registered User
    Join Date
    Sep 2004
    Posts
    17
    ah, ok I didn't realize it was ; ...
    I went through and changed them, but I am still getting the same error, only now it is showing the error only for

    Code:
    for (int i=1
    I don't know why it is splitting that for loop...

    here is my new code:
    Code:
    for (int i=1; i<=days; )
    {	if(days-i>=7)
    	{        for (int x=0; x<7 ; x++){
    		  printf("%d ",i++);
    	} else {
    		  for (;days-i>0;i++){
    			printf("%d",i);
    		  }
    	printf("\n");
    	}
    	}
    }

  8. #8
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Ok, I'm just noticing that your brackets aren't lined up properly. Your inner for loop is missing a closing brace, although altogether you have the right number of opening/closing brackets. I'll clean it up for you:
    Code:
    for (int i=1; i<=days; )
    {
    	if(days-i>=7)
    	{
    		for (int x=0; x<7 ; x++)
    		{
    			printf("%d ",i++);
    		}  //This was missing
    	}
    	else
    	{
    		for (;days-i>0;i++)
    		{
    			printf("%d",i);
    		}
    		printf("\n");
    	}  //There was an extra closing brace here
    }
    Or, to get rid of some un-useful braces:
    Code:
    for (int i=1; i<=days; )
    {
    	if(days-i>=7)
    	{ //This can be removed, but easier to read with it (and below too)
    		for (int x=0; x<7 ; x++)
    			printf("%d ",i++);
    	} //<--
    	else
    	{
    		for (;days-i>0;i++)
    			printf("%d",i);
     
    		printf("\n");
    	}
    }
    Unless you're really experienced, it's good practice to line up your brackets like that so that it's easy to see when you've got a missing or misplaced bracket. Even if it takes more space, it's 'safer' to use and will keep those debugging headaches away.
    Last edited by Hunter2; 09-11-2004 at 07:01 PM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  9. #9
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Declare i outside of the for loop. That could be what your compiler is complaining about.

  10. #10
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Is that not allowed? I know that MSVC allows it (that's what I've always done, and I've seen several textbooks do it), but then MSVC 6 isn't exactly standards compliant.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  11. #11
    Registered User
    Join Date
    Sep 2004
    Posts
    17
    ah, thank you ... unfortunately the compiler is still giving me the same error, which makes me wonder if it is because I am missing something farther up in the program.
    So here is my entire program:
    Code:
    #include <stdio.h>
    
    int julian(int yr, int mn, int dy)
    {       long k1, jln;
            int kyr, k2;
    
            kyr = yr+4712;
            k1 = (long)365*kyr+(kyr+3)/4;
            k2 = (int)(30.6*mn- (mn<3?30.1:(kyr%4==0)?31.3:32.3))-1;
            jln = k1+k2+dy;
            if (jln>2361221) {
                    kyr = yr-300;
                    if (mn<3)
                            kyr--;
                    jln -= kyr/100*3/4+1;
            }
            return jln;
    }
    
    /* 
    Function for printing a month 
    Takes name of month, number of days in month, and start day of month
    */
    
    pmonth(char month[13],int days,int start)
    {	printf("%d\n", month);
    	printf("S M T W T F S");
    	
    	/* To print first calandar line */
    	switch (start)	
    	{	case 0: printf("%d %d %d %d %d %d %d\n", 1, 2, 3, 4, 5, 6, 7);
    			break;
    		case 1: printf("  %d %d %d %d %d %d\n", 1, 2, 3, 4, 5, 6);
    			break;
    		case 2: printf("    %d %d %d %d %d\n", 1, 2, 3, 4, 5);
    			break;
    		case 3: printf("      %d %d %d %d\n", 1, 2, 3, 4);
    			break;
    		case 4: printf("        %d %d %d\n", 1, 2, 3);
    			break;
    		case 5: printf("          %d %d\n", 1, 2);
    			break;
    		case 6: printf("            %d\n", 1);
    			break;
    	}
    	
    	/* Print remaining calandar lines */
    	for (int i=1; i<=days; )
    	{
    		if(days-i>=7)
    		{
    			for (int x=0; x<7 ; x++)
    			{
    				printf("%d ",i++);
    			}
    		}
    		else
    		{
    			for (;days-i>0;i++)
    			{
    				printf("%d",i);
    			}
    		
    		printf("\n");
    		}
    	}
    }
    
    
    
    main ()
    {	int year, start, jan, feb, mar, apr, may, jun, jul, aug, sep, nov, dec;
    		
    	/*Prompt for year */
    	printf("Enter Calandar Year");	
    
    	/* Read in year */
    	scanf(%i,year);
    	
    	/* Check if leap year */
    	if(year/100==0)
    	{	if(year/400==0)
    		{	leap=1;
    		}else{
    			leap=0;
    		}
    	}else{
    		if(year/4==0)
    		{	leap=1;
    		}else{
    			leap=0;
    		}
    	}	
    
    	/* Set month start dates */
    	jan = (julian(year, 1, 1) +1)%7;
    	feb = (jan + 31)%7;
    	mar = if(leap==0)
    		{ 	(feb+28)%7;
    		} else {
    			(feb+29)%7;
    		}
    	apr = (mar +31)%7;
    	may = (apr +30)%7;
    	jun = (may +31)%7;
    	jul = (jun +30)%7;
    	aug = (jul +31)%7;
    	sep = (aug +31)%7;
    	oct = (sep +30)%7;
    	nov = (oct +31)%7;
    	dec = (nov +30)%7;
    
    	/* Print out this years calandar */
    	switch(leap)
    	{	case 0: pmonth("   January",31,jan);
    			pmonth("   February",28,feb);
    			pmonth("    March  ",31,mar);
    			pmonth("    April",30,apr);
    			pmonth("     May",31,may);
    			pmonth("    June",30,jun);
    			pmonth("    July",31,jul);
    			pmonth("   August",31,aug);
    			pmonth("  September",30,sep);
    			pmonth("   October",31,oct);
    			pmonth("  November",30,nov);
    			pmonth("December",31,dec);
    			break;
    		
    		/* Leap year calandar */
    		case 1: pmonth("   January",31,jan);
    			pmonth("   February",29,feb);
    			pmonth("    March  ",31,mar);
    			pmonth("    April",30,apr);
    			pmonth("     May",31,may);
    			pmonth("    June",30,jun);
    			pmonth("    July",31,jul);
    			pmonth("   August",31,aug);
    			pmonth("  September",30,sep);
    			pmonth("   October",31,oct);
    			pmonth("  November",30,nov);
    			pmonth("December",31,dec);
    			break;
    	}
    
    }

  12. #12
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by Hunter2
    Is that not allowed? I know that MSVC allows it (that's what I've always done, and I've seen several textbooks do it), but then MSVC 6 isn't exactly standards compliant.
    It's best to always declare all of your variables at the top of a code block (after an opening brace) before any statements appear.

    To the OP, you might want to consider a different style of braces. I don't use TOTBS for the exact reason demonstrated here. It's hard to tell which closing brace goes with which opening brace.

    Try:
    Code:
    if(somevar)
    {
      if(someothervar)
      {
        // some code here
      }
    }
    ...instead of:
    Code:
    if(somevar) {
      if(someothervar {
        // Some code here
      }
    }
    You can see in the first style that it's much easier to follow the opening/closing of braces. Of course, it all ends up being up to personal preference
    If you understand what you're doing, you're not learning anything.

  13. #13
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    I get so many errors with your code that it's not even funny...I'll see if I can't figure out what's going on in there.

    EDIT: Forget it...start smaller...much smaller. Here's a couple of things that I ran into just looking through it quickly. if() doesn't have a return value. You can't do something like a = if(cond) b; else c;. Try this instead: a = cond ? b : c;. Also, the first argument to scanf() should be a string...%i isn't a string, but "%i" is. You're using a variable called leap but it doesn't exist. Seriously, I must have gotten about 200 errors from that code.
    Last edited by itsme86; 09-11-2004 at 07:18 PM.
    If you understand what you're doing, you're not learning anything.

  14. #14
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Quote Originally Posted by itsme86
    I get so many errors with your code that it's not even funny...I'll see if I can't figure out what's going on in there.

    EDIT: Forget it...start smaller...much smaller. Here's a couple of things that I ran into just looking through it quickly. if() doesn't have a return value. You can't do something like a = if(cond) b; else c;. Try this instead: a = cond ? b : c;. Also, the first argument to scanf() should be a string...%i isn't a string, but "%i" is. You're using a variable called leap but it doesn't exist. Seriously, I must have gotten about 200 errors from that code.
    "%i" will read a string in? I think we are looking at the same code, and he is getting a value to store in the scalar variable year which is type int. Of course he missed the " " and the &...

    ~/
    Last edited by kermit; 09-11-2004 at 08:21 PM.

  15. #15
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    Every compiler I've used to compile C code, has always forced me to declare variables at the beginning of the block of they're scope.

    For example:
    Code:
    // I couldnt do this
    for(int loop = 0; loop < 2; loop++)
    
    // I had to
    int loop;
    
    for(loop = 0; loop < 2; loop++)
    That for me was the hardest thing to get used to (As far as differences between C and C++).
    What is C++?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 04:35 PM
  2. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  3. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 AM
  4. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM