如何将变量从字符串函数获取到 main

how to get a variable from a string function to main

提问人:underloaded_operator 提问时间:10/25/2022 最后编辑:underloaded_operator 更新时间:10/27/2022 访问量:54

问:

#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <unistd.h>

using namespace std;

void welcome();
void goodbye();
void CheckLadders();
void CheckChutes();
int spin();
string orderPlay();


const char YES = 'Y';
const int MAX = 6;
const int MIN = 1;
int currPos1, currPos2, dice, newPos1, newPos2;
const int END = 100;
string playerName1, playerName2;


int main()
{
    srand(time(0)); // srand(time(NULL));
    newPos1 = 0;
    newPos2 = 0;
    currPos2 = 0;
    currPos1 = 0;
    char choice;
    //dice = 0;
    //initialize all the variables as zero (reset)
    do{
        //while (newLocation < 100 && currentLocation >= 99)
        orderPlay();
        cout << "Press enter to spin...";
        cin.ignore();
        cout << "You threw " << dice << ".";
        spin();

        cout << playerName1 << "Your current location is: " << currPos1 << endl;
        cout << playerName2 << "Your current location is: " << currPos2 << endl;

        if (playerTurn == "p1" || playerTurn == "P2"){ //call plyerTurn from orderPlay...dont knw if its correct
            newPos1 = currPos1 + dice;
            cout << playerName1 << "Your new location is: " << newPos1 << endl; 
        }else{
            newPos2 = currPos2 + dice;    
            cout << playerName2 << "Your new location is: " << newPos2 << endl;
        }


//////////////////////////////////////////////////////////////////////////////////////
        cout << "Do you want to play again? (y/n) ";
        cin >> choice;
        cout << endl;
    } while (choice == YES);

    if(choice != YES)
        goodbye();

    return 0;
}

void welcome(){

    cout << "Welcome to the chutes and ladders game. Both " << endl;
    cout << "players start at 0, and the first one to 100 " << endl;
    cout << "wins the game. However, if you land on a chute," << endl;
    cout << "your player will move down, but a ladder " << endl;
    cout << "will move you up." << endl;
}
void goodbye(){

    cout << endl
         << endl;
    cout << "Thank you for playing the game, I hope you enjoyed it!";
    cout << endl
         << endl;
}
int spin(){

    dice = rand() % ((MAX - MIN) + 1) + MIN;

return dice;
}
string orderPlay(){ // should only be for player turn without names i think
  string playerTurn;
  cout << "Please initiate which player is going first (P1/P2): ";
  cin >> playerTurn;

  if (playerTurn == "P1" || playerTurn == "p1"){
    cout << "Player 1 enter your name: ";
    getline(cin, playerName1);
    cout << endl;
    cout << playerName1 << "'s turn" << endl;
    cout << endl;
  }else{
    cout << "Player 2 enter your name: ";
    getline(cin, playerName2);
    cout << endl;
    cout << playerName2 << "'s turn" << endl;
    cout << endl;
  }

return playerTurn;
}

void CheckChutes (int NewPos)
{
    //checks for chutes

    if (NewPos == 98)
        NewPos = 78;
    else if (NewPos == 95)
        NewPos = 75;
    else if (NewPos == 93)
        NewPos = 70;
    else if (NewPos == 87)
        NewPos = 24;
    else if (NewPos == 64)
        NewPos = 60;
    else if (NewPos == 62)
        NewPos = 19;
    else if (NewPos == 56)
        NewPos = 53;
    else if (NewPos == 49)
        NewPos = 11;
    else if (NewPos == 48)
        NewPos = 26;
    else if (NewPos == 16)
        NewPos = 6;
    else
        NewPos = NewPos;
    cout << "You landed on a chute, and have to move down" << endl;
}

void CheckLadders (int NewPos)
{
    //checks for ladders

    if (NewPos == 1)
        NewPos = 38;
    else if (NewPos == 4)
        NewPos = 14;
    else if (NewPos == 9)
        NewPos = 21;
    else if (NewPos == 23)
        NewPos = 44;
    else if (NewPos == 28)
        NewPos = 84;
    else if (NewPos == 36)
        NewPos = 44;
    else if (NewPos == 51)
        NewPos = 66;
    else if (NewPos == 71)
        NewPos = 90;
    else if (NewPos == 80)
        NewPos = 100;
    else
        NewPos = NewPos;
    cout << "You landed on a ladder, and get to move up the board!" << endl;
}

我对C++和编程相当陌生,我正在做我的任务滑道和梯子,我遇到了一个问题。

我不知道如何在 main 函数中使用 orderPlay 函数中的 playerTurn 变量,请友善,我知道这听起来可能很愚蠢,非常感谢所有帮助。

请忽略评论,其中一些是愚蠢的。

C++ 字符串 函数

评论

0赞 kotatsuyaki 10/25/2022
该函数返回字符串,因此要在 main 函数中使用它,请将其存储到类似 的变量中。orderPlayplayerTurnconst string playerTurn = orderPlay();
1赞 kotatsuyaki 10/25/2022
虽然与您的主要问题没有严格关系,但您的代码中还有其他几个问题。1)为什么“使用命名空间std”被认为是不好的做法? 2) 不是 C++ 标准库的一部分,并且由于它未在代码中的任何位置使用,因此请将其删除。3)使用C++11或更高版本,有一种更现代的方法来生成随机数。请参阅使用 C++11 随机库生成随机数unistd.h

答:

0赞 georgep 10/27/2022 #1

该函数返回字符串,因此要在 main 函数中使用它,请将其存储到如下所示的变量中:orderPlayplayerTurn

const string playerTurn = orderPlay();

感谢Kotatsuyaki,他在评论中发布了答案