help on project

L

Luis

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 << " ";
}
}
 
S

Stewart Gordon

On 28/7/03 7:27 pm (UK time), Mike Wahler let loose these words:
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.
 
K

Kevin Goodsell

Stewart said:
On 28/7/03 7:27 pm (UK time), Mike Wahler let loose these words:


<snip>

std::endl is better in most cases.

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

-Kevin
 
S

Stewart Gordon

Stewart Gordon wrote:

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.
 
K

Kevin Goodsell

Stewart said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top