首页 > 解决方案 > 我正在尝试计算世纪之日,但在 unix 中出现此错误:

问题描述

我开始学习如何使用 C++,目前正在尝试计算他们在运行我的程序时提交的日期是世纪中的哪一天。我收到一个错误,但是在查找错误时它说没有任何输出。我在 mac os 的终端应用程序上使用 unix,所以我不确定这是否会影响任何事情。我已经用白板多次浏览了我的程序,但仍然迷路。非常感谢您审查我的问题。

错误:

lab6.cpp: In function 'int day_of_century(int, int, int)':
lab6.cpp:80:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^

代码:

#include <library.h>
//global variables:

int month = 0;
int year = 0;
int day = 0; //this day variable is for getting the day for a date
int days = 0;//this days variable is for counting an amount of days
int count = 1;
//functions:

int length_of_month(int month, int year) {
 if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
  cout << "31 Days" << endl;
 }//31 day if
 if (month == 4 || month == 6 || month == 9 || month == 11) {
  cout << "30 Days" << endl;
 }//30 day if
 if (month == 2) {
  if (year % 400 == 0 && year % 100 == 0) {
   cout << "29 Days" << endl;
  }//29 day if 1
 else  if (year % 4 == 0 && year % 100 == 0) {
   cout << "28 days" << endl;
  } // 28 days 1
 else if (year % 4 == 0) {
   cout << "29 days" << endl;
  } // 29 days 2
  else {
   cout << "28 Days" << endl;
  }//28 day else
 }//february
 return(0);
}

int length_of_month_value(int month, int year) {
 if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
  days = 31;  
 }//31 day if
 if (month == 4 || month == 6 || month == 9 || month == 11) {
  days = 30;
 }//30 day if
 if (month == 2) {
  if (year % 400 == 0 && year % 100 == 0) {
   days = 29;
  }//29 day if 1
 else  if (year % 4 == 0 && year % 100 == 0) {
   days = 28;
  } // 28 days 1
 else if (year % 4 == 0) {
   days = 29;
  } // 29 days 2
  else {
   days = 28;
  }//28 day else
 }//february
 return(days);
}

int day_of_year(int year, int month, int days) {
 if (month == 1) {
  days = day;
 }//if
 else {
  while (count < month) {
  days = days + length_of_month_value(count, year);
  count = count + 1;
  }//while
// days = day + days;
 }//else
 return(days);
}


int day_of_century(int year, int month, int days) {
  if (year == 2000) {
  return(day_of_year(year, month, days));
 }
 else if (year > 0) {
  return(day_of_year(year - 1, 12, 31) + day_of_century(year - 1, month, days));
 }
}


int day_of_century(int year, int month, int days) {

}

//main function:
int main() {
//1: Length of Month:
 cout << "Please enter the month (numerically) with the year (numerically)  and this program will tell you how many days are in that month" << endl;
 cout << "Month: ";
 cin >> month;
 cout << endl << "Year: ";
 cin >> year;
 cout << endl;
 length_of_month(month, year);
 cout << endl;

//2: Day of the Year:
 year = 0;
 month = 0;
 days = 0;
 day = 0;
 cout << "Please enter the year, month, and day, and this program will tell you what day of the year that date is." << endl;
 cout << "Year: ";
 cin >> year;
 cout << endl << "Month: ";
 cin >> month;
 cout << endl << "Day: ";
 cin >> day;
 cout << "You are in day " <<  day_of_year(year, month, day) << " of the year." <<  endl;
// day_of_year(year, month, day);
// cout << days+day;
 cout << endl;

//3: Day of the Century:
 year = 0;
 month = 0;
 days = 0;
 day = 0;
 count = 1;
 cout << "Please enter the year, month, and day, and this program will tell you what day of the century that date is in. This program only stays in this current century so please pick a year between 2000 and 2099." << endl;
 cout << "Year: ";
 cin >> year;
 cout << endl << "Month: ";
 cin >> month;
 cout << endl << "Day: ";
 cin >> day;
 cout << "You are in day " <<  day_of_century(year, month, day) << " of the century." <<  endl;


return(0);
}

标签: c++dateunixterminal

解决方案


我认为您可能想要重新组织您的 day_of_century 功能。例如,如果你有 year != 2000 或 year<=0 那么你就有问题了。要解决该问题,您需要在函数内的所有 if 语句之外返回 int 类型(被认为是默认返回,类似于 switch 案例)


推荐阅读