我处理作业是:如何使用iofstream将函数的输出写入文件?
提示用户一年投入不低于1582
生成一个文件(cal.dat)包含的代码我公司生产的日历写。
我写了需要输入,计算它是否是闰年,然后还给当年与cout
日历的代码。
当我尝试输出日历文件的Xcode给了我这个错误在编译:
Invalid operands to binary expression ('ofstream' (aka 'basic_ofstream<char>') and 'void')
代码的一部分是低于:
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
void PrintMonth(int year, bool leap);
ofstream calendar("cal.dat");
int main()
{
// Setting up the parameters for the PrintMonth function
int year=0;
bool leap=false;
// Input for the year
cout << "Enter a 4 digit year: ";
cin >> year;
// Loop for an incorrect entry
while (year<1582)
{
cout << "Year too low, please re-enter: ";
cin >> year;
}
// Calculate if the input year is a leap year or not
if ((year%4==0 && year%100!=0) || year%400==0)
leap=true;
// Output the year and the calendar for the year requested
calendar << setw(15) << year << endl << endl;
calendar << PrintMonth(year, leap);
return 0;
}
您需要将一个流参数添加到'PrintMonth'并使用该参数代替'cout'。 – molbdnilo
'PrintMonth'返回'void' ....当你调用这个函数时,你认为你正在给你的'ofstream'写什么? – CoryKramer
投票重新打开,因为提问者已经给答案者足够的信息以给出一个很好的答案。 –