|
-
March 25th, 2008, 12:02 AM
#1
Calendars / array pointers
update: down to one error
I'm justt trying to finish up an assignment and i hit a couple snags... anyways we have to print a calendar for the years 2008 - 2099 (depending on the year a user inputs)... anyways here's how im (trying to) do it. Its a mod of a previous assignment:
(updated)
Code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int yearload (int* days[12], int* startday[12], int year)
{
int after8=year-2008;
while (after8>0, after8++)
{
int i = 0;
while (i<12, i++)
{
if (year%4==0)
{
*startday[i]=(*startday[i]+366)%7;
}
else
{
*startday[i]=(*startday[i]+365)%7;
}
}
}
if (year%4 == 0)
{
int days[12]={31,29,31,30,31,30,31,31,30,31,30,31};
}
else
{
int days[12]={31,28,31,30,31,30,31,31,30,31,30,31};
}
return 0;
}
void pmonth (string month, int days, int day, int year)
{
//open cal.txt, and go to eof
ofstream cal ("cal.txt", ios::app);
//print first week and white space
cal<<month<<"\t\t"<<year<<"\n S M T W T F S\n";
int j = day + 1;
//print whitespace to move the 1st to the right spot
while (day > 0)
{
cal<<" ";
day--;
}
int date = 1;
while (date < days + 1)
{
//handles a new line
if ( j < 8 )
{
if (date < 10)
{
//theres an extra space to make everything look right (single digits)
cal<<" "<<date;
j++;
date++;
}
else
{
cal<<" "<<date;
j++;
date++;
}
}
//print a new line if we've run beyond the end of the week
else
{
cal<<"\n";
j=1;
}
}
//at the end of the month print two cr's
if (date=date+1)
cal<<"\n\n";
}
int main()
{
int year=2008;
cout<<"Enter a year: ";
cin>>year;
/*var ref:
month "2008"
"S M T W T F S"
1 2 3 4 5 6 7 <--Day
*/
string months[12]={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
//these are true for 2008 (or year "0" in our model)
int startday[12]={2,5,6,2,4,0,2,5,1,3,6,1};
int days[12]={31,29,31,30,31,30,31,31,30,31,30,31};
yearload (&days, &startday[12], year);
int day=2;
int i = 0;
while (i<12, i++)
{
pmonth (months[i], days[i], startday[i], year);
}
return 0;
}
... I'm getting a laundry list of compile errors mostly relating to pointers. ideas?
Update: down to one
Here's a list of errors:
Code:
1>c:\users\alex smith\documents\visual studio 2008\projects\cis150a3\cis150a3\calendar.cpp(94) : error C2664: 'yearload' : cannot convert parameter 1 from 'int (*)[12]' to 'int *'
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
Last edited by aosmith; March 25th, 2008 at 01:41 AM.
Reason: adding
-
March 25th, 2008, 02:14 AM
#2
Re: Calendars
1) How do you declare an array that will hold 12 integers?
2) How do you declare an array that will hold 12 pointers(to integers)?
-
March 25th, 2008, 02:39 AM
#3
Re: Calendars / array pointers
 Originally Posted by aosmith
update: down to one error
I'm justt trying to finish up an assignment and i hit a couple snags...
Code:
if (year%4 == 0)
{
int days[12]={31,29,31,30,31,30,31,31,30,31,30,31};
}
else
{
int days[12]={31,28,31,30,31,30,31,31,30,31,30,31};
}
Explain in your own words what this code does. It certainly doesn't do what you think it should have done.
Regards,
Paul McKenzie
-
March 25th, 2008, 08:24 AM
#4
Re: Calendars / array pointers
It should load an array with the number of days in each month depending on if that month is a leap year (and since our time range is only 2008-2099 thats the only rule we need to apply (evenly divisible by 4)).
-
March 25th, 2008, 08:28 AM
#5
Re: Calendars
 Originally Posted by 7stud
1) How do you declare an array that will hold 12 integers?
2) How do you declare an array that will hold 12 pointers(to integers)?
Code:
int days[12]={31,29,31,30,31,30,31,31,30,31,30,31};
-
March 25th, 2008, 02:38 PM
#6
Re: Calendars / array pointers
 Originally Posted by aosmith
Code:
if (year%4 == 0)
{
int days[12]={31,29,31,30,31,30,31,31,30,31,30,31};
}
else
{
int days[12]={31,28,31,30,31,30,31,31,30,31,30,31};
}
Another hint, variable scoping....
Viggy
-
March 25th, 2008, 02:54 PM
#7
Re: Calendars / array pointers
Also, the leap year rule is a bit more complicated than that. In this case it's fine, just FYI.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|