GIDForums  

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

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 10-Mar-2006, 23:50
earachefl earachefl is offline
Member
 
Join Date: Feb 2006
Posts: 178
earachefl is on a distinguished road

Anyone want to critique this?


This C++ stuff is fun! Who knew?

I've taken an assignment from "Problem Solving, Abstraction, and Design" (and slightly modified it) and was wondering if any of you geniuses had any comments on its design, style, anything. WaltP, thanks for the use of your leap year function - it was better than mine. The original assignment asked for a "function to calculate the day number for a given day represented by three type int values", which I thought strange - if the three values are month, day, and year, are they asking for the cumulative number of days from year 0?? And it also asked for a "recursive function to determine the number of days between two dates, each represented by three integers." -since the first part of the assignment had asked for a function to compute the day number, that seemed like a waste, so I wrote a function that compared the dates using two values, the day number and year. Didn't do it recursively however - so I'd probably get an F.

Here 'tis:
CPP / C++ / C Code:
//A program to compare two dates and compute the number of days between them
#include <iostream>
using namespace std;

//Function prototypes
bool IsLeapYear (int);
int DayNumber(int, int, bool);
int diff(int, int, int, int, bool);

//Constants used by function DayNumber
const int JAN = 0;
const int FEB = 31;
const int MAR = 59;
const int APR = 90;
const int MAY = 120;
const int JUN = 151;
const int JUL = 181;
const int AUG = 212;
const int SEP = 243;
const int OCT = 273;
const int NOV = 304;
const int DEC = 334;

int main () 
{
	int month1, month2;			//Input
	int date1, date2;			//Input
	int year1, year2;			//Input
	char slash;					//Input
	bool isLeap1, isLeap2;		//Variable - true if leap year
	isLeap1 = false;			//Initialise bool variables
	isLeap2 = false;			//Do I need to do this?
	int number1, number2;		//Variable - numbered date (1 - 365) (or 1 - 366 in leap year)
	int result;					//Variable - difference between two dates in days
	
	//Get input
	cout << "Please enter two dates in numeric form (i.e. 1/1/2001). Please enter first date: ";
	cin >> month1 >> slash >> date1 >> slash >> year1;
	cout << "Enter the second date: ";
	cin >> month2 >> slash >> date2 >> slash >> year2;
	   
	//Find out if year 1 is a leap year
	isLeap1 = IsLeapYear (year1);
	
	//Compute numbered date for year 1
	number1 = DayNumber (month1, date1, isLeap1);
	
	//Find out if year 2 is a leap year
	isLeap2 = IsLeapYear (year2);
	
	//Compute numbered date for year 2
	number2 = DayNumber (month2, date2, isLeap2);
	
	//Compute difference in days between the two dates
	result = diff(number1, number2, year1, year2, isLeap1);
	
	//Output the result
	cout << "The difference between " << month1 << slash << date1 << slash << year1
		<< " and " << month2 << slash << date2 << slash << year2 << " is " << result << " days." << endl;

    return 0;
}

//A function to determine if a given year is a leap year
bool IsLeapYear (int year)	//WaltP, I used your version as it was better than mine!
{	
    bool rtn;

    rtn = false;            // Assume this is not a leapyear

	
    if ((year % 400) == 0)  // if true it is a leap year 
    {                       // every 400 years....
        rtn = true;
    }
    else                    // otherwise
    if ((year % 100) == 0)  // if true it's not a leap year
    {                       // every 100 years, but not 400
        rtn = false;        // not needed but it's better to have a statement
    }
    else                    // otherwise
    if ((year % 4) == 0)    // if true it is a leap year 
    {                       // every 4 years, but not every 100 years
        rtn = true;
    }
	return rtn;             // Always return a value
}

