4 Replies - 442 Views - Last Post: 14 November 2011 - 04:48 PM Rate Topic: -----

#1 UrIkOn  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 53
  • Joined: 06-November 11

Anyone got any idea on this?

Posted 13 November 2011 - 06:16 AM

Hi,everyone!
I am writing a program display the calander using C++.First,the program prompts the user to enter year and first day of the year.
I have finished my code but i confirm my code not a true source code although i can get the result.
Is that anyone got any idea?How to shortcut my source code?
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{   
    int year;
    cout<<"Year: ";
    cin>>year;
    
    int First_day;
    cout<<"The first day of the year: ";
    cin>>First_day;
    
    cout<<endl;
    cout<<setw(20)<<"January "<<year<<endl;
    cout<<"_______________________________________"<<endl;
    cout<<setw(5)<<"Sun"<<setw(5)<<"Mon"<<setw(5)<<"Tue"<<setw(5)<<"Wed"<<setw(5)<<"Thu"<<setw(5)<<"Fri"<<setw(5)<<"Sat"<<endl;
    int count=0;
    const int day_per_line=7;
    for(int i=0;i<First_day;i++)
    { count++;
      cout<<setw(5)<<" ";    
    }
    int number;
    for(int j=1;j<=31;j++)
    { count++;
      if(count%day_per_line==0)
      {cout<<setw(5)<<j<<endl; }
      else
      {cout<<setw(5)<<j; }
      number=j;
    }

    cout<<endl;
    cout<<endl;
    
    if((year%4==0&&year%100!=0)||(year%400==0))
    {First_day=(First_day+number)%7;
     cout<<setw(20)<<"Febuary "<<year<<endl;
     cout<<"_______________________________________"<<endl;
     cout<<setw(5)<<"Sun"<<setw(5)<<"Mon"<<setw(5)<<"Tue"<<setw(5)<<"Wed"<<setw(5)<<"Thu"<<setw(5)<<"Fri"<<setw(5)<<"Sat"<<endl;
     count=0;
     for(int i=0;i<First_day;i++)
     { count++;
       cout<<setw(5)<<" ";    
     }
     for(int j=1;j<=29;j++)
     { count++;
       if(count%day_per_line==0)
       {cout<<setw(5)<<j<<endl; }
       else
       {cout<<setw(5)<<j; }
       number=j;
     }
    }
    else
    {First_day=(First_day+number)%7;
     cout<<setw(20)<<"Febuary "<<year<<endl;
     cout<<"_______________________________________"<<endl;
     cout<<setw(5)<<"Sun"<<setw(5)<<"Mon"<<setw(5)<<"Tue"<<setw(5)<<"Wed"<<setw(5)<<"Thu"<<setw(5)<<"Fri"<<setw(5)<<"Sat"<<endl;
     count=0;
     for(int i=0;i<First_day;i++)
     { count++;
       cout<<setw(5)<<" ";    
     }
     for(int j=1;j<=28;j++)
     { count++;
       if(count%day_per_line==0)
       {cout<<setw(5)<<j<<endl; }
       else
       {cout<<setw(5)<<j; }
       number=j;
     }
     }
    
    cout<<endl;
    cout<<endl;
    
    First_day=(First_day+number)%7;
    cout<<setw(20)<<"March "<<year<<endl;
    cout<<"_______________________________________"<<endl;
    cout<<setw(5)<<"Sun"<<setw(5)<<"Mon"<<setw(5)<<"Tue"<<setw(5)<<"Wed"<<setw(5)<<"Thu"<<setw(5)<<"Fri"<<setw(5)<<"Sat"<<endl;
    count=0;
    for(int i=0;i<First_day;i++)
    { count++;
      cout<<setw(5)<<" ";    
    }
    for(int j=1;j<=31;j++)
    { count++;
      if(count%day_per_line==0)
      {cout<<setw(5)<<j<<endl; }
      else
      {cout<<setw(5)<<j; }
      number=j;
    }
    
    cout<<endl;
    cout<<endl;
    
    First_day=(First_day+number)%7;
    cout<<setw(20)<<"April "<<year<<endl;
    cout<<"_______________________________________"<<endl;
    cout<<setw(5)<<"Sun"<<setw(5)<<"Mon"<<setw(5)<<"Tue"<<setw(5)<<"Wed"<<setw(5)<<"Thu"<<setw(5)<<"Fri"<<setw(5)<<"Sat"<<endl;
    count=0;
    for(int i=0;i<First_day;i++)
    { count++;
      cout<<setw(5)<<" ";    
    }
    for(int j=1;j<=30;j++)
    { count++;
      if(count%day_per_line==0)
      {cout<<setw(5)<<j<<endl; }
      else
      {cout<<setw(5)<<j; }
      number=j;
    }
    
    cout<<endl;
    cout<<endl;
    
    First_day=(First_day+number)%7;
    cout<<setw(20)<<"May "<<year<<endl;
    cout<<"_______________________________________"<<endl;
    cout<<setw(5)<<"Sun"<<setw(5)<<"Mon"<<setw(5)<<"Tue"<<setw(5)<<"Wed"<<setw(5)<<"Thu"<<setw(5)<<"Fri"<<setw(5)<<"Sat"<<endl;
    count=0;
    for(int i=0;i<First_day;i++)
    { count++;
      cout<<setw(5)<<" ";    
    }
    for(int j=1;j<=31;j++)
    { count++;
      if(count%day_per_line==0)
      {cout<<setw(5)<<j<<endl; }
      else
      {cout<<setw(5)<<j; }
      number=j;
    }
    
    cout<<endl;
    cout<<endl;
    
    First_day=(First_day+number)%7;
    cout<<setw(20)<<"June "<<year<<endl;
    cout<<"_______________________________________"<<endl;
    cout<<setw(5)<<"Sun"<<setw(5)<<"Mon"<<setw(5)<<"Tue"<<setw(5)<<"Wed"<<setw(5)<<"Thu"<<setw(5)<<"Fri"<<setw(5)<<"Sat"<<endl;
    count=0;
    for(int i=0;i<First_day;i++)
    { count++;
      cout<<setw(5)<<" ";    
    }
    for(int j=1;j<=30;j++)
    { count++;
      if(count%day_per_line==0)
      {cout<<setw(5)<<j<<endl; }
      else
      {cout<<setw(5)<<j; }
      number=j;
    }
    
    cout<<endl;
    cout<<endl;
    
    First_day=(First_day+number)%7;
    cout<<setw(20)<<"July "<<year<<endl;
    cout<<"_______________________________________"<<endl;
    cout<<setw(5)<<"Sun"<<setw(5)<<"Mon"<<setw(5)<<"Tue"<<setw(5)<<"Wed"<<setw(5)<<"Thu"<<setw(5)<<"Fri"<<setw(5)<<"Sat"<<endl;
    count=0;
    for(int i=0;i<First_day;i++)
    { count++;
      cout<<setw(5)<<" ";    
    }
    for(int j=1;j<=31;j++)
    { count++;
      if(count%day_per_line==0)
      {cout<<setw(5)<<j<<endl; }
      else
      {cout<<setw(5)<<j; }
      number=j;
    }
    
    cout<<endl;
    cout<<endl;
    
    First_day=(First_day+number)%7;
    cout<<setw(20)<<"August "<<year<<endl;
    cout<<"_______________________________________"<<endl;
    cout<<setw(5)<<"Sun"<<setw(5)<<"Mon"<<setw(5)<<"Tue"<<setw(5)<<"Wed"<<setw(5)<<"Thu"<<setw(5)<<"Fri"<<setw(5)<<"Sat"<<endl;
    count=0;
    for(int i=0;i<First_day;i++)
    { count++;
      cout<<setw(5)<<" ";    
    }
    for(int j=1;j<=31;j++)
    { count++;
      if(count%day_per_line==0)
      {cout<<setw(5)<<j<<endl; }
      else
      {cout<<setw(5)<<j; }
      number=j;
    }
    
    cout<<endl;
    cout<<endl;
    
    First_day=(First_day+number)%7;
    cout<<setw(20)<<"September "<<year<<endl;
    cout<<"_______________________________________"<<endl;
    cout<<setw(5)<<"Sun"<<setw(5)<<"Mon"<<setw(5)<<"Tue"<<setw(5)<<"Wed"<<setw(5)<<"Thu"<<setw(5)<<"Fri"<<setw(5)<<"Sat"<<endl;
    count=0;
    for(int i=0;i<First_day;i++)
    { count++;
      cout<<setw(5)<<" ";    
    }
    for(int j=1;j<=30;j++)
    { count++;
      if(count%day_per_line==0)
      {cout<<setw(5)<<j<<endl; }
      else
      {cout<<setw(5)<<j; }
      number=j;
    }
    
    cout<<endl;
    cout<<endl;
    
    First_day=(First_day+number)%7;
    cout<<setw(20)<<"October "<<year<<endl;
    cout<<"_______________________________________"<<endl;
    cout<<setw(5)<<"Sun"<<setw(5)<<"Mon"<<setw(5)<<"Tue"<<setw(5)<<"Wed"<<setw(5)<<"Thu"<<setw(5)<<"Fri"<<setw(5)<<"Sat"<<endl;
    count=0;
    for(int i=0;i<First_day;i++)
    { count++;
      cout<<setw(5)<<" ";    
    }
    for(int j=1;j<=31;j++)
    { count++;
      if(count%day_per_line==0)
      {cout<<setw(5)<<j<<endl; }
      else
      {cout<<setw(5)<<j; }
      number=j;
    }
    
    cout<<endl;
    cout<<endl;
    
    First_day=(First_day+number)%7;
    cout<<setw(20)<<"November "<<year<<endl;
    cout<<"_______________________________________"<<endl;
    cout<<setw(5)<<"Sun"<<setw(5)<<"Mon"<<setw(5)<<"Tue"<<setw(5)<<"Wed"<<setw(5)<<"Thu"<<setw(5)<<"Fri"<<setw(5)<<"Sat"<<endl;
    count=0;
    for(int i=0;i<First_day;i++)
    { count++;
      cout<<setw(5)<<" ";    
    }
    for(int j=1;j<=30;j++)
    { count++;
      if(count%day_per_line==0)
      {cout<<setw(5)<<j<<endl; }
      else
      {cout<<setw(5)<<j; }
      number=j;
    }
    
    cout<<endl;
    cout<<endl;
    
    First_day=(First_day+number)%7;
    cout<<setw(20)<<"December "<<year<<endl;
    cout<<"_______________________________________"<<endl;
    cout<<setw(5)<<"Sun"<<setw(5)<<"Mon"<<setw(5)<<"Tue"<<setw(5)<<"Wed"<<setw(5)<<"Thu"<<setw(5)<<"Fri"<<setw(5)<<"Sat"<<endl;
    count=0;
    for(int i=0;i<First_day;i++)
    { count++;
      cout<<setw(5)<<" ";    
    }
    for(int j=1;j<=31;j++)
    { count++;
      if(count%day_per_line==0)
      {cout<<setw(5)<<j<<endl; }
      else
      {cout<<setw(5)<<j; }
      number=j;
    }
    cout<<endl;
    
    system("pause");
    
    return 0;
    
}


