【问题标题】:How can i save every move that was made "a, w, s, or d" to an array?如何将“a、w、s 或 d”的每一个动作保存到数组中?
【发布时间】:2021-12-06 21:07:43
【问题描述】:

我想把每一个移动'char'保存到一个数组中,然后回调所有数组来查看历史

cout << "===    Chose    ===" << endl;
cout << "Choose were to GO" << endl;
cout << "a, w, s, or d" << endl;
cout << endl;
cin >> Сhoice_1;

if (tolower(Сhoice_1) == 'a')
{
    cout << "You made a step to the left" << endl;
    value1 = -1;
    if (value1 < 0) break;
}
if (tolower(Сhoice_1) == 's')
{
    cout << "You made a step back" << endl;
    value1 = -1;
    if (value1 < 0) break;
}
if (tolower(Сhoice_1) == 'w')
{
    cout << "You made a step foward" << endl;
    value1 = -1;
    if (value1 < 0) break;
}
if (tolower(Сhoice_1) == 'd')
{
    cout << "You made a step to the right" << endl;
    value1 = -1;
    if (value1 < 0) break;
}
if (LifeOptionMain <= 0)
{
    value1 = -1;
    if (value1 < 0) break;
}

我试过for循环

for (int i = 0; i < 10; i++)
{
    move[i + 1];
    move[i] = Сhoice_1;
}

但所有元素都只有一个符号,最后一个输入符号,如果最后一个是a,那么输出将是 一种 一种 一种 …… 我究竟做错了什么???请帮忙。

【问题讨论】:

  • 考虑到您将value1 设置为-1 之后立即检查value 是否小于0 似乎有点不必要。你可以break

标签: c++ arrays char


【解决方案1】:

您可能会在数组长度方面遇到问题,但这可行。

char moveArray[1024];
int moveArrayIndex = 0;

...

cin >> Сhoice_1;
moveArray[moveArrayIndex++] = Choice_1;
moveArray[moveArrayIndex] = 0;  // This makes it a printable string.

...

这样做的问题是,如果移动的次数多于您腾出的空间,您就会从阵列的末端流走。不同的数据结构会更安全。

std::vector<char> moveArray;

cin >> Choice_1;
moveArray.push_back(Choice_1);

但是,这可能是在使用您尚未准备好的技术。

【讨论】:

    【解决方案2】:

    非常感谢我找到了解决方案,如果有人需要解决类似问题的方法,我会留下代码。我提前道歉,因为这只是一个学习任务。

    功能#1

    do
                {
                    cout << "=== Make a move ===" << endl;
                    cout << "===    Chose    ===" << endl;
                    cout << "Choose were to GO" << endl;
                    cout << "a, w, s, or d" << endl;
                    cout << endl;
                    cin >> Сhoice_1;
    
                    if (tolower(Сhoice_1) == 'a')
                    {
                        cout << "You made a step to the left" << endl;
                        value1 = -1;
                        move.push_back(Сhoice_1);
                        if (value1 < 0) break;
                    }
                    if (tolower(Сhoice_1) == 's')
                    {
                        cout << "You made a step back" << endl;
                        value1 = -1;
                        move.push_back(Сhoice_1);
                        if (value1 < 0) break;
                    }
                    if (tolower(Сhoice_1) == 'w')
                    {
                        cout << "You made a step foward" << endl;
                        value1 = -1;
                        move.push_back(Сhoice_1);
                        if (value1 < 0) break;
                    }
                    if (tolower(Сhoice_1) == 'd')
                    {
                        cout << "You made a step to the right" << endl;
                        value1 = -1;
                        move.push_back(Сhoice_1);
                        if (value1 < 0) break;
                    }
                    if (LifeOptionMain <= 0)
                    {
                        value1 = -1;
                        if (value1 < 0) break;
                    }
                    return Сhoice_1;
    

    功能#2

    void Move_History()
        {
            for (char i : move)
            {
                cout << "You made a move - : " << i << ":";
                cout << endl;
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-14
      • 2015-10-15
      • 2013-09-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多