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

Time:10-20

I want to save every move 'char' to an array, and then call back all array to see 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.

CodePudding user response:

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.

  • Related