Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/144.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 更改数组中当前显示的字符串_C++ - Fatal编程技术网

C++ 更改数组中当前显示的字符串

C++ 更改数组中当前显示的字符串,c++,C++,我想通过按按钮更改屏幕上显示的当前字符串。例如: 我这里有一个字符串数组: std::string test[] = { "hey", "how", "are", "you" }; 然后我在这里有一些代码来更改数组中当前显示的字符串: if (GetAsyncKeyState(VK_LEFT)) //display one string left from the array else if (GetAsyncKeyState(VK_RIGHT)) //display stri

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

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

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;

那么我应该把什么样的代码放在注释部分呢?

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

int index = 1;
比如说

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

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

std::cout << test[index];


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

我投票将这个问题作为离题题来结束,因为这不是一个免费的代码编写服务。您正在使用吗?这是否在终端中运行,因此您希望清除屏幕并显示新字符串?如何跟踪当前显示的字符串?这个问题太宽泛了。您可以通过确保std::cout两个片段都是错误的来改进这个答案。sizeoftest返回4*sizeofstd::string,而不是数组的大小,两个ifs都在检查索引的值,然后再对其进行递增/递减。@Yksisarvinen现在代码应该可以了
 if (GetAsyncKeyState(VK_LEFT))
    if (index > 0)
    {
        index--;
    }
std::cout << test[index];