我只是想知道是否有人注意到我的代碼塊有問題。這個程序應該是一個比較兩個日期的測試程序。如果調用日期較大,則im正在處理的函數應該返回1,如果調用日期小於該函數,則爲-1,如果調用日期與參數中的日期相等,則返回0。我的測試程序:似乎無法從我的代碼塊得到正確的輸出
#include <cstdlib>
#include <iostream>
#include <string>
#include "date.h"
using namespace std;
//date is initialized in a month/day/year format.
int main(int argc, char* argv[])
{
string* d;
date d1(4,1,4);
date d4(4,4,4);
int greaterTest = d4.compareTo(d1);
int lessTest = d1.compareTo(d4);
cout << greaterTest << endl; //i believe these two lines are printing out a
cout << lessTest << endl; //location in memory
cout<<&d <<endl;
system("pause");
return EXIT_SUCCESS;
}
巨大的compareTo()函數:
int date::compareTo (date another_date)
{
if (this->year == another_date.year && this->month == month && this->day < another_date.day) //if both year and month are the same, test to see if day is less
{
return -1;
}
else if (this->year == another_date.year && this->month == month && this->day > another_date.day) //if both year and month are the same, test to see if day is greater
{
return 1;
}
else if (this->year == another_date.year && this->month > month) //if the years are the same, test to see if the invoking month is greater
{
return 1;
}
else if (this->year == another_date.year && this->month < month) //if the years are the same, test to see if the invoking month is less
{
return -1;
}
else if (this->year > another_date.year) //test to see if the invoking year is greater
{
return 1;
}
else if (this->year < another_date.year) //test to see if the invoking year is less
{
return -1;
}
else if(this-> year == another_date.year && this-> month == another_date.month //test if the dates are exactly the same
&& this-> day == another_date.day)
{
return 0;
}
//else{ return 15;} //if none are true, return 15
}
唯一的問題即時得到是,當我試圖改變天(日期第二個參數)。
你能編輯你的問題,包括一些不能正確比較的日期嗎? –
爲什麼在某些條件下不使用another_date.month?在應付StackOverflow時出錯?否則它是錯誤的。 – Aslan986