//A function to compute the numbered date from 1 - 365 (1 - 366 if leap year)
int DayNumber (int month, int date, bool isLeap)	//Should I declare these parameters as constants?
{
	int number;				//Local variable - result of computation
	number = date;			//Puts value of date in number
	
	switch (month)			//Depending on what month is input
	{
		case 1:				//If January
			number += JAN;	//Add offset determined by constants declared prior to main
		break;
		case 2:				//If February
			number += FEB;	//Add offset
		break;
		case 3:				//etc.
			number += MAR;
		break;
		case 4:
			number += APR;
		break;
		case 5:
			number += MAY;
		break;
		case 6:
			number += JUN;
		break;
		case 7:
			number += JUL;
		break;
		case 8:
			number += AUG;
		break;
		case 9:
			number += SEP;
		break;
		case 10:
			number += OCT;
		break;
		case 11:
			number += NOV;
		break;
		case 12:
			number += DEC;
		break;
		default:							//If none of the above
			cout << "Error!" << endl;
	}										//End of switch(month)
	if ((isLeap == true) && (month > 2))	//If both a leap year and later than February
	{
		number++;							//Add one day
	}   	
	return number;							//Returns the computed date number
}											//End DayNumber function

//A function to compute the difference in days between two dates
int diff(int number1, int number2, int year1, int year2, bool isLeap1)
{
	int result;								//Local variable - result of computation
	if (year1 == year2)						//If years are the same
	{
		if(number1 > number2)				//Determine if the computed dates are out of order
		{									//Reverse dates
			int temp;
			temp = number1;
			number1 = number2;
			number2 = temp;
		}									//Dates are now in proper order
		result = number2 - number1;			//Compute the result as difference
	}
	else									//Otherwise (if years are different)
	{
		if(year1 > year2)					//Determine if years are in reverse order
		{
			int temp;						//If so, first reverse years
			temp = year1;
			year1 = year2;
			year2 = temp;
			temp = number1;					//then also reverse computed dates to correspond with years
			number1 = number2;
			number2 = temp;
		}									//Years and dates are now in proper order
		result = 365 - number1;				//Result gets 365 minus the lowest date
		
		isLeap1 = IsLeapYear(year1);		//Calls function to determine if the lowest year is a leap year
		if (isLeap1 == true)				//If the lowest year is a leap year
		{
			result++;						//Add one day to the result
		}
		year1++;							//Increment lowest year

		while (year1 < year2)				//Loop repetition condition - lowest year must be less than highest year
		{									//While true
			result += 365;					//Add 365 days to result
			isLeap1 = IsLeapYear(year1);	//Determine if current year is leap year
				if (isLeap1 == true)		//If true
				{
					result++;				//Add one day to result
				}	
			year1++;						//Increment current year
		}									//End while loop
		result += number2;					//After while loop is finished, add number of days in year two to result
	}
	return result;							//Return result
}
	
  #2  
Old 11-Mar-2006, 08:51
balusss balusss is offline
Junior Member
 
Join Date: Dec 2005
Posts: 64
balusss is on a distinguished road

Re: Anyone want to critique this?


hi chef,
i am also a novice in CPP and i find your style & indenting very good. i have a small doubt.
in your diff(...) , when (year1==year2), can't we simply return the value of abs(number1-number2)?
and the rest prog is no doubt very good, at least for me.
__________________
balu------>>>>>U
perseverance pays slowly.
  #3  
Old 11-Mar-2006, 11:37
davis
 
Posts: n/a

Re: Anyone want to critique this?


Quote:
Originally Posted by earachefl
This C++ stuff is fun! Who knew?

I've taken an assignment from "Problem Solving, Abstraction, and Design" (and slightly modified it) and was wondering if any of you geniuses had any comments on its design, style, anything. WaltP, thanks for the use of your leap year function - it was better than mine. The original assignment asked for a "function to calculate the day number for a given day represented by three type int values", which I thought strange - if the three values are month, day, and year, are they asking for the cumulative number of days from year 0?? And it also asked for a "recursive function to determine the number of days between two dates, each represented by three integers." -since the first part of the assignment had asked for a function to compute the day number, that seemed like a waste, so I wrote a function that compared the dates using two values, the day number and year. Didn't do it recursively however - so I'd probably get an F.

Here 'tis:
CPP / C++ / C Code:
//A program to compare two dates and compute the number of days between them
#include <iostream>
using namespace std;

//Function prototypes
bool IsLeapYear (int);
int DayNumber(int, int, bool);
int diff(int, int, int, int, bool);

