所以这就是我想实现简单的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