Challenging code! Help much appreciated:)

Hello
I am working on a program that asks the users for :

A)what year they want the calender, (eg. 2012)

B) then asks on what # day of the week (eg. #1-7) January 1st is on.

and then creates the calender accordingly.

*erroneous/unexpected inputs havebto be addressed with error statement
*must print the months in 3x4 format.


The project is much longer this is just the stuff im having a tough time with.
Any & all help would be greatly appreciated!

Please note that this is not a homework site. We won't do your homework for you. The purpose of homework is that you learn by doing. However we are always willing to help solve problems you encountered, correct mistakes you made in your code and answer your questions.

We didn't see your attempts to solve this problem yourself and so we cannot correct mistakes you didn't made and answer questions you didn't ask. To get help you should do something yourself and get real problems with something. If your problem is "I don't understand a thing", then you should go back to basics and study again.


You promised:
Challenging code

but there is no code, not even simple code, within your post. False advertising.
include<stdio.h>
#include<iostream>
#define TRUE 1
#define FALSE 0
using namespace std;
#include <iomanip>

int days_in_month[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
char *months[] =
{
" ",
"\n\n\nJanuary",
"\n\n\nFebruary",
"\n\n\nMarch",
"\n\n\nApril",
"\n\n\nMay",
"\n\n\nJune",
"\n\n\nJuly",
"\n\n\nAugust",
"\n\n\nSeptember",
"\n\n\nOctober",
"\n\n\nNovember",
"\n\n\nDecember"
};


int inputyear(void)
{
int year;

cout<<"Please enter a year (example: 1999) : ";
cin>>year;
if ((year % 400 == 0) || ((year % 100 == 0) ^ (year % 4 == 0)))
{

cout << "\nThe year is leap year.";
}
else
{

cout << "\nThe year is odd year.";
}

return year;
}

int determinedaycode(int year)
{
int daycode;
cout << "\nEnter the first day of January: 1 for Monday, 2 for Tuesday etc. ";
cin >> daycode;
while (daycode < 0 || daycode > 7)
{
cout << "\nThe number entered is invalid.\nPlease enter a number between 1 and 7. ";
cin >> daycode;
}
return daycode;
}


int determineleapyear(int year)
{
if (year % 4 == FALSE && year % 100 != FALSE || year % 400 == FALSE)
{
days_in_month[2] = 29;
return TRUE;
}
else
{
days_in_month[2] = 28;
return FALSE;
}
}

void calendar(int year, int daycode)
{
int month, day;
for (month = 1; month <= 12; month++)
{
cout<<months[month];
cout<<"\n\nSun Mon Tue Wed Thu Fri Sat\n";

// Correct the position for the first date
for (day = 1; day <= 1 + daycode * 5; day++)
{
cout<<" ";
}

// Print all the dates for one month
for (day = 1; day <= days_in_month[month]; day++)
{
cout<< setw(2)<<day;

// Is day before Sat? Else start next line Sun.
if ((day + daycode) % 7 > 0)
cout<<" ";
else
cout<<"\n ";
}
// Set position for next month
daycode = (daycode + days_in_month[month]) % 7;
}
}

int main(void)
{

int year, daycode, leapyear;

year = inputyear();

daycode = determinedaycode(year);
determineleapyear(year);
calendar(year, daycode);
cout<<"\n";
}
Sorry I should have posted it before.
So i am having trouble with this part.
int determinedaycode(int year)
{
int daycode;
cout << "\nEnter the first day of January: 1 for Monday, 2 for Tuesday etc. ";
cin >> daycode;
while (daycode < 0 || daycode > 7)
{
cout << "\nThe number entered is invalid.\nPlease enter a number between 1 and 7. ";
cin >> daycode;
}
return daycode;
}
In the while loop i dont know how to catch the wrong inputs if the user entered a char just say somebodys name

I tried to use bool valied statement but no luck
Last edited on
In the while loop i dont know how to catch the wrong inputs if the user entered a char just say somebodys name

See http://www.cplusplus.com/forum/beginner/203596/#msg967222
Thanx that cleared a lot of things.
I have one more question..
My program prints the months beneath each other but I want to print to print them beside each other....
I know i should be figuring it out by myself but if somebody can guide a little bit it will be a great help
Plz and thnx
closed account (3ApN6Up4)
My program prints the months beneath each other but I want to print to print them beside each other....


cout << "January February Match April May June July August September October November December" << endl;
Haha..
Anyways jokes apart
I was thinking about using multidimensional arrays. But need a brief overview. Is there any discussion on it.
Hello OP

See if this can be of any use to you or not ....
http://www.cplusplus.com/forum/general/202016/
In essence, you want to shift from
foo
1 2
3 4

bar
1 2
3 4

gaz
1 2
3 4

into
foo  bar
1 2  1 2
3 4  3 4

gaz
1 2
3 4

Printing one month was relatively easy:
name newline
some numbers newline
some numbers newline

Now you want:
some names newline
some numbers * N, with spacing newline
some numbers * N, with spacing newline

That does not require multidim arrays. Little bit more nested loops. (And the special case handling.)
Topic archived. No new replies allowed.