尝试替换字符串数组的元素,但只能在该位置输入新字符串。 C++

Trying to replace an element of a string array but only able to input a new string in that position. C++

您好,我正在尝试创建一个非常简化的库存系统,但遇到了一个小问题。当尝试更改我的字符串数组中的元素时,我将一个新的字符串值放入现有数组并将其他所有内容向前推。

// ConsoleApplication2.cpp : Defines the entry point for the console application.

#include "stdafx.h"
#include <iostream>
#include <string>

int main()
{
    const int maxItems = 10; //Maximum number of items inventory can hold
    int numbItems = 0; //number of current items

    std::string inventory[maxItems]; //inventory

    //Items in inventory
    inventory[++numbItems] = "Sword";
    inventory[++numbItems] = "Cloak";
    inventory[++numbItems] = "Boots";

    //Show player items in inventory
    for (int i = 0; i <= numbItems; ++i)
    {
        std::cout << inventory[i] << "\n";
    }


    inventory[0] = "Axe"; //Replace sword with axe

    //Show player items in inventory
    for (int i = 0; i <= numbItems; ++i)
    {
        std::cout << inventory[i] << "\n";
    }


    //keep window open
    std::string barn;
    std::cin >> barn;

    return 0;
}

这段代码输出; "axe, sword, cloak and boots" 当期望的结果是 "axe, cloak and boots" 时。

提前谢谢你。

据我从您的代码中可以看出,sword 的 numbItems 是 1(您使用预增量)。可能使用 post-increment 会解决你的问题。
尝试使用下面的代码 (live IdeOne code):

// ConsoleApplication2.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include <iostream>

#include <string>

int main()
{
    const int maxItems = 10; //Maximum number of items inventory can hold
    int numbItems = 0; //number of current items

    std::string inventory[maxItems]; //inventory

    //Items in inventory
    inventory[numbItems++] = "Sword";
    inventory[numbItems++] = "Cloak";
    inventory[numbItems++] = "Boots";

    //Show player items in inventory
    for (int i = 0; i <= numbItems; ++i)
    {
        std::cout << inventory[i] << "\n";
    }


    inventory[0] = "Axe"; //Replace sword with axe

    //Show player items in inventory
    for (int i = 0; i <= numbItems; ++i)
    {
        std::cout << inventory[i] << "\n";
    }


    //keep window open
    std::string barn;
    std::cin >> barn;

    return 0;
}

在我的电脑上,这有效(输出是 Sword,Cloak,Boots and Axe,Cloak,Boots)。

您使用预增量运算符来填充您的数组。

inventory[++numbItems] = "Sword";

由于 numbItems 从 0 开始,因此您在 1 处插入第一个元素。

只需使用 post 增量,就可以正常工作。

将预自增运算符更改为post-自增运算符,您将获得想要的结果。这是代码,

#include "stdafx.h"
#include <iostream>
#include <string>

int main()
{
    const int maxItems = 10; //Maximum number of items inventory can hold
    int numbItems = 0; //number of current items

std::string inventory[maxItems]; //inventory

//Items in inventory
inventory[numbItems++] = "Sword";
inventory[numbItems++] = "Cloak";
inventory[numbItems++] = "Boots";

//Show player items in inventory
for (int i = 0; i < numbItems; ++i)
{
    std::cout << inventory[i] << "\n";
}


inventory[0] = "Axe"; //Replace sword with axe

//Show player items in inventory
for (int i = 0; i < numbItems; ++i)
{
    std::cout << inventory[i] << "\n";
}

return 0;

}

另请注意,由于您的数组从第 0 个索引开始,因此在 for 循环中它应该是 i < numbItems 而不是 i <= numbItems