从数组中更改当前显示的字符串[关闭]

问题描述 投票:-4回答:1

我想通过按下按钮来更改屏幕上显示的当前字符串。例如:

我这里有一个字符串数组:

std::string test[] = { "hey", "how", "are", "you" };

然后我在这里有一些代码来改变数组中当前显示的字符串:

if (GetAsyncKeyState(VK_LEFT))
    //display one string left from the array
else if (GetAsyncKeyState(VK_RIGHT))
    //display string next to the current one in array

std::cout << test;

那么我应该把什么样的代码放到评论的部分?

c++
1个回答
-1
投票

您应该有一个属性或变量,用于保存当前字符串的索引。

int index = 1;

例如。

然后你必须检查索引是否在注释部分中有效。

 if (GetAsyncKeyState(VK_RIGHT))
    if (index == test.size())
    {
        index = test.size() - 1;
    } else {
        index++;
    }

std::cout << test[index];

 if (GetAsyncKeyState(VK_LEFT))
    if (index > 0)
    {
        index--;
    }
std::cout << test[index];

最后,它看起来应该是这样的。

© www.soinside.com 2019 - 2024. All rights reserved.