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

我目前正在编写一个程序来模拟溜槽和梯子。对于用户而言,出现在控制台框中的第一件事是询问播放器1的名称和播放器2的名称。显然,我可以执行cout

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的名字是什么? 输入名称

     

使用两个玩家名称运行游戏

抱歉,我是一个非常新的程序员!感谢我能在这里获得的任何帮助。

straybird9 回答:从问题中获取输入并在主函数中使用该输入的函数

一个函数只能返回一个值。您可以返回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 names\n";
    cin>>player1>>player2;
}

,

虽然您可以创建一个可以为您读取字符串的函数,但是您需要提供一种方法来指示在实际读取和存储时函数成功失败字符串。否则,如果出现任何流错误,或者如果用户未能捕获到用户使用 Ctrl + d (或Windows上的 Ctrl + z )生成手册EOF的情况,这样,坏事就会发生。

这并非特定于此读取字符串函数,对于完成其余代码所依赖的某些任务的每个函数,都是如此。 “验证,验证,确认” 是规则。一个简单的整数,如果成功则返回1,如果失败则返回0(反之亦然,您可以选择)。 bool还是指示成功/失败的简单类型。

对于您而言,您要关心读取是否成功(取决于读取后的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;
}

这允许您将显示的提示传递给用户,然后根据对true/false的调用是否成功来返回getline

main()中,您可以简单地检查getstring函数是成功还是失败,然后再依赖字符串s中的信息,例如

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 << "\n\n";
        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 (或Windows上的 Ctrl + z )生成手册EOF

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

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

本文链接:https://www.f2er.com/3134212.html

大家都在问