问题
I'm trying to write a program that outputs a calendar. The user has to input the day in which the month starts (Monday-0, Tuesday-1, etc.), and how many days are in the month. Depending on what day the month starts, the calendar dates will start under that specific day. The issues I'm having is, I'm not sure how to get the calendar to start under a specific day, and I'm not sure how to get the dates to go to a new line after the 7 days. Any help would be appreciated. We haven't learnt much so far, so I'm really only allowed to use the basics, no functions or things like that.
Here's what I've got so far. I could be far out.
#include <iostream>
#include <iomanip>
#include <conio.h>
using namespace std;
int main()
{
int monthStartDay, daysInMonth;
cout << "Enter the first day of the month: ";
cin >> monthStartDay;
cout << "Enter how many days are in the month: ";
cin >> daysInMonth;
cout<<"\nSun Mon Tue Wed Thu Fri Sat";
cout<<"\n\n"<<setw(2);
for (int x=1; x <= daysInMonth; x++){
cout << x << setw(6);
for (int i=1; i == 6; i++) {
cout << "\n";
}
}
return 0;
}
回答1:
The solution is using a new index, that will show a position in your calendar row. That is:
int startDayPostion = (monthStartDay + 1) % 7;
becouse you are counting zero from Monday, but your print is starting from Sunday. Hence a "shift to the right" is need. Add above line after reading a monthStartDay
.
You need then to add a loop, which will print all spaces you need, and will shift above mentioned position to the desired startDayPostion
:
int p = 0;
for (; p < startDayPostion; ++p) {
cout << "" << setw(6);
}
(Insert this before your for
loop with x
)
Now, when you have a shift, you can simply fill the rest of the cells, keeping in mind that you are before the end (Sat
).
After
cout << x << setw(6);
keep shifting the help index:
++p;
and then, if you are done with the line, go to the new line and reset p:
if (p > 6) {
cout << '\n';
p = 0;
}
I dont know why do you put here a for (int i=1; i == 6; i++)
loop... You can simply delete those lines of code.
回答2:
The inner loop you have checks if i == 6, and that will never happen. Maybe why you feel stuck?
There are two problems to solve here: How to put the new line in the correct place, and how to pad until the first day.
I will leave the first problem for you.
It's hard to give concrete help on things like this without giving the answer. Let me start with. If I say the first day is the fifth day, and there are 30 days, then you are going to print out 35 things. The first five are empty space and then the rest are the numbers of the day.
回答3:
I think, this is something you are looking for:
#include <iostream>
#include <iomanip>
#include <conio.h>
using namespace std;
int main()
{
int monthStartDay, daysInMonth;
cout << "Enter the first day of the month: ";
cin >> monthStartDay;
cout << "Enter how many days are in the month: ";
cin >> daysInMonth;
cout<<"\nSun Mon Tue Wed Thu Fri Sat";
cout<<"\n\n"<<setw(2);
int offset = monthStartDay; // offset for the first date
for (int i = 0; i < offset; ++i)
cout << "" << setw(6); // output blank space
for (int x=1 ; x <= daysInMonth; x++)
{
cout << x << setw(6);
if ((x+offset)%7 == 0) // after each 7th output we have to
cout << "\n "; // make a new line
}
return 0;
}
Also I assume, that in case of your calendar header it will be Sunday=0, Monday=1,...
回答4:
Here is something that I whipped up - hope it helps!
#include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int i, days, startday;
cout << "Please enter the number of days in the month:" << endl;
cin >> days;
cout << "Please enter the starting date of the month (1 = Monday, 7 = Sunday):" << endl;
cin >> startday;
for (i = 1; i < startday; i++)
{
cout << " ";
}
for (i = 1; i <= days; i++)
{
cout << setw(3) << i;
if ((startday + i - 1) % 7 == 0)
{
cout << "\n";
}
}
cout << endl;
system("pause");
return 0;
}
Let me know if clarification is needed.
回答5:
This is my project (include Leaf Year). You can reference for this. Rules 1.Have to use En month. 2.Have to use 1 -> 01~ 9-> 09 3.Have to use "------------------" when down the line. 4.Have to use "Input value" out to "Output Value.
#include <iostream>
#include <ostream>
#include <iomanip>
using namespace std;
void leafCalendar(int inputYear, int inputMonth);
bool leafYear(int inputYear);
int startToYear(int inputYear);
int startToMonth(int inputYear, int inputMonth);
int finishToDay[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
char display_year[5];
char display_month[3];
int main()
{
int inputYear, inputMonth, i;
char response;
char dayofWeek[7][5] = { {"Sun"},{"Mon"},{"Tue"},{"Wed"},{"Thu"},{"Fri"},{"Sat"} };
do
{
cout << "enter the year and month. (exam: 2003 5) " << endl;
cin >> inputYear >> inputMonth;
if (leafYear(inputYear))
finishToDay[1] = 29;
else finishToDay[1] = 28;
cout << ("Input Month ");
cout << (display_year, "$4d", inputYear);
cout << ("Year");
cout << (" ");
cout << (display_month, "$2d", inputMonth);
cout << ("Month");
cout << ("is....") << endl;
cout << ("\t< ");
cout << (display_month, "$4d", inputYear);
cout << (" ");
if (inputMonth == 1) {
cout << "January";
};
if (inputMonth == 2) {
cout << "February";
};
if (inputMonth == 3) {
cout << "March";
};
if (inputMonth == 4) {
cout << "April";
};
if (inputMonth == 5) {
cout << "May";
};
if (inputMonth == 6) {
cout << "June";
};
if (inputMonth == 7) {
cout << "July";
};
if (inputMonth == 8) {
cout << "August";
};
if (inputMonth == 9) {
cout << "September";
};
if (inputMonth == 10) {
cout << "October";
};
if (inputMonth == 11) {
cout << "November";
};
if (inputMonth == 12) {
cout << "December";
};
cout << (" >");
cout << "\n============================\n";
if (inputMonth >= 1 && inputMonth <= 12)
{
for (i = 0; i < 7; i++)
cout << " " << dayofWeek[i];
cout << "\n----------------------------";
}
cout << endl;
leafCalendar(inputYear, inputMonth);
cout << ("\n----------------------------\n");
cout << "\n============================\n" << endl;
cout << "Repeat?(Y/N): ";
cin >> response;
} while (response != 'N' || response != 'n');
return 0;
}
void leafCalendar(int inputYear, int inputMonth)
{
int StartToDay, LineBreak;
int TermToLine = (startToYear(inputYear) + startToMonth(inputYear, inputMonth)) % 7;
LineBreak = TermToLine;
for (StartToDay = 0; StartToDay < TermToLine; StartToDay++)
cout << " ";
for (StartToDay = 1; StartToDay <= finishToDay[inputMonth - 1]; StartToDay++)
{
std::cout << " " << std::setw(2) << std::setfill('0') << StartToDay;
if (LineBreak == 6) {
cout << "\n----------------------------" <<endl;
LineBreak = 0;
}
else
LineBreak++;
}
}
bool leafYear(int b)
{
if ((b % 4 == 0 && !(b % 100 == 0)) || (b % 400 == 0))
return true;
else {
return false;
}
}
int startToMonth(int inputYear, int inputMonth)
{
int CheckToLeaf = 0;
for (int i = 1; i < inputMonth; i++)
CheckToLeaf += finishToDay[i - 1] % 7;
if (inputMonth > 2 && leafYear(inputYear))
CheckToLeaf++;
return CheckToLeaf % 7;
}
int startToYear(int inputYear)
{
int CheckToLeaf = 4;
for (int a = 1980; a >= inputYear; a--)
{
CheckToLeaf += 6;
if (leafYear(a))
CheckToLeaf += 6;
} CheckToLeaf %= 7;
return CheckToLeaf;
}
来源:https://stackoverflow.com/questions/19884707/c-calendar-problems