1

Hello I am attempting to create a very simplified inventory system and I am having a slight issue. When trying to change an element in my string array, I am instead putting a new string value into the existing array and pushing everything else forwards.

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

This code outputs; "axe, sword, cloak and boots" when the desired result is "axe, cloak and boots".

Thank you in advance.

3 Answers 3

3

You use the preincrement-operator to fill your array.

inventory[++numbItems] = "Sword";

As numbItems starts at 0, you insert your first element at 1.

Just use the post-increment, and it will work fine.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! Are there any detailed explanations about pre/post increment that I can read to properly understand it?
Any introductory text for c / c++ / c# should do. The mechanism is the same in all c-derived languages. Personally I prefer the Kernighan Richie when teaching, it's short, concise and well written. en.wikipedia.org/wiki/The_C_Programming_Language
3

As far as I can see from your code, the numbItems for sword is 1 (you use pre-increment). Probably using post-increment will fix your problem.
Try using the code below (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;
}

On my computer, this works (the output is Sword,Cloak,Boots and Axe,Cloak,Boots).

1 Comment

Thank you! Are there any detailed explanations about pre/post increment that I can read to properly understand it?
1

Change the pre-increment operator to post-increment operator and you'll get your desired result. Here's the code,

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

}

Also note that since your array starts with 0th index, it should be i < numbItems in the for loop rather than i <= numbItems

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.