-
October 16th, 2010, 06:41 PM
#1
Old algorithm, new language, trouble starting. 1 page yearly calendar program.
I've been programming since the early 80s. Done various languages, FORTRAN, BASIC, C, JAVA and others. Both in college and on the job, electrical engineer. So, I don't have a problem with logistical flow and algorithms. But I am new to C++.
20 years ago I had developed an algorithm in c to build a 1 page yearly calendar. The concept is to build up strings and print them out and when printed on paper or screen it represents a 3 column, 4 row accurate calendar of any year you input. After I developed this in 1990 in c, I adapted it to JAVA in 1999. Now I am trying to do it with C++. My problem is I cannot even get it compiled so I can run and troubleshoot my algorithm.
I'm not using classes for what C++ is known for, but if you can help put my functions into classes
please show me.
Below is my source code, I'm having problems with function calls mainly. If I can get some assistance with getting this off the ground and get my algorithm running in C++, I'd like to make it into a simple windows application, I have never done any windows GUI programming. If I can get a running program with screen output, I would be able to troubleshoot the flow. This is a cool little application, anyone like to work with me on this?
About me, Kerry Newman, electrical engineering UC Berkeley 1984, Living in Houston Texas.
Here is my JAVA version on Java Boutique: http://javaboutique.internet.com/KNCalendar/
My JAVA source code: http://javaboutique.internet.com/KNCalendar/source.html
I'm using VC++ 5.1.
Thanks
Kerry
Compile Errors:
--------------------Configuration: calendar - Win32 Debug--------------------
Compiling...
calendar.cpp
C:\Program Files\DevStudio\MyProjects\calendar\calendar.cpp(41) : error C2065: 'firstweek' : undeclared identifier
C:\Program Files\DevStudio\MyProjects\calendar\calendar.cpp(43) : error C2065: 'otherweek' : undeclared identifier
C:\Program Files\DevStudio\MyProjects\calendar\calendar.cpp(60) : error C2371: 'firstweek' : redefinition; different basic types
C:\Program Files\DevStudio\MyProjects\calendar\calendar.cpp(61) : error C2064: term does not evaluate to a function
C:\Program Files\DevStudio\MyProjects\calendar\calendar.cpp(63) : error C2065: 'strcat' : undeclared identifier
C:\Program Files\DevStudio\MyProjects\calendar\calendar.cpp(82) : error C2040: 'otherweek' : 'int (int)' differs in levels of indirection from 'int'
C:\Program Files\DevStudio\MyProjects\calendar\calendar.cpp(120) : error C2040: 'firstday' : 'int (int,int)' differs in levels of indirection from 'int'
Error executing cl.exe.
calendar.exe - 7 error(s), 0 warning(s)
/////////////////////////////////////////////////////////////////
////////////////// Main program flow ////////////////////////////
/////////////////////////////////////////////////////////////////
#include <iostream.h>
#include <stdlib.h>
#include <stdio.h>
int row, col, year, month, week, i, dayofweek, firstday;
int day[3];
char line[80];
int main ( )
{
cout << "Please Enter the Year)";
year: cin >> year;
for ( row= 0 ; row <= 3 ; ++row ) // 4 rows of Months 0-1-2 3-4-5 6-7-8 9-10-11 //
{
if ( row != 0 )
switch (row) // Draw Month names //
{
case 0: cout << " ***** JANUARY ****** ***** FEBRUARY ***** ******* MARCH ******"; break;
case 1: cout << " ****** APRIL ******* ******* MAY ******** ******* JUNE *******"; break;
case 2: cout << " ******* JULY ******* ****** AUGUST ****** ***** SEPTEMBER ****"; break;
case 3: cout << " ***** OCTOBER ****** ***** NOVEMBER ***** ***** DECEMBER *****";
}
cout << " Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa";
for ( int week = 0 ; week <= 5 ; ++week ) //week loops. one line includes one week for 3 Months. //
{
cout << " ";
for ( int col = 0 ; col <= 2 ; ++col ) //3 Months in a row //
{
if (week == 0) // 1st week needs to know what day to start. //
firstday = firstweek( row*3 + col, year );
else
otherweek( row*3 + col );
} // Column end //
cout << itoa ( dayofweek, line, 10 ); // done with line, print it out.
cout << " ";
}
}
return 0;
}
/////////////////////////////////////////////////////////////////
/////////////////// first week of the Month /////////////////////
/////////////////////////////////////////////////////////////////
char firstweek( int Month, int year )
{
dayofweek = firstday ( Month, year ); //get starting day of week.
for ( int i = 0 ; i < dayofweek ; i++ ) //print blanks until 1st.
strcat(line, " ");
do
{
strcat( line, " " ); //( day[Month] ) ); //print day of Month with leading blanks.
++day[Month];
++dayofweek;
}
while (dayofweek <= 6 );
if ( ((Month + 1) % 3) != 0 ) // if last day of week, print spaces between Month. //
cout << " ";
}
/////////////////////////////////////////////////////
/////////////////// weeks 2 - 6 /////////////////////
/////////////////////////////////////////////////////
otherweek( int Month )
{
dayofweek = 0; // initialize day of week to 0. Sunday //
int daysinMonth[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if (((year % 4) == 0) && (((year % 100) != 0) || ((year % 400) == 0)))
daysinMonth[1] = 29;
do
{
if ( day[Month] <= daysinMonth[ Month ] ) // check to see if days still left. //
{
if ( day[Month] < 10 ) // if 1-9, 2 leading spaces needed. //
strcat( line, " ", itoa( day[Month],line, 10 ) );
else // if 10-31, 1 leading space needed. //
strcat( line, " ", itoa( day[Month],line, 10 ) );
++day[Month];
++dayofweek;
}
else
{
strcat( line, " "); // if after last day of Month, print blanks. //
++dayofweek;
}
}
while ( dayofweek <= 6 );
if ( ((Month + 1) % 3) != 0 ) //if last day of week, print spaces between Month. //
strcat( line, " ");
return line;
} //end of otherweek
/////////////////////////////////////////////////////
///////////////// first day of Month ////////////////
/////////////////////////////////////////////////////
firstday( int Month, int year )
{
int yeardiff, days_since, dayofyear[]={ 0,31,59,90,120,151,181,212,243,273,304,334 };
yeardiff = year - 1;
days_since = yeardiff * 365;
days_since += ( ( yeardiff/4 ) - ( yeardiff/100 ) + ((yeardiff)/400) ) + dayofyear[ Month ];
if (((((year % 4) == 0) && (((year % 100) != 0) || ((year % 400) == 0)))) && ( Month > 1 ))
days_since++;
return ( ( days_since + 1 ) % 7 );
}
//////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////
//
// Calendar Generator. Original program in C, 05/01/90
// v1.0 Converted to Java code. 09/23/99
// Modified from spaghetti code to structured code
// v1.1 Added button to inc/dec years. 09/27/99
// v1.2 Made Month and day names bold. 09/29/99
// v1.3 Added 'about' and 'now, buttons. 10/02/99
// v1.4 Added Font and Background color buttons. 10/03/99
// v1.5 Added frames (lines) around Months. 10/08/99
// v2.0 Pop-up window for individual Months 10/10/99 - 10/12/99
// v2.01 Some mouse event changes. Update month in
// window each time a month is clicked. 10/13/99
// v2.02 Adjusted lines on month window. 10/14/99
// v2.03 Month windows no longer show empty weeks 10/20/99
// v2.1 Used date class to set year from clock 10/22/99
// v2.2 Made separate applet for printing 05/14/00
// v2.21 Removed monthframe for now 05/16/00
// v2.3 Made Background & Font Colors user selectable 12/31/00
// v3.0 Adapting it to make it easy for laser 10/05/10
// Downloaded VC++ to work on program. 10/08/10
// Started serious programming C++ 10/13/10
//
//////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////
//
// This program works by building text lines and printing them out.
//
// for row = 0 to 3 - 4 rows by 3 columns of months
// for week = 0 to 5 - each week is a line and has to include the same week for all 3 months
// for column = 0 to 2 - each line includes 3 months
//
// This is the basic structure of the program. For the 1st week the
// start of the week has to be found and blanks inserted before the 1st.
// For the remaining weeks, the last day of the month needs to be found
// and blanks are inserted prior to the next month. After the 6 weeks are
// built for 3 months (1 row) the next row is then worked on the same way.
//
//////////////////////////////////////////////////////////////////
// Kerry Newman - [ 1999/2000
// http://web.wt.net/~newman/
2010
Edit by admin: no contact info permitted on the forum, thank you
-
October 16th, 2010, 08:11 PM
#2
Re: Old algorithm, new language, trouble starting. 1 page yearly calendar program.
Your immediate problems are you're trying to use functions in main that the compiler hasn't seen yet. Try moving firstweek, etc. before main.
-
October 17th, 2010, 05:14 AM
#3
Re: Old algorithm, new language, trouble starting. 1 page yearly calendar program.
Please use code tags when posting code.
In addition to GCDEF's solution, you can also declare your functions before main and define them after main, as such
Code:
#include <iostream>
void foo(); // declaration
int main()
{
foo();
}
void foo() // definition
{
std::cout << "foo";
}
Note that:
- you should include iostream, not iostream.h
- cout is defined in the namespace std
- functions must have a return type specified. If a function returns nothing, use void.
Which book are you using? These things should be explained in one of the first chapters.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
-
October 17th, 2010, 03:02 PM
#4
Re: Old algorithm, new language, trouble starting. 1 page yearly calendar program.
Thanks for the tips. I declared the functions before main and it stopped the initial c2065 error. I only had my JAVA source code on file and I cut and pasted it to a .cpp and started editing it. It has been nearly 10 years since I've done any programming. I've always loved programming, one day while working at the Boeing 747 plant in Everett WA I had this ideal. I had found this routine (source code in c) which calculated which day of the week the first day of the month fell on: Sun (0) - Sat(6) for any given month, year. I needed to improve my c skills so I thought of a plan to make a yearly calendar by building each line one at a time by pieces and then printing them out.
In 1999 I started learning JAVA and needed an application to do, I remembered this and I applied it to run in JAVA. I also in 1999 did a Visual Basic one too. Now I want to take this same ideal and redo it again. I choose C++ for several reasons, popularity, I have VC++ 5 and it can easily be adapted to a windows application.
The reason I am doing it again is because I have a laser engraver that I'd like to engrave calendars on. I have been able to cut -n- paste these onto my graphics program but I want to get rid of the boxes around the months to make the engraving quicker.
I purchased "C++ A Beginner's Guide" Herbert Schildt 2004.
Last edited by knewman60; October 17th, 2010 at 03:53 PM.
Reason: more info
-
October 17th, 2010, 03:54 PM
#5
Re: Old algorithm, new language, trouble starting. 1 page yearly calendar program.
Now I got it to this point:
--------------------Configuration: calendar - Win32 Debug--------------------
Linking...
LIBCD.lib(wincrt0.obj) : error LNK2001: unresolved external symbol _WinMain@16
Debug/calendar.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.
calendar.exe - 2 error(s), 0 warning(s)
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|