函数,从问题中获取输入,并在主函数中使用该输入
Function that takes the input from a question, and uses that input in the main function
我目前正在编写一个程序来模拟溜槽和梯子。对于用户来说,出现在控制台框中的第一件事就是询问玩家1的名字和玩家2的名字。很明显,我可以做cout<lt;"name 1?"和cin作为变量。然而,在这个任务中,如果我们制作一个函数,接受这个输入并在主函数中使用它,我们将获得额外的积分。换句话说,我应该如何编写一个函数,以便在被调用时,它会带来我们返回的输入?我试了一段时间,但还是想不通。这是我正在尝试的伪代码
string GetPlayerNames(string player1Name, string player2Name);
int main ()
{
string GetPlayerNames(player1Name, player2Name); // Calling the function
// rest of code for the game
}
string GetPlayerNames(string player1Name, string player2Name)
{
cout << "What is the name of player 1? ";
cin >> player1Name;
return player1Name;
cout << "What is the name of player 2? ";
cin >> player2Name;
return player2Name;
}
最理想的情况是,这就是运行程序时输出的样子。
玩家1的名字是什么输入名称
玩家2叫什么名字输入名称
使用两个玩家名称运行游戏
对不起,我是一个非常新的程序员!感谢我在这里能得到的任何帮助。
std::pair<std::string, std::string>
#include <iostream>
#include <string>
#include <utility>
using std::cin;
using std::cout;
using std::pair;
using std::string;
pair<string, string> GetPlayerNames();
int main ()
{
auto [player1Name, player2Name] = GetPlayerNames(); // Calling the function
// rest of code for the game
}
pair<string, string> GetPlayerNames()
{
string player1Name, player2Name;
cout << "What is the name of player 1? ";
cin >> player1Name;
cout << "What is the name of player 2? ";
cin >> player2Name;
return std::make_pair(player1Name, player2Name);
}
您可以使用引用而不是返回名称
#include <iostream>
#include <string>
#include <utility>
using std::cin;
using std::cout;
using std::pair;
using std::string;
void GetPlayerNames(string &player1Name, string &player2Name);
int main ()
{
string player1Name, player2Name;
GetPlayerNames(player1Name, player2Name); // Calling the function
// rest of code for the game
}
void GetPlayerNames(string &player1Name, string &player2Name)
{
cout << "What is the name of player 1? ";
cin >> player1Name;
cout << "What is the name of player 2? ";
cin >> player2Name;
}
通过引用传递它们。轰,你不需要归还任何东西。
void get_name(string & , string &);
int main()
{
string player1 , player2;
cout<<player1<<player2<<endl;
get_name(player1, player2);
cout<<player1<<player2<<endl;
return 0;
}
void get_name(string &player1, string & player2)
{
cout<<"Enter namesn";
cin>>player1>>player2;
}
虽然您可以创建一个为您读取字符串的函数,但您需要提供一种方法来指示函数在实际读取和存储字符串时是成功还是失败。否则,在任何流错误上,或者如果用户使用Ctrl+d(或windows上的 Ctrl+z这并不是读字符串函数特有的,对于完成剩余代码所依赖的任务的每个函数都是如此。"Validate,Validate,Validate">是规则。一个简单的整数在成功时返回 在您的情况下,您关心读取是否成功(这取决于读取后的 它允许您将显示的提示传递给用户,然后根据对 在 (这也允许您使用默认功能来连接 这将处理玩家的输入,包括名称中的空白,同时在任何输入处处理手动生成的 示例使用/输出 或者,如果用户使用Ctrl+d(或窗口上的Ctrl+z(生成手动 有很多方法可以将这些部分组合在一起,但请始终记住验证、验证、验证每个用户输入。如果您还有其他问题,请告诉我。1
或在失败时返回0
是可以的(反之亦然,您可以选择(。CCD_ 5也是用于指示成功/失败的简单类型。std::ios_base::iostate
(。美妙之处在于,您不需要检查.eof(), .fail()
或.bad()
的细节(尽管您可以自由检查(,只需检查输入函数的返回(在处理空白以及任何无关字符时,使用getline
可能比使用>>
更可取(。在这里,你可以简单地做一些类似的事情:#include <iostream>
bool getstring (std::string& str, const std::string prompt)
{
std::cout << prompt; /* output prompt */
/* fill str, return true/false for success/failure */
return getline (std::cin, str) ? true : false;
}
getline
的调用是否成功返回true/false
。main()
中,在依赖字符串s
中的信息(例如(之前,您可以简单地检查getstring
函数是成功还是失败int main (void) {
std::string s,
prompt {"enter "};
for (int i = 0; i < 2; i++) {
if (getstring(s, prompt + "player " + (char)('1' + i) + ": "))
std::cout << s << "nn";
else {
std::cout << "(stream error or user canceled input)n";
return 1;
}
}
}
std::string
以形成提示——只要使用上述方法的玩家不超过9人(EOF
。一个有两个玩家的简短例子是:$ ./bin/chutes
enter player 1: Mickey Mouse
Mickey Mouse
enter player 2: Goofy Dog
Goofy Dog
EOF
:$ ./bin/chutes
enter player 1: (stream error or user canceled input)
- 如果我更改函数输入,我的递归不起作用?
- 初始化与函数输入相关的全局数组
- 如何生成比用户使用 rand() 函数输入的数字大 5-10% 的数字?
- 将函数更改为具有设置函数输入变量的方法的类
- 如何避免将 const 和非 const 成员函数输入到模板中的代码重复
- 是否有一种方法可以避免标头文件中使用的constexpr函数输入全局范围,而无需额外的名称空间
- 迭代器作为C 中的函数输入
- 如何为作为雇员类的指针传入的构造函数输入数据
- 最小的函数输入和输出错误
- C++ 函数输入错误
- QComboBox 作为 QT 中的函数输入参数
- 函数输入之前的分割故障
- 如何在视觉工作室 2012年设置函数输入参数的颜色
- 函数输入变化
- 使用函数输入 1 个 int 和 1 个字符串数组并循环
- 将数组用于函数输入参数
- 如何在c++中将函数输入强制转换为智能指针
- 自定义检查器的覆盖率引用函数输入
- 为什么我不能使用两个scanf函数输入字母
- 犰狳cov函数输入