我怎样才能将 "a, w, s, or d" 的每一步都保存到数组中?

How can i save every move that was made "a, w, s, or d" to an array?

我想把每一步'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 (int i = 0; i < 10; i++)
{
    move[i + 1];
    move[i] = Сhoice_1;
}

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

您可能 运行 遇到数组长度的问题,但这可行。

char moveArray[1024];
int moveArrayIndex = 0;

...

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

...

问题在于,如果移动次数多于 space 的移动次数,您就会从数组的末尾流出。不同的数据结构会更安全。

std::vector<char> moveArray;

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

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

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

函数 #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;
        }
    }