help on project

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Luis

    help on project

    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(in t firstDay, int& columnCount);
    void daysInMonth(int month,int year, int& numDaysInMonth );
    bool isLeapYear(int year);
    void printOneMonth(i nt 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(in t 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(yea r))
    {

    numDaysInMonth = 29;

    }
    else if (!isLeapYear(ye ar))
    {
    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(i nt month,int year,int firstDay)
    {
    int numDaysInMonth;
    int columnCount;

    header(month);
    daysInMonth( month, year, numDaysInMonth );
    goToFirstDay(fi rstDay,columnCo unt);
    printNumbers (numDaysInMonth );



    }


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



    for (count;count<=n umDaysInMonth;c ount++)
    {

    cout << count << " ";
    }
    }





  • Stewart Gordon

    #2
    Re: help on project

    On 28/7/03 7:27 pm (UK time), Mike Wahler let loose these words:
    <snip>[color=blue]
    > You cause subsequent output to appear on a 'new line' with
    > the, um, newline character.
    >
    > std::cout << '\n';[/color]
    <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.

    Comment

    • Kevin Goodsell

      #3
      Re: help on project

      Stewart Gordon wrote:[color=blue]
      > On 28/7/03 7:27 pm (UK time), Mike Wahler let loose these words:
      > <snip>
      >[color=green]
      >> You cause subsequent output to appear on a 'new line' with
      >> the, um, newline character.
      >>
      >> std::cout << '\n';[/color]
      >
      > <snip>
      >
      > std::endl is better in most cases.
      >[/color]

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

      -Kevin

      Comment

      • Stewart Gordon

        #4
        Re: help on project

        On 1/8/03 6:52 pm (UK time), Kevin Goodsell let loose these words:
        [color=blue]
        > Stewart Gordon wrote:[/color]
        <snip>[color=blue][color=green]
        >> std::endl is better in most cases.
        >>[/color]
        >
        > Why? Flushing the stream when it's not necessary is wasteful.[/color]

        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.

        Comment

        • Kevin Goodsell

          #5
          Re: help on project

          Stewart Gordon wrote:
          [color=blue]
          > On 1/8/03 6:52 pm (UK time), Kevin Goodsell let loose these words:
          >[color=green]
          >> Stewart Gordon wrote:[/color]
          >
          > <snip>
          >[color=green][color=darkred]
          >>> std::endl is better in most cases.
          >>>[/color]
          >>
          >> Why? Flushing the stream when it's not necessary is wasteful.[/color]
          >
          >
          > 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.
          >[/color]

          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

          Comment

          Working...