我是c ++的新手。有什么更好的方法呢?我试图输出一周的日子,并在日期之间放置功能。我想在星期一输出,而不是输出一周中的其他几天。
#include <iostream>
#include <string>
#include <ctime>
using namespace std;
void day(char* name[]);
int main()
{
char* dayNames[] =
{ "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday",
"Sunday"};
day(dayNames);
day(dayNames + 1);
day(dayNames + 2);
day(dayNames + 3);
day(dayNames + 4);
day(dayNames + 5);
day(dayNames + 6);
system ("pause");
return 0;
}
void day(char* name[])
{
int n = 0;
if (n >=0 && n <= 6)
{
cout << "Today is " << name[n] << "." << endl;
cout << endl;
}
}
答案 0 :(得分:0)
我不太确定你想要完成什么,但是为什么不让day
函数包含数组,而不是用day
函数调用n
函数,而不是通过天数进入功能?这样,在函数中使用void day(int);
int main()
{
day(-1);
day(0);
day(3);
day(7);
}
void day(int n)
{
const char *day_names[] = {
"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"
};
if (n >= 0 && n < 7)
cout << "Today is " << day_names[n] << "." << endl << endl;
}
就有意义了。
{{1}}
答案 1 :(得分:0)
要理解你要做的事情有点难,但是从day()
的多次调用我猜你正在尝试调用一个函数,当传递一个表示索引的数字时在星期几中,打印与该索引相关联的那一天。
这是你想要做的吗?
// monday = day 1, sunday = day 7
void printDay(unsigned day)
{
static char *dayNames[] = { "Monday", "Tuesday", "Wednesday", "Thursday",
"Friday", "Saturday", "Sunday"};
if (day >=1 && day <= 7)
cout << "Today is " << dayNames[n - 1] << "." << endl;
}
printDay(1)
将打印“今天是星期一。”
printDay(7)
将打印“今天是星期天。”