GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 11-Sep-2004, 19:58
Newworld Newworld is offline
New Member
 
Join Date: Sep 2004
Posts: 7
Newworld is on a distinguished road

C programming trouble


I have a little experience in Java programming, but I am completely new to C. So I am a little rusty on my coding.

I'm having trouble with this program I'm trying to write: Some of the errors I have when I try to compile are:

Parse error, expecting ''," or 'SEP'
CPP / C++ / C Code:
for (int i=1; i<=days; )

Also:
Parse error, expecting ',' or 'SEP'
CPP / C++ / C Code:
 main ()
{	int year; 

Here is my entire program:
CPP / C++ / C 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
*/

void 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;
	int leap;
	int start;	
	
	/*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 */
	int jan = (julian(year, 1, 1) +1)%7;
	int feb = (jan + 31)%7;
	int mar = leap==0 ? (feb+28)%7 ; (feb+29)%7
	int apr = (mar +31)%7;
	int may = (apr +30)%7;
	int jun = (may +31)%7;
	int jul = (jun +30)%7;
	int aug = (jul +31)%7;
	int sep = (aug +31)%7;
	int oct = (sep +30)%7;
	int nov = (oct +31)%7;
	int 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;
	}

} 
Last edited by JdS : 13-Sep-2004 at 06:09. Reason: Please insert your example C/C++ codes between [c] and [/c] tags
  #2  
Old 11-Sep-2004, 20:25
crystalattice's Avatar
crystalattice crystalattice is offline
Flame War Instigator
 
Join Date: Apr 2004
Location: San Diego
Posts: 1,534
crystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nice
I just finished a C programming course, but I feel I can address your errors at least. ;-)
CPP / C++ / C Code:
for (int i=1; i<=days; )

for (int x=0; x<7 ; x++)
These two lines require that i and x are defined outside the loop, i.e.
CPP / C++ / C Code:
int i;
for (i=1; i<=days);

int x;
for (x=0; x<7; x++)
This line:
CPP / C++ / C Code:
int mar = leap==0 ? (feb+28)%7 ; (feb+29)%7
requires you change the semicolon to a normal colon, if you are using a ternary operator. And don't forget the semicolon at the end of the line.
__________________
Common Sense v2.0-Striving to make the world a little bit smarter.
  #3  
Old 11-Sep-2004, 21:00
dabigmooish's Avatar
dabigmooish dabigmooish is offline
Member
 
Join Date: May 2004
Location: Baltimore (middle of Canton)
Posts: 165
dabigmooish will become famous soon enough
You can do this
CPP / C++ / C Code:
for (int i=1; i<=days; )

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

but the line
CPP / C++ / C Code:
for (int i=1; i<=days; )
needs to have an incrimenter for i (i.e. i++ at the end).

This line also needs to be fixed
CPP / C++ / C Code:
for (;days-i>0;i++)
you need to at least include an "i =something " statment at the beginning of it.

I'm also curious as to what this line is supposed to do
CPP / C++ / C Code:
        k2 = (int)(30.6*mn- (mn<3?30.1:(kyr%4==0)?31.3:32.3))-1;
__________________
"To argue with a person who has renounced the use of reason is like administering medicine to the dead."
-Thomas Paine
www.sullivan-county.com/deism.htm
  #4  
Old 11-Sep-2004, 21:14
Newworld Newworld is offline
New Member
 
Join Date: Sep 2004
Posts: 7
Newworld is on a distinguished road
I'm also curious as to what this line is supposed to do
CPP / C++ / C Code:
        k2 = (int)(30.6*mn- (mn<3?30.1:(kyr%4==0)?31.3:32.3))-1;
[/quote]

Heh, that segment of code was actually given to us from our professor as to generate the Julian date i.e. number of elapsed days since January 1, 4713 B.C. This function allows me to calculate Jan 1st for any year given. Unfortunately his code has some porblems as I get some "bad type" errors from his code because of the use of long's and int's.
  #5  
Old 11-Sep-2004, 21:29
dabigmooish's Avatar
dabigmooish dabigmooish is offline
Member
 
Join Date: May 2004
Location: Baltimore (middle of Canton)
Posts: 165
dabigmooish will become famous soon enough
You know what I've done a little research and I believe the mystery line is actually type casting to an int. I could be wrong though. What other errors are you getting?
__________________
"To argue with a person who has renounced the use of reason is like administering medicine to the dead."
-Thomas Paine
www.sullivan-county.com/deism.htm
  #6  
Old 11-Sep-2004, 21:34
Newworld Newworld is offline
New Member
 
Join Date: Sep 2004
Posts: 7
Newworld is on a distinguished road
Quote:
Originally Posted by dabigmooish
You know what I've done a little research and I believe the mystery line is actually type casting to an int. I could be wrong though. What other errors are you getting?

bad types for * multiply
Code:
k1 = (long)365*kyr+(kyr+3)/4;


args of different type
Code:
k2 = (int)(30.6*mn- (mn<3?30.1:(kyr%4==0)?31.3:32.3))-1;

and

wrong type for add
Code:
jln = k1+k2+dy;
  #7  
Old 12-Sep-2004, 12:16
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,234
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by dabigmooish
You can do this
CPP / C++ / C Code:
for (int i=1; i<=days; )

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

but the line
CPP / C++ / C Code:
for (int i=1; i<=days; )
needs to have an incrimenter for i (i.e. i++ at the end).

Not true. In C, you cannot define i in the for statement. crystalattice was correct. C++ allows it, but not C. And an iterator is optional. As long as the value i or days is modified in the loop the program works fine.