//Constants used by function DayNumber
const int JAN = 0;
const int FEB = 31;
const int MAR = 59;
const int APR = 90;
const int MAY = 120;
const int JUN = 151;
const int JUL = 181;
const int AUG = 212;
const int SEP = 243;
const int OCT = 273;
const int NOV = 304;
const int DEC = 334;

int main () 
{
	int month1, month2;			//Input
	int date1, date2;			//Input
	int year1, year2;			//Input
	char slash;					//Input
	bool isLeap1, isLeap2;		//Variable - true if leap year
	isLeap1 = false;			//Initialise bool variables
	isLeap2 = false;			//Do I need to do this?
	int number1, number2;		//Variable - numbered date (1 - 365) (or 1 - 366 in leap year)
	int result;					//Variable - difference between two dates in days
	
	//Get input
	cout << "Please enter two dates in numeric form (i.e. 1/1/2001). Please enter first date: ";
	cin >> month1 >> slash >> date1 >> slash >> year1;
	cout << "Enter the second date: ";
	cin >> month2 >> slash >> date2 >> slash >> year2;
	   
	//Find out if year 1 is a leap year
	isLeap1 = IsLeapYear (year1);
	
	//Compute numbered date for year 1
	number1 = DayNumber (month1, date1, isLeap1);
	
	//Find out if year 2 is a leap year
	isLeap2 = IsLeapYear (year2);
	
	//Compute numbered date for year 2
	number2 = DayNumber (month2, date2, isLeap2);
	
	//Compute difference in days between the two dates
	result = diff(number1, number2, year1, year2, isLeap1);
	
	//Output the result
	cout << "The difference between " << month1 << slash << date1 << slash << year1
		<< " and " << month2 << slash << date2 << slash << year2 << " is " << result << " days." << endl;

    return 0;
}

//A function to determine if a given year is a leap year
bool IsLeapYear (int year)	//WaltP, I used your version as it was better than mine!
{	
    bool rtn;

    rtn = false;            // Assume this is not a leapyear

	
    if ((year % 400) == 0)  // if true it is a leap year 
    {                       // every 400 years....
        rtn = true;
    }
    else                    // otherwise
    if ((year % 100) == 0)  // if true it's not a leap year
    {                       // every 100 years, but not 400
        rtn = false;        // not needed but it's better to have a statement
    }
    else                    // otherwise
    if ((year % 4) == 0)    // if true it is a leap year 
    {                       // every 4 years, but not every 100 years
        rtn = true;
    }
	return rtn;             // Always return a value
}

//A function to compute the numbered date from 1 - 365 (1 - 366 if leap year)
int DayNumber (int month, int date, bool isLeap)	//Should I declare these parameters as constants?
{
	int number;				//Local variable - result of computation
	number = date;			//Puts value of date in number
	
	switch (month)			//Depending on what month is input
	{
		case 1:				//If January
			number += JAN;	//Add offset determined by constants declared prior to main
		break;
		case 2:				//If February
			number += FEB;	//Add offset
		break;
		case 3:				//etc.
			number += MAR;
		break;
		case 4:
			number += APR;
		break;
		case 5:
			number += MAY;
		break;
		case 6:
			number += JUN;
		break;
		case 7:
			number += JUL;
		break;
		case 8:
			number += AUG;
		break;
		case 9:
			number += SEP;
		break;
		case 10:
			number += OCT;
		break;
		case 11:
			number += NOV;
		break;
		case 12:
			number += DEC;
		break;
		default:							//If none of the above
			cout << "Error!" << endl;
	}										//End of switch(month)
	if ((isLeap == true) && (month > 2))	//If both a leap year and later than February
	{
		number++;							//Add one day
	}   	
	return number;							//Returns the computed date number
}											//End DayNumber function

