所以這就是我想實現簡單的C++計算星期幾,有點卡住了?
這是我的代碼。我現在完全陷入困境,我在這裏錯過了什麼,那正好在我面前?我覺得我有所有的功能,但我不認爲它會正確計算它。我在這裏有一些錯誤,我無法修復甚至試圖運行它。 謝謝你的任何幫助; -
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
class dayType
{
public:
void setDay(int dayNum);
// set the day with the dayNum as parameter
void printd(dayNum);
// print the dayNum
int returnDay();
// return the day of the week
void dayAfter();
// return next day
void dayBefore();
// return previous day
void randomDay(int dayNum);
// function to return a day after a certain number of days
dayType(int dayNum);
// Constructor with parameters setting dayNum according to parameters
dayType();
//Default constructor
private:
int today;
int yest;
int tom;
int dayN;
};
void dayType::printd(int dayNum)
{
if (dayNum == 1)
cout << "Monday" << endl;
if (dayNum == 2)
cout << "Tuesday" << endl;
if (dayNum == 3)
cout << "Wednesday" << endl;
if (dayNum == 4)
cout << "Thursday" << endl;
if (dayNum == 5)
cout << "Friday" << endl;
if (dayNum == 6)
cout << "Saturday" << endl;
if (dayNum == 7)
cout << "Sunday" << endl;
}
void dayType::setDay(int dayNum)
{
today = dayNum;
};
int dayType::returnDay()
{
return today;
};
void dayType::printd(<#int dayNum#>);
{
cout << "The current day is: " << today << endl;
}
void dayType::dayBefore()
{
if(today == 0)
yest = 6;
else today--;
};
void dayType::dayAfter()
{
if(today == 6)
tom = 0;
};
void dayType::randomDay(int dayNum)
{
dayN=(today+dayNum);
today =(dayN%7);
};
dayType::dayType()
{
today = 0;
}
dayType::dayType(int daynum)
{
today = daynum;
}
// do I need these constructors here doing this?
int main()
{
int dayWeek;
cout << "Please enter a number for the day of the week: " << endl;
cout << "1 - Monday" << endl;
cout << "2 - Tuesday" << endl;
cout << "3 - Wednesday" << endl;
cout << "4 - Thursday" << endl;
cout << "5 - Friday" << endl;
cout << "6 - Saturday" << endl;
cout << "7 - Sunday" << endl;
while (dayWeek<= 7)
cin >> dayWeek;
dayType thisDay;
cout << "Today is: ";
thisDay.returnDay();
thisDay.printd(int dayNum);
cout << "Yesterday was: ";
thisDay.dayBefore();
thisDay.printd(int dayNum);
cout << "Tomorrow is: ";
thisDay.dayAfter();
thisDay.printd(int dayNum);
cout << "Type a number of days from today and it will be: ";
thisDay.randomDay(dayNum);
return 0;
};
}
嗨Croset!我在代碼中看到了一些錯誤。但首先,你的問題是什麼?你有錯誤嗎?行爲不是你所期望的嗎? – Dylan
嗨迪倫,老實說,我覺得我有那裏的功能,但我不認爲它會按照我的計劃工作。不確定我的打印是否正確。我首先收到一些基本的語法錯誤,所以我不能運行程序來檢查 - 真的可以感謝任何幫助,以瞭解我出錯的地方!謝謝:) – Croset
在你的書中閱讀更多有關*返回*函數中的值以及如何調用函數的內容。 (你還沒有完成XCode中'printd'參數的定義。) – molbdnilo