Quote:
Originally Posted by dabigmooish
This line also needs to be fixed
CPP / C++ / C Code:
for (;days-i>0;i++)
you need to at least include an "i =something " statment at the beginning of it.
And again, this is optional. As long as i is initialized before the loop, this works as expected.



Quote:
Originally Posted by dabigmooish
I'm also curious as to what this line is supposed to do
CPP / C++ / C Code:
        k2 = (int)(30.6*mn- (mn<3?30.1:(kyr%4==0)?31.3:32.3))-1;

It's obviously meant to confuse even the most learned programmers ;-)

Let's add whitespace and parens to make it understandable:
CPP / C++ / C Code:
k2 = (int)(30.6*mn- ( mn<3 ? 30.1  :  (kyr%4==0)  ?  31.3 : 32.3) ) - 1;

First thing I see is:
CPP / C++ / C Code:
(kyr%4==0)  ?  31.3 : 32.3
if kyr is exactly divisible by 4, you get 32.3 otherwise you get 31.3. Call this A which gives us:
CPP / C++ / C Code:
k2 = (int)(30.6*mn- ( mn<3 ? 30.1  :  A) ) - 1;
if mn is < 3 you get 30.1, otherwise A from above. Call it B
CPP / C++ / C Code:
k2 = (int)(30.6*mn- (B) ) - 1;
From there you can figure the rest out. It results in a floating point number which is converted to an integer and stored in k2


Oh, and one more thing... The proper format for main is
CPP / C++ / C Code:
int main()
You should always typecast main as an int -- some compilers will flag it as an error if you don't.


Quote:
Originally Posted by Newworld
bad types for * multiply
Code:
k1 = (long)365*kyr+(kyr+3)/4;


args of different type
Code:
k2 = (int)(30.6*mn- (mn<3?30.1:(kyr%4==0)?31.3:32.3))-1;

and

wrong type for add
Code:
jln = k1+k2+dy;
I don't see anything overtly wrong with these satements other than
1) no whitespace -- makes it understandable but won't affect the outcome
2) different types which is fine with C
Please post the exact errors with the lines that cause them. And let us know what compiler and OS you are using.
__________________

Cow: You're a lawyer too?
Mooseblood (mosquito): Ma'am, I was already a bloodsucking parasite. All I needed was a briefcase!
  #8  
Old 12-Sep-2004, 18:14
Newworld Newworld is offline
New Member
 
Join Date: Sep 2004
Posts: 7
Newworld is on a distinguished road
Quote:
I don't see anything overtly wrong with these satements other than
1) no whitespace -- makes it understandable but won't affect the outcome
2) different types which is fine with C
Please post the exact errors with the lines that cause them. And let us know what compiler and OS you are using.

Well I decided to try compiling the program on the unix machines at my school and the program compiled with out any errors...

When the program executes I get the prompt to "Enter Calendar Year", then when I type in an year like 2004 and hit return, a bunch of blank lines go by and it stops withe curser blink, but I cannot type anything else. I'm assuming I have an error with my scanf() line:

CPP / C++ / C Code:
 scanf("%d",&year);

here is the rest of the program:

CPP / C++ / C 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
*/

void pmonth(char month[13],int days,int start)
{	int i, x;
	printf("%s\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 (i=1; i<=days; )
	{
		if(days-i>=7)
		{
			for (x=0; x<7 ; x++)
			{
				printf("%d ",i++);
			}
		}
		else
		{
			for (;days-i>0;i++)
			{
				printf("%d",i);
			}
		}
		printf("\n");
		
	}
}



int main (){
	int jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec;
	int year;
	int leap;
	int start;	
	
	/*Prompt for year */
	printf("Enter Calendar Year \n");	

	/* Read in year */
	scanf("%d",&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 = leap==0?(feb+28)%7 : (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;
	}
	
}
Last edited by JdS : 13-Sep-2004 at 06:10. Reason: Please insert your example C/C++ codes between [c] and [/c] tags
  #9  
Old 12-Sep-2004, 23:06
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,234
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Your problem is in this section of code:
Code:
/* Print remaining calandar lines */ for (i=1; i<=days; ) { if(days-i>=7) { for (x=0; x<7 ; x++) { printf("%d ",i++); } } else { for (;days-i>0;i++) { printf("%d",i); } } printf("\n"); }
First try changing the "\n" to "." (or any other character) and see what happens.

Then try printing out your variables at key points to see what values you are generating -- this will help you pinpoint what values are not being set properly.



Also, for this code:
CPP / C++ / C Code:
 
{  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);
 ...
wouldn't this be more understandable:
CPP / C++ / C Code:
 
{  case 0: printf("1 2 3 4 5 6 7\n");
      break;
    case 1: printf("  1 2 3 4 5 6\n");
 ...
or this which would be faster:
CPP / C++ / C Code:
 
{  case 0: puts("1 2 3 4 5 6 7\n");
      break;
    case 1: puts("  1 2 3 4 5 6\n");
 ...

Just a thought...
__________________

Cow: You're a lawyer too?
Mooseblood (mosquito): Ma'am, I was already a bloodsucking parasite. All I needed was a briefcase!
 
 

Recent GIDBlogFirst week of IA training by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[Tutorial] GUI programming with FLTK dsmith FLTK Forum 10 03-Oct-2005 15:41
GUI programming crystalattice CPP / C++ Forum 5 14-Sep-2004 12:17
OZ programming language runner MySQL / PHP Forum 0 25-Aug-2004 02:49
Having trouble trying to format C: Nickster64 Computer Software Forum - Windows 2 27-Jul-2004 07:31
Need some exercises in C++, plus info on GUI programming. BlockAndBash CPP / C++ Forum 4 18-May-2004 20:07

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 06:59.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.