我对C.有点新鲜。我正在制作一整年的日历并将其存储到文件中。我的主要问题是每当我尝试制作一个文件时,它就会出现空白。我一直试图弄清楚这一点,并决定最后问。有人可以告诉我我做错了什么吗?提前谢谢!
#include <stdio.h>
#include <stdbool.h>
#define MIN_YEAR 2000
#define MAX_YEAR 3000
#define JANUARY 1
#define FEBRUARY 2
#define MARCH 3
#define APRIL 4
#define MAY 5
#define JUNE 6
#define JULY 7
#define AUGUST 8
#define SEPTEMBER 9
#define OCTOBER 10
#define NOVEMBER 11
#define DECEMBER 12
bool getContinueChoice();
int getYear ();
int getStartingDay (int year);
bool isLeapYear (int year);
void makeFile (int year, int startingDay);
void makeMonthHeading (int month, int year, FILE *file);
int getNumberOfDays (int month, int year);
int main()
{
int startingDay,
year;
printInstructions();
while(getContinueChoice() == true)
{
year = getYear();
startingDay = getStartingDay(year);
makeFile(year, startingDay);
printf("\nCalendar made for the year %d.\n", year);
printf("Look for the file called file.txt to see it.\n");
}
printGoodbye();
return 0;
}
bool getContinueChoice()
{
char getChoice;
bool choice;
scanf(" %c", &getChoice);
while(getChoice != 'y' || 'n')
{
printf("\n***************************************\n");
printf("* Error: That was not a (y/n) answer. *\n");
printf("***************************************\n\n");
printf("Would you like to print a calendar? (y/n): ");
scanf(" %c", &getChoice);
}
if(getChoice == 'y')
{
choice = true;
}
else
{
choice = false;
}
return choice;
}
int getYear()
{
int userYear;
printf("\nPlease enter a year.\n");
scanf("%d", &userYear);
return userYear;
}
int getStartingDay(int year)
{
int firstDay;
firstDay = (((year - 1) * 365) + ((year - 1) / 4) - ((year - 1) / 100) +
((year - 1) / 400) + 1) % 7;
return firstDay;
}
bool isLeapYear(int year)
{
bool leapYear;
if((!(year % 4) && (year % 100)) || !(year % 400))
{
leapYear = true;
}
return leapYear;
}
void makeFile(int year, int startingDay)
{
int daysInMonth;
FILE *file;
file = fopen("file.txt","w");
for(int month = JANUARY; month <= DECEMBER; month++)
{
makeMonthHeading(month, year, file);
fprintf(file, "SUN MON TUE WED THU FRI SAT\n");
for(int day = 1; day <= 1 + startingDay * 5; day++)
{
printf(" ");
daysInMonth = getNumberOfDays(month, year);
for(int days = 1; days <= daysInMonth; days++)
{
fprintf(file, "%2d", days);
if((days + startingDay) % 7 > 0)
{
printf(" ");
}
else
{
printf("\n");
}
}
}
}
printf("Calendar made for the year %d", year);
printf("\nLook for the file called file.txt to see it.\n\n");
getContinueChoice();
return;
}
void makeMonthHeading(int month, int year, FILE *file)
{
if(month == JANUARY)
{
fprintf(file, "JANUARY %d\n", year);
}
else if(month == FEBRUARY)
{
fprintf(file, "\nFEBRUARY %d\n", year);
}
else if(month == MARCH)
{
fprintf(file, "\nMARCH %d\n", year);
}
else if(month == APRIL)
{
fprintf(file, "\nAPRIL %d\n", year);
}
else if(month == MAY)
{
fprintf(file, "\nMAY %d\n", year);
}
else if(month == JUNE)
{
fprintf(file, "\nJUNE %d\n", year);
}
else if(month == JULY)
{
fprintf(file, "\nJULY %d\n", year);
}
else if(month == AUGUST)
{
fprintf(file, "\nAUGUST %d\n", year);
}
else if(month == SEPTEMBER)
{
fprintf(file, "\nSEPTEMBER %d\n", year);
}
else if(month == OCTOBER)
{
fprintf(file, "\nOCTOBER %d\n", year);
}
else if(month == NOVEMBER)
{
fprintf(file, "\nNOVEMBER %d\n", year);
}
else if(month == DECEMBER)
{
fprintf(file, "\nDECEMBER %d\n", year);
}
return;
}
int getNumberOfDays(int month, int year)
{
int monthDays;
if(month == JANUARY)
{
monthDays = 31;
}
else if(month == FEBRUARY)
{
if(isLeapYear(year) == true)
{
monthDays = 29;
}
else
{
monthDays = 28;
}
}
else if(month == MARCH)
{
monthDays = 31;
}
else if(month == APRIL)
{
monthDays = 30;
}
else if(month == MAY)
{
monthDays = 31;
}
else if(month == JUNE)
{
monthDays = 30;
}
else if(month == JULY)
{
monthDays = 31;
}
else if(month == AUGUST)
{
monthDays = 31;
}
else if(month == SEPTEMBER)
{
monthDays = 30;
}
else if(month == OCTOBER)
{
monthDays = 31;
}
else if(month == NOVEMBER)
{
monthDays = 30;
}
else if(month == DECEMBER)
{
monthDays = 31;
}
return monthDays;
}
答案 0 :(得分:1)
您在同一行上有两个问题:
while(getChoice != 'y' || 'n')
这不符合你的想法。让我们来做一个例子,getChoice = 'a'
第一项操作getChoice != 'y'
将评估为TRUE
,因为'a' != 'y'
。请记住,您正在通过此操作获得TRUE
。
现在,您的TRUE || 'n'
始终为TRUE
因此,您的while条件将始终为TRUE
,这就是您陷入该循环的原因。
如果您使用
解决此问题while((getChoice != 'y') || (getChoice != 'n'))
你将意识到你的第二个问题,那就是你的逻辑。
想象一下,如果用户输入'y'
。然后会发生什么?
(getChoice != 'y')
将为FALSE
,但(getChoice != 'n')
将为TRUE
,因此您的同时条件为TRUE
,您将再次陷入该循环
考虑其他方案,然后您将得出结论,您需要使用&&
而不是||
。
因此,将该行更改为:
while((getChoice != 'y') && (getChoice != 'n'))
另外,正如@purec所说,完成后你需要fclose()
文件。