GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 08-Sep-2005, 19:14
tones1986 tones1986 is offline
New Member
 
Join Date: Sep 2005
Posts: 5
tones1986 is on a distinguished road

C++ Program Print Calendar HELP


Well ok the idea of this program is that it will print out the calendar for any given month given to it by user input. I am writing this using PICO under the UNIX system. I get the following error message now and i do not know why:


Segmentation Fault (Core Dumped)

It looks exactly like this when ran:

mp% assign1
Month and Year? : 5 2005
May 2005
S M Tu W Th F S
Segmentation Fault (core dumped)

So it does get out the user input, and displays the headers, but for some reason now its pissed at me... i cant figure out why. It was fine before i started playing around with the Return statments at the end of the functions and tried the things that are // in main. Please someone take a look and help me get through this. Thanks alot. Will mean alot for some help.

CPP / C++ / C Code:
#include <iostream>
#include <iomanip>
#include <string>

using std::cout;
using std::cin;
using std::endl;
using std::boolalpha;
using std::string;
using std::setw;


//Function prototypes
int leaptest(int);
string calheader(int, int);
int days(int, int);
int weekday(int, int);

//Sets up a bool to test for leapyears
bool leapyear;

int main()
{
  // Initialize space for month and year input
  int month, year;
  //Asks for input
  cout << "Month and Year? : ";
  // inputs data into space
  cin >> month >> year;

int day, dayofweek, leapyeartest;

//Calls leaptest function sending year
leaptest(year);

// creates a string for real month
string realmonth;

//calls the function that prints header info
calheader(month, year);

// calls days function
days(month, year);

// calls function to find out days of week
weekday(month, year);

//for (int counter = 0; counter < day; counter++)
//    cout << setw(4) << "- ";
}

int leaptest(int year)
{

if (year % 4 == 0) {
  if (year % 100 == 0) {
    if (year % 400 == 0)
leapyear = true;
  else leapyear = false;
  }
  else leapyear = true;
}
else leapyear = false;
return leapyear;
}

string calheader(int month,int year)
{

string realmonth;
if (month < 1 || month > 12)
   cout << "Incorrect Input!";
else if (month == 1)
  realmonth = "January";
else if (month == 2)
  realmonth = "Febuary";
else if (month == 3)
  realmonth = "March";
else if (month == 4)
  realmonth = "April";
else if (month == 5)
   realmonth = "May";
else if (month == 6)
   realmonth = "June";
else if (month == 7)
   realmonth = "July";
else if (month == 8)
   realmonth = "August";
else if (month == 9)
  realmonth = "September";
else if (month == 10)
  realmonth = "October";
else if (month == 11)
  realmonth = "November";
else if (month == 12)
  realmonth = "December";

cout << setw(10) <<  realmonth << "   " <<  year << endl;

cout << setw(4) << " S " << " M " << " Tu " << " W "
     << " Th " << " F "  << " S " << endl;

}

int days(int month,int year)
{
int days;
int month2;

month2 = month + 11;
month2 = month2 * 7;
month2 = month2 % 12;

if (month2 < 7)
  days = 31;
else
  days = 30;
if (month == 2)
  {
   if (leaptest(year) == true)
    {
    days = 29;
    }
   else
   {
     days = 28;
   }
//return days;
}
}
int weekday(int month,int year)
{
int yearedit;
int totaldays;
int monthedit;
int correction;
int daysofweek;
yearedit = year - 1;
totaldays = yearedit * 365;
totaldays += (yearedit / 4);
totaldays -= (yearedit / 100);
totaldays += (yearedit / 400);
monthedit = (month * 367) - (362) / (12);
totaldays += monthedit;
totaldays += 1;
if (month <= 2)
   correction = 0;
if (month > 2 || leaptest(year) == true)
   correction = -1;
if (month > 2 || leaptest(year) == true)
   correction = -2;
totaldays += correction;
daysofweek = totaldays % 7;
cout << daysofweek << endl;

//return daysofweek;

}


Last edited by LuciWiz : 09-Sep-2005 at 00:06. Reason: Please insert your C++ code between [c++] & [/c++] tags
  #2  
Old 08-Sep-2005, 20:58
Guidelines Plz Guidelines Plz is offline
Junior Member
 
Join Date: Sep 2005
Posts: 87
Guidelines Plz is on a distinguished road
Your code is unreadable without the formatting. Please read the http://www.gidforums.com/t-5566.html for suggestions on helping us read your posts and get the best info into your request.
  #3  
Old 08-Sep-2005, 21:05
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,245
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Try adding more output statements to pinpoint where the problem is. Once you pinpoint closer where the problem is, we can tell better what the problem is.

And use code tags not quotes to post code.
__________________

Age is unimportant -- except in cheese
 
 

Recent GIDBlogPython ebook by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Type casts ? kai85 C++ Forum 12 23-Jun-2005 12:04
[TUTORIAL] Calling an external program in C (Linux) dsmith C Programming Language 4 22-Apr-2005 13:30
fltk-2.0 cvs Plumb FLTK Forum 20 13-Nov-2004 07:10
Help with calendar program mike3340 C++ Forum 0 18-Nov-2003 20:25
Guestbook error BobbyDouglas Web Design Forum 1 16-Oct-2003 22:17

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 18:23.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.