Home > Enterprise >  C how to put items in an array that is in a class
C how to put items in an array that is in a class

Time:10-10

So I have a program that has a class that represents a player (also called player). The player needs to have a name, password, amount of experience, a position, and an inventory of four items. The program needs to create three (hardcoded) players each with a name, password, experience amount, position, and an inventory of four items. I have it mostly done except one thing, the inventory array. I have it partially set up with a setInv and getInv to both set and get the inventory, but I'm uncertain on how to use getInv to put items into the inventory so I can use getInv to display it. I tried putting an already filled array in the class but the display outputs none of the items. I also tried playerOne("a"); and playerOne.setInv() = "a"; to but both fail. With the first giving me the error of "too many arguments in function call".

I'm also getting the errors The second one give me two errors of "Index '4' is out of valid index range '0' to '3' for possibly stack allocated buffer 'inv'." and "Reading invalid data from 'inv': the readable size is '112' bytes, but '140' bytes may be read."

I'm very new to C and would appreciate any help, thank you!

#pragma warning(disable: 4996) 
#include<string> 
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;

//player class
class player {
public:
    //name
    void setName(string name) { 
        this->name = name; 
    } //setName end
    string getName() {
        return name;
    } //getName end

    //password
    void setPass(string pass) {
        this->pass = pass;
    } //setPass end
    string getPass() {
        return pass;
    } //getPass end

    //experience
    void setXP(int xp) {
        this->xp = xp;
    } //setXP end
    int getXP() {
        return xp;
    } //getXP end

    //position
    void setPosX(int xPos) {
        this->xPos = xPos;
    } //setPosX end
    void setPosY(int yPos) {
        this->yPos = yPos;
    } //setPosY end
    int getPosX() {
        return xPos;
    } //getPosX end
    int getPosY() {
        return yPos;
    } //getPosY end
    
    //inventory
    string setInv() {
        string inv[4];
        this->inv = inv[4];
    } //setInv end
    string getInv() {
        return inv;
    } //getInv end

    void display();

private:
    string name;
    string pass;
    string inv;
    int xp = 0;
    int xPos = 0;
    int yPos = 0;
}; //class end

void player::display() {
    //playerOne output
    cout << "Player Info - \n";
    cout << "Name: " << getName() << "\n";
    cout << "Password: " << getPass() << "\n";
    cout << "Experience: " << getXP() << "\n";
    cout << "Position: " << getPosX() << ", " << getPosY() << "\n";
    cout << "Inventory: ";
    for (int i = 0; i < 4; i  ) {
        getInv();
        cout << "\n\n";
    } //for end
}
int main()
{
    //playerOne
    player playerOne;
    playerOne.setName("Porcupixel");
    playerOne.setPass("PokeHerFace2008");
    playerOne.setXP(1477);
    playerOne.setPosX(16884);
    playerOne.setPosY(10950);

    //playerTwo
    player playerTwo;
    playerTwo.setName("Commandroid");
    playerTwo.setPass("RodgerRodger00110001");
    playerTwo.setXP(73721);
    playerTwo.setPosX(6620);
    playerTwo.setPosY(36783);


    //playerThree
    player playerThree;
    playerThree.setName("BumbleBeast");
    playerThree.setPass("AutoBotsRule7");
    playerThree.setXP(20641);
    playerThree.setPosX(15128);
    playerThree.setPosY(46976);

    playerOne.display();
    playerTwo.display();
    playerThree.display();

    return 0;
} //main end

CodePudding user response:

All right, first for your inventory set up, you could do it in a bunch of diverse ways, to begin within your 'setInv' function you are not receiving parameters which is weird since what are you trying to initialize your inventory with? You could initialize all values passing in an array of strings if that makes sense, also from what I gather from your getInv function it looks like you're trying to store the contents of your array of strings in your private var 'inv' however you might be failing to do this since it is only a string and not an array of string, meaning it can only store ONE string or object.

Answering your question more specifically, your 'getInv' is not returning anything because you are not storing anything into your string to begin with: Just to explain a little bit more.

string setInv() {
    string inv[4];
    this->inv = inv[4];
} //setInv end

In this code, you are declaring a new array of strings of size 4, then saying that inv is equal to inv[4] which is never initialized therefore you're not storing anything, anyway as I explained earlier it doesn't look like the right way to do what you're trying to!

  • Related