Thread: Calendar ALMOST DONE!!!

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    14

    Exclamation Calendar ALMOST DONE!!!

    Howdie all.

    Up front I'd like to thank your for taking the time to read this.
    Here is my problem , I wrote a program that takes the date entered as MMDDYYYY, and it converts it to for example

    01012000 to January 1, 2000..

    Here is my problem when I type in the about input 01012000 I get January 20 2000...I know its a math error, but for the life of me I cannot find it...any help would be appreciated.

    Oh and there is one more thing , If at all you could give suggestions on how to shorten the code, i would be very grateful, thanks again. Heres the code

    Code:
    #include <iostream>
    #include <iomanip>
    #include <string>
    using namespace std;
    
    void input();
    bool checkdigit();
    bool checklength();
    bool checkday();
    bool checkmonth();
    int contoint();
    int contointd();
    int contointy();
    void printdate();
    
    int n,a,b,c;
    string date, stop, day, year, month;
    bool ok;
    
    int main()
    {
    	input();
    
    	while (date != "stop")
    	{
    		ok = checklength();
    		if (ok == true)
    			ok = checkdigit();
    		if (ok == true)
    		{
    			month = date.substr (0,2);
    			day = date.substr (2,2);
    			year = date.substr (4,4);
    			a = contoint();
    			b = contointd();
    			c = contointy();
    			ok = checkmonth();
    			if (ok = true)
    			{
    				ok = checkday();
    				if (ok == true)
    					printdate();
    			}
    		}
    
    		input();
    	}
    	return 0;
    }
    
    void input()
    {
    	cout << endl<< "Enter Stop to quit" << endl << "Enter the date in the form of MMDDYYYY or MDDYYY :";
    		cin >> date;
    }
    
    bool checklength()
    {
    	if (date.length() == 7)
    		date = "0" + date;
    	if (date.length() != 8)
    	{
    		cout << "Your Lenght is wrong" << endl;
    		return false;
    	}
    	
    	return true;
    }
    
    bool checkdigit()
    {
    	n = 0;
    	while (n <=7)
    	{
    		if (date[n] >= 48 && date[n] <= 57)
    			n++;
    		else n = 50;
    			if(n == 50 || n == 0)
    			{
    				cout << "Use all numbers"<< endl;
    				return false;
    			}
    	}
    	return true;
    }
    
    int contoint()
    {
    	int a = 0, x = 0;
    	int len = month.length();
    	while (x < len)
    	{
    		a = a * 10 + int (month[x]) - 48;
    		x++;
    	}
    	return a;
    }
    
    int contointd()
    {
    	int b = 0, x=0;
    	int len = day.length();
    	while (x < len)
    	{ b=b * 10 + int (year[x]) - 48;
    	x++;
    	}
    	return  b;
    }
    
    int contointy()
    { 
    	int c = 0, x = 0;
    	int len = year.length();
    	while (x < len)
    	{ 
    		c = c * 10 + int ( year [x]) - 48;
    		x++ ;
    	}
    	return c;
    }
    
    bool checkmonth()
    { 
    	if (a == 0 || a > 12)
    	{ 
    		cout << "Month is Bad."<< endl;
    		return false;
    	}
    		return true;
    }
    
    bool checkday()
    {
    	if (b == 0 || b > 31)
    	{
    		cout<<" Day is Bad." << endl;
    		return false;
    	}
    	if (a == 2 && b == 29)
    	{
    		if (c % 400 == 0)
    			return true;
    		else if (c % 100 == 0)
    		{
    			cout << " Day is not correct." << endl;
    			return false;
    		}
    		else if ( c % 4 == 0 )
    			return true;
    		else 
    		{
    			cout << "The day is not correct." << endl;
    			return false;
    		}
    	}
    	if (a == 4 || a == 6 || a == 9 || a == 11)
    	{ 
    		if (b > 30) 
    		{
    			cout << " Day is bad." << endl;
    			return false;
    		}
    	}
    	return true;
    }
    	void printdate()
    	{
    		if (a == 1)
    			cout <<" January";
    		else if (a == 2)
    			cout << "February";
    		else if (a == 3)
    			cout << "March";
    		else if (a == 4)
    			cout << "April";
    		else if (a == 5) 
    			cout << "May";
    		else if (a == 6)
    			cout << "June";
    		else if (a == 7)
    			cout << "July";
    		else if (a == 8)
    			cout << "August";
    		else if (a == 9) 
    			cout << "September";
    		else if (a == 10) 
    			cout << "October";
    		else if (a == 11)
    			cout << "November";
    		else if (a == 12)
    			cout << "December";
    		cout << b<<" ,  " << c<< endl;
    	}

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Right here:
    Code:
    int contointd()
    {
    	int b = 0, x=0;
    	int len = day.length();
    	while (x < len)
    	{ b=b * 10 + int (year[x]) - 48;
    	x++;
    	}
    	return  b;
    }
    That should be day[x]
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > int contoint();
    > int contointd();
    > int contointy();
    > void printdate();
    Pass some parameters to these functions
    Then you can get rid of all your global variables.

    > int n,a,b,c;
    And while you're at it, give them better names.

    > if (ok = true)
    1. you meant ==
    2. since it is a boolean, you should just say either
    Code:
    if ( ok ) // its all good
    if ( !ok ) // its all bad
    In fact you can reduce the whole thing to
    Code:
    if ( checkmonth() && checkday() ) printdate();
    > if (date[n] >= 48 && date[n] <= 57)
    Don't assume you're using the ASCII character set
    if (date[n] >= '0' && date[n] <= '9')
    is both more readable and more portable

    > a = a * 10 + int (month[x]) - 48;
    Basically the same code in 3 different functions
    With some parameters, you could so easily make all 3 the same function.

    > if (a == 1)
    > cout <<" January";
    Use an array
    Code:
    static char *months[] = { "January", .... };
    cout << months[a-1];
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Writing a program to make a calendar
    By Northstar in forum C Programming
    Replies: 17
    Last Post: 11-07-2007, 11:34 AM
  2. how to convert from solar calendar to lunar calendar??
    By zaracattle in forum C++ Programming
    Replies: 7
    Last Post: 11-30-2006, 07:17 AM
  3. calendar program for linux
    By *ClownPimp* in forum Tech Board
    Replies: 1
    Last Post: 10-01-2005, 02:31 AM
  4. Calendar Program. What am I doing wrong?
    By Sektor in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2004, 11:39 PM
  5. writing a calendar program help please!!!
    By Newbie2006 in forum C Programming
    Replies: 7
    Last Post: 11-20-2002, 07:36 PM