我是静态类型 C++ 的新手。在 JavaScript 中,我可以先检查数据类型,但这似乎非常复杂,而且答案似乎都暗示您没有“了解”该语言。
这是我用来测试 rand() 的代码,在那里我遇到了将字符串转换为整数的问题:
int main(){
std::string input;
cout <<endl<< "What to do?"<<endl;
cin >> input;
if (input == "rand")
{
cout << "what is the max?" << endl;
cin >> input;
int number;
if (stoi(input) > 1) {
number = stoi(input);
}
else {
number = 10;
cout << "using 10"<<endl;
}
cout << rand() % stoi(input);
return main();
}
}
Run Code Online (Sandbox Code Playgroud)
所以在 Javascript 中,我只会检查输入或结果的类型,但是人们在 C++ 中做什么?
不允许在评论中说谢谢,所以我在这里说谢谢!