//A function to compute the difference in days between two dates
int diff(int number1, int number2, int year1, int year2, bool isLeap1)
{
	int result;								//Local variable - result of computation
	if (year1 == year2)						//If years are the same
	{
		if(number1 > number2)				//Determine if the computed dates are out of order
		{									//Reverse dates
			int temp;
			temp = number1;
			number1 = number2;
			number2 = temp;
		}									//Dates are now in proper order
		result = number2 - number1;			//Compute the result as difference
	}
	else									//Otherwise (if years are different)
	{
		if(year1 > year2)					//Determine if years are in reverse order
		{
			int temp;						//If so, first reverse years
			temp = year1;
			year1 = year2;
			year2 = temp;
			temp = number1;					//then also reverse computed dates to correspond with years
			number1 = number2;
			number2 = temp;
		}									//Years and dates are now in proper order
		result = 365 - number1;				//Result gets 365 minus the lowest date
		
		isLeap1 = IsLeapYear(year1);		//Calls function to determine if the lowest year is a leap year
		if (isLeap1 == true)				//If the lowest year is a leap year
		{
			result++;						//Add one day to the result
		}
		year1++;							//Increment lowest year

		while (year1 < year2)				//Loop repetition condition - lowest year must be less than highest year
		{									//While true
			result += 365;					//Add 365 days to result
			isLeap1 = IsLeapYear(year1);	//Determine if current year is leap year
				if (isLeap1 == true)		//If true
				{
					result++;				//Add one day to result
				}	
			year1++;						//Increment current year
		}									//End while loop
		result += number2;					//After while loop is finished, add number of days in year two to result
	}
	return result;							//Return result
}
	

A couple of points of order...

In:

Please enter two dates in numeric form (i.e. 1/1/2001). Please enter first date:

...I'd probably put a newline character after the period and before the second Please.

Also, I'd use a different convention than 1/1/2001, since it is unclear which "1" is month and which "1" is day of the month.

Code:
Please enter two dates in numeric form (i.e. 1/1/2001). Please enter first date: 14/1/2006 Enter the second date: 15/1/2006 Error! Error! The difference between 14/1/2006 and 15/1/2006 is 0 days.

...your error messages are typical "meaningless" error messages in that they don't tell the user anything meaningful in order to correct their input.

Something like: DD/MM/YYYY might be worth considering...then followed with an "example" of 1/1/2001 it will be clear how you expect input. You can also use something like: 12/31/2001, because we all know that there are not 31 months in a year.

Also, your program doesn't work "completely" properly:

CPP / C++ / C Code:
Please enter two dates in numeric form (i.e. 1/1/2001). Please enter first date: 1/1/1752
Enter the second date: 12/31/1752
The difference between 1/1/1752 and 12/31/1752 is 365 days.

Here is the calendar for the year 1752:

Code:
1752 January February March Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa 1 2 3 4 1 1 2 3 4 5 6 7 5 6 7 8 9 10 11 2 3 4 5 6 7 8 8 9 10 11 12 13 14 12 13 14 15 16 17 18 9 10 11 12 13 14 15 15 16 17 18 19 20 21 19 20 21 22 23 24 25 16 17 18 19 20 21 22 22 23 24 25 26 27 28 26 27 28 29 30 31 23 24 25 26 27 28 29 29 30 31 April May June Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa 1 2 3 4 1 2 1 2 3 4 5 6 5 6 7 8 9 10 11 3 4 5 6 7 8 9 7 8 9 10 11 12 13 12 13 14 15 16 17 18 10 11 12 13 14 15 16 14 15 16 17 18 19 20 19 20 21 22 23 24 25 17 18 19 20 21 22 23 21 22 23 24 25 26 27 26 27 28 29 30 24 25 26 27 28 29 30 28 29 30 31 July August September Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa 1 2 3 4 1 1 2 14 15 16 5 6 7 8 9 10 11 2 3 4 5 6 7 8 17 18 19 20 21 22 23 12 13 14 15 16 17 18 9 10 11 12 13 14 15 24 25 26 27 28 29 30 19 20 21 22 23 24 25 16 17 18 19 20 21 22 26 27 28 29 30 31 23 24 25 26 27 28 29 30 31 October November December Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 1 2 3 4 1 2 8 9 10 11 12 13 14 5 6 7 8 9 10 11 3 4 5 6 7 8 9 15 16 17 18 19 20 21 12 13 14 15 16 17 18 10 11 12 13 14 15 16 22 23 24 25 26 27 28 19 20 21 22 23 24 25 17 18 19 20 21 22 23 29 30 31 26 27 28 29 30 24 25 26 27 28 29 30 31

