如何将具有功能的日历程序输出到.txt文件
How to output a calendar program with functions to a .txt file
我遇到的问题是弄清楚我的程序有什么问题以及为什么它不会写入我指定的输出文件。
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <fstream>
using namespace std;
int numOfDaysInAMonth(int, int);
void printMonth(int);
void printDaysofMonth(int, int&);
void skipToDay(int);
int main()
{
ofstream ofile;
ofile.open("calendar.txt");
int year, firstDayInCurrentMonth;
int currentMonth = 1;
int numDays;
cout << "What year is this? ";
cin >> year;
cout << endl;
cout << " 0 - Sundayn";
cout << " 1 - Mondayn";
cout << " 2 - Tuesdayn";
cout << " 3 - Wednesdayn";
cout << " 4 - Thursdayn";
cout << " 5 - Fridayn";
cout << " 6 - Saturdayn";
cout << "What day of then week is Jan 1? ";
cin >> firstDayInCurrentMonth;
ofile << year << endl;
while (currentMonth <= 12) {
numDays = numOfDaysInAMonth(currentMonth, year);
printMonth(currentMonth);
printDaysofMonth(numDays, firstDayInCurrentMonth);
ofile << endl << endl << endl;
currentMonth = currentMonth + 1;
}
ofile << endl;
system("pause");
return 0;
}
我使用ile来输出文本,但没有正确返回。
// This function returns the number of days in a month
int numOfDaysInAMonth(int m, int y)
{
if (m == 1)
return(31);
else if (m == 2 && y % 4 != 0)
return(28);
if (m == 2 && y % 4 == 0)
return(29);
else if (m == 3)
return(31);
else if (m == 4)
return(30);
else if (m == 5)
return(31);
else if (m == 6)
return(30);
else if (m == 7)
return(31);
else if (m == 8)
return(31);
else if (m == 9)
return(30);
else if (m == 10)
return(31);
else if (m == 11)
return(30);
else if (m == 12)
return(31);
}
// It takes the number of the month and prints outs the name of the month and the frame of the calander
void printMonth(int m)
{
ofstream ofile;
ofile.open("calendar.txt");
if (m == 1)
{
ofile << "January" << endl;
}
else if (m == 2)
{
ofile << "February" << endl;
}
else if (m == 3)
{
ofile << "March" << endl;
}
else if (m == 4)
{
ofile << "April" << endl;
}
else if (m == 5)
{
ofile << "May" << endl;
}
else if (m == 6)
{
ofile << "June" << endl;
}
else if (m == 7)
{
ofile << "July" << endl;
}
else if (m == 8)
{
ofile << "August" << endl;
}
else if (m == 9)
{
ofile << "September" << endl;
}
else if (m == 10)
{
ofile << "October" << endl;
}
else if (m == 11)
{
ofile << "November" << endl;
}
else if (m == 12)
{
ofile << "December" << endl;
}
ofile << " S M T W T F S" << endl;
ofile << "_____________________" << endl;
}
// Helps with the skipToDay function
void skip(int i)
{
ofstream ofile;
ofile.open("calendar.txt");
while (i > 0)
{
ofile << " ";
i = i - 1;
}
}
// This function prints out the days in the month after the header for each month
void printDaysofMonth(int numDays, int &weekDay)
{
ofstream ofile;
ofile.open("calendar.txt");
int day = 1;
skipToDay(weekDay);
while (day <= numDays)
{
ofile << setw(2) << day << " ";
if (weekDay == 6)
{
ofile << endl;
weekDay = 0;
}
else weekDay = weekDay + 1;
day = day + 1;
}
}
// Prints spaces in monthly calander
void skipToDay(int d)
{
return skip(3 * d);
}
void printDaysofMonth(int numDays, int &weekDay)
{
ofstream ofile;
ofile.open("calendar.txt");
...
}
int main()
{
ofstream ofile;
ofile.open("calendar.txt");
printDaysofMonth(...);
ofile << "data...";
...
}
文件已经打开。第一个流将写第二个流的写入(如果有的话)。通过参考将功能更改为传递ofstream
:
void printDaysofMonth(ofstream &ofile, int numDays, int &weekDay)
{
...
}
int main()
{
ofstream ofile;
ofile.open("calendar.txt");
printDaysofMonth(ofile, ...);
...
}
您可以通过将数据保存在数组中,以使程序较短。较少的键入意味着较小的潜在错误。
int is_leap_year(int y)
{
return (y % 4) == 0 && y % 100 != 0 || y % 400 == 0;
}
int numOfDaysInAMonth(int m, int y)
{
int days = 0;
if(m >= 1 && m <= 12)
{
const int days_in_month[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int days = days_in_month[m - 1];
if(m == 2 && is_leap_year(y))
days++;
}
return days;
}
const char* get_month_name(int m)
{
const char *month_name[] = {
"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December" };
if(m >= 1 && m <= 12)
return month_name[m - 1];
return "month name error";
}
void printMonth(ofstream &ofile, int m)
{
ofile << get_month_name(m) << "n";
ofile << " S M T W T F S" << endl;
ofile << "_____________________" << endl;
}
相关文章:
- 如何将c++程序的一些输出传递给shell,以便在shell中使用
- 使用C++程序合并排序没有得到正确的输出
- 显示错误输出的简单数组排序程序
- 更改.cpp程序的输入文件中数据的位置会意外更改输出
- 如何从Windows应用程序输出到标准?
- 我写了一个C++程序来模拟Enigma机器.我没有得到输出
- 如何使用C++读取另一个程序中的源代码输出
- 为什么我的C++程序的程序集输出充满了 .ascii,没有汇编代码?
- 为什么这个程序的输出不如预期
- 在由Sublime文本3编译后在cmd上显示Java程序输出
- C++ 每次运行程序时我都会"nan"输出的问题
- 如何从 Win32 C++ 应用程序输出到父控制台窗口?
- 这是使用回溯的 nqueen 问题,但我使用了动态 2d 数组,我的程序编译良好,但不返回任何输出
- 而循环:简单的除法程序输出零,不明白为什么
- 如何在 html 页面中插入 cgi 应用程序的输出?
- C 中的输出程序无明显的原因是白色的
- C++:更改简单输出程序的字体
- C++ 简单的输入/输出程序 + 枚举器
- C++字母等效数字代码输出程序
- 编译输出程序集