函数,从问题中获取输入,并在主函数中使用该输入

Function that takes the input from a question, and uses that input in the main function

本文关键字:函数 输入 获取 问题      更新时间:2023-10-16

我目前正在编写一个程序来模拟溜槽和梯子。对于用户来说,出现在控制台框中的第一件事就是询问玩家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">是规则。一个简单的整数在成功时返回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

或者,如果用户使用Ctrl+d(或窗口上的Ctrl+z(生成手动EOF

$ ./bin/chutes
enter player 1: (stream error or user canceled input)

有很多方法可以将这些部分组合在一起,但请始终记住验证、验证、验证每个用户输入。如果您还有其他问题,请告诉我。