Thread: Calendar Program I'm having trouble with

  1. #1
    Registered User
    Join Date
    May 2013
    Posts
    5

    Calendar Program I'm having trouble with

    Hey everyone, beginner posting again. I am working on this for a school HW assignment. I have the code thus far to make a 'calendar' of sorts. I can't seem to get it to make a new line at the proper spot and continue counting the days of the month.

    I can kind of see why. I know it has something to do with my displayTable function. I need it to make a new line before printing the date... It also has this problem when I input 6 for the offset, it will print the date below Sun, but then make a line.

    I don't even know if I am asking the right question, it's hard for me to word. Anyway, I've been working on this staring at the screen for hours and can't seem to find a solution myself or through google. There are similar problems, but I just can't get mine to work.

    Thank you for your help. I was even going to visit my tutor today about this problem, but he had to cancel. :frusty:

    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    void displayHeader()
    {
       cout << "  Su  Mo  Tu  We  Th  Fr  Sa" << endl;
       return;
    }
    
    
    
    
    int getDays()
    {
       int days;
       cout << "Number of days: ";
       cin >> days;
       return days;
    }
    
    
    
    
    int getOffset()
    {
       int offset;
       cout << "Offset: ";
        cin >> offset;
       return offset;
    }
    
    
    void displayTable(int numDays, int offset)
    {
    
    
       // Variable
    
    
       int days;
    
    
       if (offset == 0)  // Mon
          cout << setw(4)  << " ";
       else if (offset == 1)  // Tues
          cout << setw(8)  << " ";
       else if (offset == 2)  // Wed
          cout << setw(12) << " ";
       else if (offset == 3)  // Thurs
          cout << setw(16) << " ";
       else if (offset == 4)  // Fri
          cout << setw(20) << " ";
       else if (offset == 5)  // Sat
          cout << setw(24) << " ";
       else (offset == 6)  // Sun
               ;
    
    
       for (days = 1; days <= numDays; days++)
       {
          cout << "  " << setw(2) << days;
          if ((days + offset) % 7 == 0)
          cout  << "\n";
       }
       cout << endl;
       return;
    }
    
    
    
    
    int main()
    {
       int numDays = getDays();
       int offset = getOffset();
       displayHeader();
    
    
       displayTable(numDays, offset);
       return 0;
    }
    This is what the output is showing:

    Code:
    Number of days: 30
    Offset: 3
      Su  Mo  Tu  We  Th  Fr  Sa
                       1   2   3   4
       5   6   7   8   9  10  11
      12  13  14  15  16  17  18
      19  20  21  22  23  24  25
      26  27  28  29  30

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    The printing of the spaces for the offset is wrong. Yout table starts with Su therefore offset 0 should be no spaces .
    Next the modulo 7 doesn't work if your counter starts from 1.
    try this way
    Code:
       for (days = 0; days < numDays; days++)
       {
          if ((days + offset) % 7  == 0 && days != 0 )
          cout  << "\n";
          cout << "  " << setw(2) << days + 1;
      }
    the additional condition ( && days != 0 ) is needed because you don't want to start with printing as '\n' if offset is 0

    Kurt

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
       if (offset == 0)  // Mon
          cout << setw(4)  << " ";
       else if (offset == 1)  // Tues
          cout << setw(8)  << " ";
       else if (offset == 2)  // Wed
          cout << setw(12) << " ";
       else if (offset == 3)  // Thurs
          cout << setw(16) << " ";
       else if (offset == 4)  // Fri
          cout << setw(20) << " ";
       else if (offset == 5)  // Sat
          cout << setw(24) << " ";
       else (offset == 6)  // Sun
               ;
    This is equal to
    cout << setw(4 + 4 * offset) << " ";
    (except for offset==6)
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Elysia View Post
    Code:
       if (offset == 0)  // Mon
          cout << setw(4)  << " ";
       else if (offset == 1)  // Tues
          cout << setw(8)  << " ";
       else if (offset == 2)  // Wed
          cout << setw(12) << " ";
       else if (offset == 3)  // Thurs
          cout << setw(16) << " ";
       else if (offset == 4)  // Fri
          cout << setw(20) << " ";
       else if (offset == 5)  // Sat
          cout << setw(24) << " ";
       else (offset == 6)  // Sun
               ;
    This is equal to
    cout << setw(4 + 4 * offset) << " ";
    (except for offset==6)
    In other words, it's exactly equal to:
    Code:
    cout << setw(4 * (offset + 1) % 7) << " ";
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calendar Program
    By tgarrisoniv in forum C Programming
    Replies: 8
    Last Post: 03-06-2012, 02:44 PM
  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. Help with calendar program please
    By blindleaf in forum C Programming
    Replies: 1
    Last Post: 10-31-2002, 11:11 AM
  4. Calendar program
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 07-22-2002, 08:44 PM
  5. calendar program
    By dharmastum in forum C Programming
    Replies: 3
    Last Post: 01-25-2002, 05:35 AM