有人可以向我解释下面的语法我做错了吗?我的教科书没有任何用户定义的字符串函数的例子,我无法弄清楚我应该做什么。提前致谢!
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <string>
string dayOfWeek(int day);
using namespace std;
int main()
{
cout << "Please enter a day of week (0 for Sunday, 1 for Monday, etc): ";
cin >> day;
cout << "The name of the day of the week is: " dayOfWeek(day) << endl;
}
string dayOfWeek(int day)
{
if (day == 0)
return "SUNDAY";
else if (day == 1)
return "MONDAY";
else if (day == 2)
return "TUESDAY";
else if (day == 3)
return "WEDNESDAY";
else if (day == 4)
return "THURSDAY";
else if (day == 5)
return "FRIDAY";
else if (day == 6)
return "SATURDAY";
}
答案 0 :(得分:2)
#include <string>
#include <iostream>
std::string dayOfWeek(int day)
{
if (day == 0)
return "SUNDAY";
else if (day == 1)
return "MONDAY";
else if (day == 2)
return "TUESDAY";
else if (day == 3)
return "WEDNESDAY";
else if (day == 4)
return "THURSDAY";
else if (day == 5)
return "FRIDAY";
else if (day == 6)
return "SATURDAY";
return "Invalid input!"; // May be you should guarantee a return value.
}
int main() {
std::cout << dayOfWeek(5) << std::endl;
}
应该按预期工作。
答案 1 :(得分:2)
问题的关键在于:
string dayOfWeek(int day);
using namespace std;
using
位于依赖于它的代码之后,因此编译器会查找string
并找不到它。它不会去寻找std :: string,因为它尚未被告知。
您可以将using
向上移动几行,但由于Why is "using namespace std" considered bad practice?的答案中涵盖的众多原因以及一些未涵盖的原因,您可能会更好不放
using namespace std;
在您的代码中。明确地加上std::
的前缀,你就可以避免一连串令人讨厌的惊喜。
我还建议关注函数所在的@πάνταῥεῖ示例。向前声明然后声明会给你两个地方来改变代码,另外一个地方有一个bug。声明将要使用它的功能意味着您只需要更改一个声明。
将所有内容与其他一些小调整结合在一起:
#include <string>
#include <iostream>
std::string dayOfWeek(int day)
{
switch (day) // for stuff like this, I like a switch. I find it looks nicer.
{
case 0:
return "SUNDAY";
case 1:
return "MONDAY";
case 2:
return "TUESDAY";
case 3:
return "WEDNESDAY";
case 4:
return "THURSDAY";
case 5:
return "FRIDAY";
case 6:
return "SATURDAY";
default:
return "No such day"; // functions must always return something.
// in this case what will cout print if you
// don't return a string? Don't know?
// Don't feel bad. Neither do I.
// Welcome to Undefined Behaviour
}
}
int main()
{
int day; //this was missing
std::cout << "Please enter a day of week (0 for Sunday, 1 for Monday, etc): ";
std::cin >> day;
std::cout << "The name of the day of the week is: " << dayOfWeek(day) << std::endl;
}
您可以使用另一个技巧来完全摆脱if
或switch
#include <string>
#include <iostream>
// define an array of days of the week to print. const means dayOfWeek cannot be changed
const std::string dayOfWeek[] =
{
"SUNDAY",
"MONDAY",
"TUESDAY",
"WEDNESDAY",
"THURSDAY",
"FRIDAY",
"SATURDAY"
};
int main()
{
unsigned int day;
//Note unsigned int. Negative numbers are now disallowed
std::cout << "Please enter a day of week (0 for Sunday, 1 for Monday, etc): ";
// keep looping until user provides a good number
while (!(std::cin >> day) // user MUST input a number
|| day > 6) // number is a usable number
{
std::cin.clear(); // clean up bad input
std::cout << "Please enter a day of week (0 for Sunday, 1 for Monday, etc): ";
}
// since we know day is a usable (0..6) number we can read the day out of the array
std::cout << "The name of the day of the week is: " << dayOfWeek[day] << std::endl;
}