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 09-Nov-2004, 20:18
dilmv dilmv is offline
New Member
 
Join Date: Oct 2004
Posts: 16
dilmv is on a distinguished road

need some help formating a display


I have made a calender program and need some help formating the display. The DisplayCal function is where my program outputs the calender. I am having trouble figuring out how to format the calender correctly. I need the calender to line up the first day of the month with the day of the week and also to wrap the days when it gets to saturday. If anyone has any ideas I would appreciate the input.
Thanks
Dillon
Here is what I have...
CPP / C++ / C Code:
#include <iostream>
#include <iomanip>
using namespace std;

//Function Prototypes
int ReadMonth();
int ReadYear();
int IsLeap(int);
int DaysInMonth();
int GetMonthDays(int, int);
int GetMonthName(int, int);
int DaysInYears(int, int);
void DisplayCal(int, int, int);

int main()
{
	int month, year, leap, days, startDay;

	month = ReadMonth(); 
	year = ReadYear();
	leap = IsLeap(year); //determines if it is a leap year
	GetMonthName(month, year); //displays the month name
	days = GetMonthDays(month, year); //how many days the month has
	startDay = DaysInYears(year, month); //what day of the week the month starts on
	DisplayCal(year, days, startDay); //prints out the calender

	return 0;
}

int ReadMonth()
{
	int month;
	cout << "Enter month number: ";
	cin >> month;
	return month;
}

int ReadYear()
{
	int year;
	cout << "Enter year (1900 to 9999): ";
	cin >> year;
	return year;
}

int IsLeap(int year)
{
	int leap;
	if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0))
		leap = 1; //is a leap year
	else
		leap = 0; //is not a leap year
	return leap;
}

int GetMonthName(int month, int year)
{
	int days = 0;
	switch(month)
	{
		case 1: cout << "January";
				days = 31;
				break;
		case 3: cout << "March";
				days = 31;
				break;
		case 5: cout << "May";
				days = 31;
				break;
		case 7: cout << "July";
				days = 31;
				break;
		case 8: cout << "August";
				days = 31;
				break;
		case 10: cout << "October";
				days = 31;
				break;
		case 12: cout << "December";
				days = 31;
				break;
		case 4: cout << "April";
				days = 30;
				break;
		case 6: cout << "June";
				days = 30;
				break;
		case 9: cout << "September";
				days = 30;
				break;
		case 11: cout << "November";
				days = 30;
				break;
		case 2: cout << "February";
				days = 28 + IsLeap(year);
	}
	return days;
}

int GetMonthDays(int month, int year)
{
	int days = 0;
	switch(month)
	{
		case 1: 
				days = 31;
				break;
		case 3: 
				days = 31;
				break;
		case 5: 
				days = 31;
				break;
		case 7: 
				days = 31;
				break;
		case 8: 
				days = 31;
				break;
		case 10: 
				days = 31;
				break;
		case 12: 
				days = 31;
				break;
		case 4: 
				days = 30;
				break;
		case 6: 
				days = 30;
				break;
		case 9: 
				days = 30;
				break;
		case 11:
				days = 30;
				break;
		case 2: 
				days = 28 + IsLeap(year);
	}
	return days;
}

void DisplayCal(int year, int days, int startDay)
{
	cout << "\nS" << setw(4) << "M" << setw(4) << "T" << setw(4) << "W" << setw(4) << "T"
		 << setw(4) << "F" << setw(4) << "S\n";
	cout << setw(startDay);
	for (int i = 1; i <= days; i++)
	{
		if (startDay == 0 && i <= 7)
			cout << i << "\n";
	}

}

int DaysInYears(int year, int month) //calculates what day of the week the calendar starts on
{
	int totalLeap = 0, totalDays = 0, day_year = 0, startDay;
	for (int i = 1900; i < year; i++)
		totalLeap += IsLeap(i);
	totalDays = (year - 1900) * 365 + totalLeap;
	for (int i = 0; i < month; i++)
		day_year += GetMonthDays(i, year);
	totalDays += day_year;
	if (totalDays % 7 == 0)
		startDay = 0;
	if (totalDays % 7 == 1)
		startDay = 1;
	if (totalDays % 7 == 2)
		startDay = 2;
	if (totalDays % 7 == 3)
		startDay = 3;
	if (totalDays % 7 == 4)
		startDay = 4;
	if (totalDays % 7 == 5)
		startDay = 5;
	if (totalDays % 7 == 6)
		startDay = 6;
	else
		startDay = 7;
	return startDay;

}
  #2  
Old 09-Nov-2004, 20:39
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,335
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
Look into the printf() statement found in the header cstdio. You'll find it very easy to format text and numbers, whereas C++'s cout is quite difficult. See post #23 (second page) in this thread
__________________

During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence?
  #3  
Old 09-Nov-2004, 23:11
dilmv dilmv is offline
New Member
 
Join Date: Oct 2004
Posts: 16
dilmv is on a distinguished road
Quote:
Originally Posted by WaltP
Look into the printf() statement found in the header cstdio. You'll find it very easy to format text and numbers, whereas C++'s cout is quite difficult. See post #23 (second page) in this thread

actually I must use the cout, though I wish I could use the printf
  #4  
Old 10-Nov-2004, 00:52
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,335
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 dilmv
actually I must use the cout, though I wish I could use the printf
Well, unless someone else can help you with the iomanip stuff, I'm not going to be able to help you with the formatting commands. But the basic concept is:

CPP / C++ / C Code:
StartDay = the day of the week the month starts on (0=Sun, 1=Mon, etc.
EndDay = number of days of the month
CurrDay = 0, the current day-of-week (always starts with Sunday)

Loop from 0 to StartDay - 1
    output an empty day 
    increment CurrDay

Loop from 0 to EndDay-1
    output the day based on loop counter
    increment CurrDay
    if CurrDay = 7
        go to the next line.
        set CurrDay to 0

Loop from CurrDay to 6
    output an empty day 
That's the basic concept to displaying a monthly calendar
__________________

During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence?
  #5  
Old 10-Nov-2004, 02:07
Dr. Evil Dr. Evil is offline
Member
 
Join Date: Oct 2004
Location: Netherlands
Posts: 120
Dr. Evil will become famous soon enough
You could use the gotoxy() function to set the place where you want to print each amount of info. Go here for an example.
  #6  
Old 10-Nov-2004, 02:12
dilmv dilmv is offline
New Member
 
Join Date: Oct 2004
Posts: 16
dilmv is on a distinguished road
thanks for the suggestions everyone. I figured it out. Just had to do lots of playing around with for loops and the cout statement
  #7  
Old 10-Nov-2004, 13:09
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,335
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 Dr. Evil
You could use the gotoxy() function to set the place where you want to print each amount of info. Go here for an example.
Only if you have a compiler that has the gotoxy() function, which most do not.
__________________

During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence?
 
 

Recent GIDBlogReview: Gel laptop cooling pad 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
Image display problem Griff Web Design Forum 1 23-Aug-2004 18:47
Problems with changing display resolution vsseym Computer Software Forum - Windows 2 27-Jul-2004 11:01
uisng php to display php dopee MySQL / PHP Forum 6 14-May-2004 19:40
algorithm for display complex graph ctai010 C++ Forum 0 20-Mar-2004 06:24

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

All times are GMT -6. The time now is 21:00.


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