C Calender Help - Unusual error


 
Thread Tools Search this Thread
Top Forums Programming C Calender Help - Unusual error
# 1  
Old 03-26-2007
C Calender Help - Unusual error

I'm making a program that you input the month and year, and it creates a calender for that month of that year. This is my largest project yet, and I broke it up into several source files.

cal.c
Code:
#include "cal.h"
#include <stdio.h>

main() {
	int month, year;

	scanf("%d %d", &month, &year);
	
	if (yearstart(year) == 0)
		printf("error: year (%d) is lower then 1989:\n\tprogram will continue, but may give incorrect results\n\a", year);
	if (yearend(year) == 0)
		printf("error: year (%d) is larger then 2017:\n\tprogram will continue, but may give incorrect results\n\a", year);
	if (monthstart(month) == 0) {
		printf("error: month (%d) is lower then 1:\n\tthis error makes the program unusable; program will end now\n\a", month);
		return 0;
	}
	if (monthend(month) == 0) {
		printf("error: month (%d) is higher then 12:\n\tthis error makes the program unusable; program will end now\n\a", month);
		return 0;
	}

	monthprint(month);
	printf("\n");
}

cal.h
Code:
#define YEARSTART  1989
#define YEAREND    2017
#define MONTHSTART 1
#define MONTHEND   12

#define JANNUMBER 1
#define FEBNUMBER 2
#define MARNUMBER 3
#define APRNUMBER 4
#define MAYNUMBER 5
#define JUNNUMBER 6
#define JULNUMBER 7
#define AUGNUMBER 8
#define SEPNUMBER 9
#define OCTNUMBER 10
#define NOVNUMBER 11
#define DECNUMBER 12

int yearstart(int);
int yearend(int);
int monthstart(int);
int monthend(int);

void monthprint(int);

error.c
Code:
#include "cal.h"
#include <stdio.h>

int yearstart(int year) {
	return (year < YEARSTART) ? 0 : 1;
}
int yearend(int year) {
	return (year > YEAREND) ? 0 : 1;
}
int monthstart(int month) {
	return (month < MONTHSTART) ? 0 : 1;
}
int monthend(int month) {
	return (month > MONTHEND) ? 0 : 1;
}

print.c
Code:
#include "cal.c"
#include <stdio.h>

const char jan[] = "January";
const char feb[] = "Febuary";
const char mar[] = "March";
const char apr[] = "April";
const char may[] = "May";
const char jun[] = "June";
const char jul[] = "July";
const char aug[] = "August";
const char sep[] = "September";
const char oct[] = "October";
const char nov[] = "November";
const char dec[] = "December";

void monthprint(int month) {
	switch(month) {
		case JANNUMBER:
			printf("%s", jan);
			break;
		case FEBNUMBER:
			printf("%s", feb);
			break;
		case MARNUMBER:
			printf("%s", mar);
			break;
		case APRNUMBER:
			printf("%s", apr);
			break;
		case MAYNUMBER:
			printf("%s", may);
			break;
		case JUNNUMBER:
			printf("%s", jun);
			break;
		case JULNUMBER:
			printf("%s", jul);
			break;
		case AUGNUMBER:
			printf("%s", aug);
			break;
		case SEPNUMBER:
			printf("%s", sep);
			break;
		case OCTNUMBER:
			printf("%s", oct);
			break;
		case NOVNUMBER:
			printf("%s", nov);
			break;
		case DECNUMBER:
			printf("%s", dec);
			break;
		defualt:
			printf("fatal error\n\a");
			break;
	}
}


And here is my compile method:
Code:
~/c/etc/calender $ ls
cal.c  cal.h  error.c  print.c
~/c/etc/calender $ gcc -o cal cal.c cal.h error.c print.c
/tmp/ccU3dRmi.o: In function `main':print.c:(.text+0x0): multiple definition of `main'
/tmp/ccscxcaw.o:cal.c:(.text+0x0): first defined here
collect2: ld returned 1 exit status

There is no main in print.c...is there?
# 2  
Old 03-26-2007
Check your print.c file:
Code:
#include "cal.c"
#include <stdio.h>

You have #included cal.c instead of cal.h. This is causing the multiple occurance of main. Fix that and it should compile.
# 3  
Old 03-26-2007
FWIW - consider the C standard library for most of what you're doing.
Code:
#include <time.h>
#include <stdio.h>

char *setday1(char *month, char *year, struct tm *input)
{
	struct tm *retval=NULL;
	char tmp[80]={0x0};
	
	sprintf(tmp,"01-%s-%s", month, year);
	if(strptime(tmp,"%d-%m-%Y", input) == NULL) /* format for dd-mm-yyyy */
		return NULL;
	return month;
}