Another issue is that it is not clear what "between" means. Your output is using "between" to mean the beginning of the day following the first date to the end of the day of the second date. If I said "How many (whole) numbers are between 1 and 3," any 5-year old would be able to tell me that there is only a single number "2" between them. However, in your code, if I say how many days are between 1/1/1 and 1/2/1 it tells me one day. How many whole numbers are between 1 and 2? None, they are adjacent. Basically, you have a start of day/end of day issue that should be clarified in your requirements elucidation phase in order to code a proper response to first = 1/1/1 and second = 1/2/1.

A natural extension of any such "real world" program would be how many hours are between a certain date/time stamp and another date/time stamp. For example, if someone where to ask you "How many days are in the year, 2006?" You would easily say: 365. But your program says 364 when using first = 1/1/2006 and second = 12/31/2006. If someone asked you how many days are in a leap year, you would easily be able to say 366. Your program gives us 365. Do you see how important it is to clearly define what "between" means? Why isn't "between" the period beginning exactly at the start of the first date entered and ending at the end of the second date entered? In that case, if one entered first = 1/1/1 and second = 1/2/1 the answer would be 2 days and definitely not zero.

If we think about what a "day" is, it is an interval of time, not so much a number on a calendar. Anyone older than a 5 year old will know that the day begins when the previous day ends. It is the whole "is the time 12:00AM or 00:00" issue. In 24-hour clocks, we have 23:59.59.9999 as the last "reasonable" moment before the new day begins...even if a particular clock isn't that accurate. When someone asks how many days are between Sunday and Saturday, what do we say? We look at the calendar and we see Monday, Tuesday, Wednesday, Thursday and Friday...and we say 5 days are between Sunday and Saturday. Of course, your program says 6. This is one of those situations where "human" logic and "computer/math" logic don't really add up very well...and certainly one where the "division" of where "between" starts and ends must be clearly understood.

