By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,188 Members | 980 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,188 IT Pros & Developers. It's quick & easy.

help on project

P: n/a
My project is to make a calander which will print out every month depending
on the year leap year or not) and the first day of the year. this is what i
have so far, but i don't know how to go about making it go to a new line
when the end od the current line is reaxched. Please take a look at my code,
and tell me if you have suggestions. It has no comments, and is pretty raw,
i am to the point of ust printing only 1 month, not all 12 yet. Please help
out, if i don't pass this, I may not pass the class.

#include <iostream>

using namespace std;

void getData (int& year , int& firstDay);
void header (int month);
void goToFirstDay(int firstDay, int& columnCount);
void daysInMonth(int month,int year, int& numDaysInMonth );
bool isLeapYear(int year);
void printOneMonth(int month , int year,int firstDay);
void printNumbers (int numDaysInMonth);

int main ( )
{
int month = 2;
int year; //info given from getData()
int firstDay ; // info from getData()
getData (year , firstDay);
printOneMonth( month, year, firstDay);

return 0;
}

void getData (int& year , int& firstDay)
{
cout << "what year do you want the calander for?" ;
cin >> year;
cout << "what day of the week does january 1 fall on?" << endl;
cout << "(enter 0 for sunday , 1 for monday, etc.)";
cin >> firstDay;
}


void header (int month)
{
switch (month)
{

case 1 : cout << " January" << endl;
cout << " S M T W T F S " << endl;
cout << "--------------------" << endl;
break;

case 2 : cout << " Febuary" << endl;
cout << " S M T W T F S " << endl;
cout << "--------------------" << endl;
break;

case 3 : cout << " March" << endl;
cout << " S M T W T F S " << endl;
cout << "--------------------" << endl;
break;

case 4 : cout << " April" << endl;
cout << " S M T W T F S " << endl;
cout << "--------------------" << endl;
break;

case 5 : cout << " May" << endl;
cout << " S M T W T F S " << endl;
cout << "--------------------" << endl;
break;

case 6 : cout << " June" << endl;
cout << " S M T W T F S " << endl;
cout << "--------------------" << endl;
break;

case 7 : cout << " July" << endl;
cout << " S M T W T F S " << endl;
cout << "--------------------" << endl;
break;

case 8 : cout << " August" << endl;
cout << " S M T W T F S " << endl;
cout << "--------------------" << endl;
break;
case 9 : cout << " September" << endl;
cout << " S M T W T F S " << endl;
cout << "--------------------" << endl;
break;

case 10 :cout << " October" << endl;
cout << " S M T W T F S " << endl;
cout << "--------------------" << endl;
break;

case 11: cout << " November" << endl;
cout << " S M T W T F S " << endl;
cout << "--------------------" << endl;
break;

case 12: cout << " December" << endl;
cout << " S M T W T F S " << endl;
cout << "--------------------" << endl;
break;
}
}


void goToFirstDay(int firstDay, int& columnCount )
{
int count = 0;

cout << " ";
if (firstDay > 0)
{
for (count; count < firstDay ;count++)

{
cout << " ";
}
}

columnCount = count + (count*3);

}



void daysInMonth( int month, int year, int& numDaysInMonth )
{

switch (month)
{
case 1 :
case 3 :
case 5 :
case 7 :
case 8 :
case 10 :
case 12 : numDaysInMonth = 31;
break;
case 4 :
case 6 :
case 9 :
case 11 : numDaysInMonth = 30;
break;

case 2 : if (isLeapYear(year))
{

numDaysInMonth = 29;

}
else if (!isLeapYear(year))
{
numDaysInMonth = 28;

}
}

}



bool isLeapYear(int year)
{
if (year % 400 == 0){
return true;
}

if (year % 100 == 0){
return false;
}

if (year % 4 == 0){
return true;
}

return false;
}




void printOneMonth(int month,int year,int firstDay)
{
int numDaysInMonth;
int columnCount;

header(month);
daysInMonth( month, year, numDaysInMonth );
goToFirstDay(firstDay,columnCount);
printNumbers (numDaysInMonth);

}
void printNumbers (int numDaysInMonth)
{
int count = 1;

for (count;count<=numDaysInMonth;count++)
{

cout << count << " ";
}
}

Jul 19 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On 28/7/03 7:27 pm (UK time), Mike Wahler let loose these words:
<snip>
You cause subsequent output to appear on a 'new line' with
the, um, newline character.

std::cout << '\n';

<snip>

std::endl is better in most cases.

Stewart.

--
My e-mail is valid but not my primary mailbox. Please keep replies on
on the 'group where everyone may benefit.

Jul 19 '05 #2

P: n/a
Stewart Gordon wrote:
On 28/7/03 7:27 pm (UK time), Mike Wahler let loose these words:
<snip>
You cause subsequent output to appear on a 'new line' with
the, um, newline character.

std::cout << '\n';


<snip>

std::endl is better in most cases.


Why? Flushing the stream when it's not necessary is wasteful.

-Kevin

Jul 19 '05 #3

P: n/a
On 1/8/03 6:52 pm (UK time), Kevin Goodsell let loose these words:
Stewart Gordon wrote:

<snip>
std::endl is better in most cases.


Why? Flushing the stream when it's not necessary is wasteful.


Mabye "most cases" was a slight exaggeration. But if cout remains
unflushed when the program exits or user input is requested, there's the
odd chance of something getting messed up.

Stewart.

--
My e-mail is valid but not my primary mailbox. Please keep replies on
on the 'group where everyone may benefit.

Jul 19 '05 #4

P: n/a
Stewart Gordon wrote:
On 1/8/03 6:52 pm (UK time), Kevin Goodsell let loose these words:
Stewart Gordon wrote:


<snip>
std::endl is better in most cases.


Why? Flushing the stream when it's not necessary is wasteful.

Mabye "most cases" was a slight exaggeration. But if cout remains
unflushed when the program exits or user input is requested, there's the
odd chance of something getting messed up.


I don't know about that... Unless I'm mistaken, cout will be flushed
when it is destroyed, so the program ending shouldn't be a problem
(although it could be, depending on how the program ends - but as long
as it ends correctly it should be fine).

Also, cout is automatically flushed when cin is used, I think.

In the case of cout you'll probably want it flushed more frequently than
other streams, and it probably will be flushed in some cases where you
don't necessarily want it, but in general streams should only be flushed
when there's a reason.

-Kevin

Jul 19 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.