int main(int argc, char **argv)
{
	struct tm working;
	char date[80]={0x0};

	if( setday1(argv[1], argv[2], &working)!=NULL)	
		strftime(date, 80, "%A, %B %d, %Y", &working);		
	else
	{
		fprintf(stderr, "Bad date\n");
		return 1;
	}
	printf("First day of the month: %s\n", date);	
	return 0;
}

man mktime
man strptime
man strftime
# 4  
Old 03-26-2007
Quote:
Originally Posted by blowtorch
Check your print.c file:
Code:
#include "cal.c"
#include <stdio.h>

You have #included cal.c instead of cal.h. This is causing the multiple occurance of main. Fix that and it should compile.
No idea how I missed that. Thanks.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Facing problem while having time popup from inline calender

I have CGI Perl script that contains date column and date popup will be displayed from inline calender. I had a html script for the same and converted the same to CGI script. html page worked fine but no luck with CGI script. Could anyone please look into the below script and let me know... (1 Reply)
Discussion started by: scriptscript
1 Replies

2. Shell Programming and Scripting

Enter number of days and get calender

Friends need assistance in getting a script either on shell or perl. Below is the situation Taking Today's calender into consideration with Month,Day,Year current .Using that i would like give number of days to get its month,day,year for future or past calender depending on the + or - days... (8 Replies)
Discussion started by: ajayram_arya
8 Replies

3. UNIX for Dummies Questions & Answers

Awk/sed to play on calender

Hello Awk'inas/Sed'ers; This is keep ringing on my mind for a while, onto play in calender with awk or sed :cool:. Given a date, month and Year would like to find out the day corresponding to it. Am still a noob on awk and sed, hence would like to learn it from your responses. Here it is;... (11 Replies)
Discussion started by: sathyaonnuix
11 Replies

4. UNIX and Linux Applications

Calender/docket software

We are running in a Linux/Samba environment. Can anyone suggest calendar/docket software that will run in our environment? (0 Replies)
Discussion started by: kbweiss
0 Replies

5. UNIX Desktop Questions & Answers

While Connecting to Google networking. Error = Unusual traffic from your computer network.

Hello, I am working in office, where, more than 60 clients machines (only 16 machines are on windows) are there and one server Centos Server, I have configured clients with server, so that internet will be used form only one IP. Only 1 ip is assigned, but now a days, my client machines are... (2 Replies)
Discussion started by: RedRocks!!
2 Replies

6. Shell Programming and Scripting

Question on Autosys calender date.

Hi I am trying to schedule a job through Autosys through UNIX on a particular day of every month (for example 20th of every month). Can some one please help me whats the command or whats the process to run on that particular day of month. Thank you, (2 Replies)
Discussion started by: sravuri
2 Replies

7. Fedora

Script to find out first day of our calender

I try to find the first day of our calender. So I used this script ... echo -n "The week of the date 01jan0001 : " echo -n `date -d 00010101 +%A` echo But its shows error bash-3.1$ sh first_day.shThe week of the date 01jan0001 : date: invalid date `00010101' (3 Replies)
Discussion started by: krishnampkkm
3 Replies

8. Shell Programming and Scripting

Calender Unix programming date issues

Hi, i;m beginner of Unix, i trying to use crontab to zip my log file automatically, below is my coding, some of the statement i don't know whether is correct or not. Pls help:) year=`date '+%Y'` month=`date '+%m'` day=`date '+%d'` day=`expr $day - 1` case $month in 1 | 3 | 5 | 7 | 8 | 9 |... (4 Replies)
Discussion started by: dannyd_y
4 Replies

9. Shell Programming and Scripting

Getting unusual error - moving files to another directory

Dear experts, Im having an unusual problem and its been giving me a bad headache. I have the script below #!/usr/bin/sh -x ticketinputdir=/IN_DATA1/tickets ticketoutputdir=/IN_DATA2 YDAY=`env TZ=A12B date '+%Y%m%d'` for i in INA INB INC IND INE do mkdir $ticketoutputdir/$i/$YDAY... (4 Replies)
Discussion started by: aismann
4 Replies

10. Solaris

Unusual error : KEY in LOCKED position ignoring debug enter sequence

Hi, This was very unusual situation I never came across & we have SUN engineers working on this. We have Sun Fire V240 Server, 2 1.5-GHz UltraSPARC IIIi CPU, Solaris 9 Installed During the boot-up it gives following message, <date> <server name> unix : KEY in LOCKED position ignoring... (5 Replies)
Discussion started by: sacrh
5 Replies
Login or Register to Ask a Question