0

I want to save every move 'char' to an array, and then call back all arrays to see the history

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;
}

I tried for loop

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

but all elements are only one symbol, last input symbol, if last was a then output will be a a a .... what am I doing wrong??? please help.

1
  • Considering you set value1 to -1 immediately afterward checking if value is less than 0 seems a tad unnecessary. You could just break.
    – Chris
    Oct 20, 2021 at 1:52

2 Answers 2

2

You could run into trouble with the length of your array, but this would work.

char moveArray[1024];
int moveArrayIndex = 0;

...

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

...

The problem with this is that you're flow off the end of your array if there are more moves than you made space for. A different data structure would be safer.

std::vector<char> moveArray;

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

However, this is probably using techniques you're not ready for yet.

0
0

I found a solution thanks a lot, if anyone needs a solution to a similar problem, I'll leave the code. I apologize in advance since this is just a study task.

function #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;

function #2

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.