Is This A Good Question/Topic? 0
  • +

Replies To: Anyone got any idea on this?

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2889
  • View blog
  • Posts: 10,002
  • Joined: 08-August 08

Re: Anyone got any idea on this?

Posted 13 November 2011 - 06:35 AM

Functions are your friends. Use them.
Was This Post Helpful? 2
  • +
  • -

#3 hulla  Icon User is offline

  • Writing Lines


Reputation: 49
  • View blog
  • Posts: 732
  • Joined: 05-March 11

Re: Anyone got any idea on this?

Posted 14 November 2011 - 07:07 AM

Yeah you need to use functions to make your code more manageable. Functions also allow you to very quickly and safely modify your code. Replace the system("PAUSE"); with cin.sync(); cin.get(); so that it is platform independent.
Was This Post Helpful? 1
  • +
  • -

#4 The Blue Phoenix  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 84
  • Joined: 07-March 10

Re: Anyone got any idea on this?

Posted 14 November 2011 - 11:48 AM

View Posthulla, on 14 November 2011 - 07:07 AM, said:

Yeah you need to use functions to make your code more manageable. Functions also allow you to very quickly and safely modify your code. Replace the system("PAUSE"); with cin.sync(); cin.get(); so that it is platform independent.


also try to make your code more readable. add comments so you understand what you are doing at all times. also be shure to use variable and function names that make sence. Little stuff goes a long way.

This post has been edited by The Blue Phoenix: 14 November 2011 - 11:48 AM

Was This Post Helpful? 1
  • +
  • -

#5 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 275
  • View blog
  • Posts: 1,778
  • Joined: 20-September 08

Re: Anyone got any idea on this?

Posted 14 November 2011 - 04:48 PM

Also ... you may like to learn some ways to get numeric input that is valid ... and ways ... so that your program will not crash if non-numeric input is entered when your program is expecting a number.

This following link demo's just one way that this might be done ...

http://developers-he...index.php/topic,2019.0.html
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1