One could easily imagine a little web-based utility to tell you how many days your vacation would be by entering the date that your vacation started and the day that it ended. If you were to fly out on a Friday evening after work, get to the resort that night and wake up "in vacation mode" the next morning...(assuming that you didn't party too seriously Friday night!)...and relaxed, ate out and had fun until you had to fly back the next Sunday so that you could be to work early Monday morning, how long was your vacation and how many days did you have to "take off" from work? Use your program to figure it out as if it were a real utility and see if the output "makes sense." If it doesn't add up to what you would think a typical user would expect, then maybe you need more requirements clarification.


:davis:
  #4  
Old 11-Mar-2006, 12:27
earachefl earachefl is offline
Member
 
Join Date: Feb 2006
Posts: 178
earachefl is on a distinguished road

Re: Anyone want to critique this?


baluss, thanks, you're absolutely right.

davis, many thanks for the points you elucidated. In regards to
Quote:
Another issue is that it is not clear what "between" means. Your output is using "between" to mean the beginning of the day following the first date to the end of the day of the second date.
You are right, it isn't clear. I plead an extenuating circumstance in that the assignment is misleading as well. From the assignment:
Quote:
Write a recursive function that determines the number of days between two dates....If the dates are in the correct order and the year is the same, the result is just the difference in the day numbers for each date.
So I believe I interpreted that part of the assignment correctly. However, you're right, the terminology isn't accurate. I should have said "The difference between the two dates is" or "The number of days until the second date is" - but the assignment threw me off.
This book has a number of assignments that have the "huh?" factor.....
So what do you think is the best book to learn from?
  #5  
Old 11-Mar-2006, 21:20
davis
 
Posts: n/a

Re: Anyone want to critique this?


Quote:
Originally Posted by earachefl
So what do you think is the best book to learn from?

Let's take a look at what you're doing. Basically, you're learning C right now. You're learning how to perform the fundamental "mechanical" operations necessary in any C or C++ program. You really haven't used C++ in any particular way exclusive of iostreams, which, BTW...are reasonably complex and involved aspects of the language all by themselves.

There is nothing wrong with learning these elements of the language, but, to be clear, there is nothing at all "C++"-like about it.

In order to even begin to be C++-ish, one would need to create a Date class, implement a "-" operator (along with +, =, ==, !=, >>, <<, >, <, ++ (pre and post) -- (pre and post) etc.).

This is the fundamental difference between C and C++. In other words, instead of doing some kind of "mechanical wizardry" inside of a "function" to calculate the "difference" between one date and another, you would simply implement the "-" operator.

For example, a date class would be able to return the month, day and year of any date and probably operations like IsLeapYear() would return a bool.

Of course, any reasonable implmentation of a "Date" class would really be a "DateTime" class, since the calculations can not be accurate without knowing when (by year, month, day, hour, minute, second, millisecond, microsecond) the particular "date" is.

CPP / C++ / C Code:
#include <iostream>
#include <stdexcept>
#include <ctime>

using namespace std;

class Time
{
public:
protected:
private:

};
class Date
{
public:
    Date()  // initializes to "now"
    {
        time_t now;
        time( &now );
    }
    Date( int month, int day, int year, bool AD=true ) :
    m_month_component( month ),
    m_day_component( day ),
    m_year_component( year ),
    m_bIsAD( AD )
    {
        if( year <= 0 )
        {
            throw new out_of_range( "Illegal Year Value" );
        }
    }
    Date( const Date& rhs )
    {
        m_month_component = rhs.m_month_component;
        m_day_component   = rhs.m_day_component;
        m_year_component  = rhs.m_year_component;
        m_bIsAD           = rhs.m_bIsAD;
    }
    // put dtor here...

protected:
private:  // private because we should probably not be derived...
    int m_month_component;
    int m_day_component;
    int m_year_component;
    bool m_bIsAD;
    Time m_time;
};

...this is something along the lines of a (very) rudimentary start on an implementation of a "datetime" class(es). Once completed, it would be C++. The idea behind C++ is that there are elements of reusable code. If we stop for a moment to look at your code, there isn't really any aspect of it that is reusable. You may be able to cut-n-paste portions of it into other programs, but basically, it is a wind-up doll....turn the knob, release and it does its thing and finally dies.

However, with a "proper" date/time class (perhaps as might be implemented in a library), anyone who needed date/time functionality could easily reuse the code for it. That is one of the many reasons why we have C++.

Now, there is no reason at all why a good date/time infrastructure can't be implemented in C, in-fact, there already is...

Furthermore, when we start to look at dates and times, we encounter a REALLY LOT of issues...such as whether someone is "right now" on the other side of the International Dateline, or not. If they are, it is "tomorrow" for them. How screwy is that when you're trying to write code?!

Additionally, one needs a facility to convert between timezones and daylight savings time for those regions that support/use it.

We really need to "think" in a different manner when it comes to C++. If not, we are not really doing anything close to C++...rather, it is just C with some C++ capability mixed in.

As for good books, I recommend Jesse Liberty's books for learning C++. His content will get you well down the road to learning the needed C and eventually C++ aspects. Once you get the basic ideas of C++ understood, then you can try to start thinking more specifically OOP-mindedly.


:davis:
  #6  
Old 12-Mar-2006, 19:30
QED's Avatar
QED QED is offline
Member
 
Join Date: Feb 2005
Location: Hudson Valley, NY
Posts: 231
QED is a jewel in the roughQED is a jewel in the roughQED is a jewel in the rough

Re: Anyone want to critique this?


I'll just add two very small suggestions.

Firstly, since you're already using C++ headers, cout, std namespace... there is a Standard C++ swap() function. You could simplify your code a littel by using that.

Secondly (and this is a personal preference really), I would typically use the preincrement operator ++result; instead of postincrement whenever I have a stand alone increment statement. With primitives it really doesn't matter, but I feel it's a good habit to get into, since it's more efficient when working with more complex objects.

Matthew

P.S. I fogot to say that the std::swap() function is defined in the header <algorithm>.
  #7  
Old 12-Mar-2006, 21:47
earachefl earachefl is offline
Member
 
Join Date: Feb 2006
Posts: 178
earachefl is on a distinguished road

Re: Anyone want to critique this?


QED, thanks, I figured there was a standard swap function somewhere, haven't gotten to it yet in my book. I didn't know that the pre-increment operator was preferable to post-increment, where possible - my book didn't state there was a preference.

davis, thanks for your comments. I should have started my post with "who knew this programming stuff was so much fun?" instead of specifically mentioning C++. I guess it's frustrating for you to have newbies like me posting our pitiful code. Just realize I'm posting out of enthusiasm, for feeling like I'm getting at least some aspects of the process. OK?
  #8  
Old 13-Mar-2006, 07:46
davis
 
Posts: n/a

Re: Anyone want to critique this?


Quote:
Originally Posted by earachefl
davis, thanks for your comments. I should have started my post with "who knew this programming stuff was so much fun?" instead of specifically mentioning C++. I guess it's frustrating for you to have newbies like me posting our pitiful code. Just realize I'm posting out of enthusiasm, for feeling like I'm getting at least some aspects of the process. OK?

Of all the newbies I've seen here, your posts are probably the least frustrating of all. I'm glad that you're getting some of the fundamentals down. There is nothing wrong with your elation or your enthusiasm. I sometimes come across as being "too heavy" (to reverse-quote our recently gone-missing WaltP), but all I wanted to do was to explain to you the differences so that you understand that there is another "whole world" of "real" C++ just beyond the "C steps" you're now taking. It is far more exciting and interesting than just the C aspects you're now learning. It is a long journey, to be sure.

Code:
Please enter two dates in numeric form (i.e. 1/1/2001). Please enter first date: 1/1/0 Enter the second date: 1/1/1 The difference between 1/1/0 and 1/1/1 is 366 days.

This is the "problem" that I tried to explain previously about the leap year routine. How can it accept a date of 0? In "C" we often return some kind of "error code," in C++ we are obligated through a convention called "contract programming" to return the value type or throw an error. You will eventually get to the location in your book where exceptions are discussed. However, briefly, they are a mechanism that allows for propagation of an error condition to be delegated to those with an interest in managing particular errors. If no code directly manages the error, the error is propagated to a "top-level" error manager that deals with it...which usually means that the program is aborted. A rather abrupt end of things, but it definitely would have prevented the results returned above that are "impossible" based on the data entered. Since year 0 can't exist, it can not also be a leap year.

I don't say any of this to discourage you, in fact, I say it to encourage you to adopt error management into your code at your earliest convenience. We call this "defensive programming." Program as if the data entered is going to be bad first and good last...or maybe never

The fact that you actively seek critics of your code is excellent! You have demonstrated a level of openness about your inexperience that few others would share. You are a far better programmer because of that simple fact. Perhaps a first step in learning is coming to an understanding that you don't already know everything about something. No matter how good you get at programming, never lose sight of that openness and an understanding that there is far more out there to learn that to ever even be able to become a "know-it-all."

Anyone who has ever seen a movie has been a movie critic. It is easy to be a critic. How hard is it to make a movie? ...even a bad one? Probably a lot harder than writing code...even code that "makes movies."

You _are_ getting some aspects of the process...and good for you! It isn't easy, is it? I've been programming for many years and I still make programming mistakes at the rate of dozens (if not a lot more) per day. You seem like you're in a perfect position to really learn well through your efforts. Don't let something that I say distract you from the opportunity you have to learn this stuff. Rather, just pile it on or send it to /dev/null (the waste basket) as you see fit. Sometimes something said won't make a lot of sense in the "now" sense of it. Perhaps later it will.

And, just so that there is something else for motivation, the elation one gets from "this programming stuff" is nothing compared to the "oh wow" that happens when the "lights come on" in understanding object-oriented programming. I don't say this to diminish your current level of enthusiasm, rather to let you know that there is something up ahead even cooler just waiting for you to discover it.


:davis:
 
 

Recent GIDBlogWriting a book 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
Please critique my new web site dhester Websites Reviewed Forum 3 15-Apr-2003 06:15

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

All times are GMT -6. The time now is 09:58.


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