Function to Print Iterations of a List Not Doing So

I have a simple program that, for now, initializes a list of class objects with variables in the constructor set according to the values entered in each call of the function that updates this list. It then calls another function that iterates through the list and is supposed to print a specific value in that element to the command prompt using std::cout. Unfortunately, what happens instead is that nothing is printed except for a variable used in the cout statement to determine which iteration is printed for that line. For some reason, it is not printing the actual value of that variable though, it is printing what appears to be the numerical value of it's address in memory. For clarification as to what I mean, see the code below:

main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <cstdio>
#include <conio.h>
#include "Shop.h"

void loop(); //program loop
void pseudoPause(); //safe alternative to system("PAUSE");

int main(){

    loop();
    pseudoPause();

    return 0;
}

void pseudoPause(){
    printf("\nPress any key to close the program...\n");
    getch();
}

void loop(){
    //class file objects here:
    Shop shop; //object for the shop

    bool finish = false;
    char endProg;

    while(finish == false){

        //see Shop.h for all comments explaining the functions called by shop
        shop.shopIntro("The Sample Shop");

        shop.shopList("Placeholder item", "a placeholder item", 1, 1);

        shop.printShop();


        endProg = getch(); //getch used to set a value that breaks the loop and returns to main()

        if(endProg == 'y'){ //temporary if statement used to determine whether or not to continue the loop
            finish = true;
        }else{
            finish = false;
        }
    }
}


Items header (Items.h)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#ifndef ITEMS_H
#define ITEMS_H
#include <string>

class Items{

public:
    Items(std::string name, std::string desc, int val, int stackval);//constructor initializes all item values(variables)
    void add();//"adds" an item to the stack(adds 1 to _stackval)
    void rem();//"removes" an item from the stack(subtracts 1 from _stackval)

    //setters
    void setStack(int Stack){Stack = _stackval;} //camelCase convention broke to avoid possible conflicts involving stack apparently being a preexisting keyword

    //getters
    std::string getName(){return _name;}
    std::string getDesc(){return _desc;}
    int getVal(){return _val;}
    int getStack(){return _stackval;}

private:
    std::string _name, _desc; //name and description of items
    int _val, _stackval; //value of the item. Items will do nothing else in this version because this is mostly practice.
};

#endif // ITEMS_H 


Items.cpp
1
2
3
4
5
6
7
8
9
10
11
12
#include "Items.h"

Items::Items(std::string name, std::string desc, int val, int stackval){
    name = _name;
    desc = _desc;
    val = _val;
    stackval = _stackval;
}

void Items::add(){_stackval++;}

void Items::rem(){_stackval--;}


Shop header (Shop.h)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef SHOP_H
#define SHOP_H
#include <list>
#include "Items.h"

class Shop{

public:
    Shop();
void shopIntro(std::string name); //introductory message informing the user of which shop they have entered
void shopList(std::string name, std::string desc, int val, int stackval); //function initializes the shop list by adding the items to the list
void printShop(); //visual reprisentation of the shop. Basically prints the contents of the item list.

private:
    std::list<Items> _itemList; //list of items(Items class objects)
    int _i; //counter variable used in printShop
};
#endif // SHOP_H 


Shop.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <cstdio>
#include "Shop.h"

Shop::Shop(){
}

void Shop::shopIntro(std::string name){
    printf("Welcome to %s! Would you like to buy something or sell something?\n\n", name.c_str());
}

void Shop::shopList(std::string name, std::string desc, int val, int stackval){
    _itemList.push_back(Items(name, desc, val, stackval)); //Item properties are set in each call. The program will eventually retrieve the properties from a file instead of being hard coded.
}

void Shop::printShop(){
    std::list<Items>::iterator lit;
    for(lit = _itemList.begin(); lit != _itemList.end(); lit++){ //The loop is used to iterate through each item on the list and the name found in the properties is printed to the screen.
    std::cout<<_i<<") "<<(*lit).getName()<<std::endl;
        _i++;
    }
}


I'd guess that you're talking about _i.
Well, _i is not initialized in the Shop constructor and hence has a random value. So initialize it to 0 or 1 (whatever start value you want).
I don't see why it's a member variable. I'd think that a local variable suffice
That fixed the issue with that variable not being printed. I could have made the variable local and didn't really have a need to make it a member variable, but it's somewhat of a habit for me. I feel no need to change it because no harm is being done(now that I have it initialized to something instead of just having it defined to null). However, this post was more geared towards the fact that (*lit).getName() is not being displayed in the cout statement. To the best of my knowledge, there is no reason that should be blank because the function that creates it to begin with is called with the variables explicity initialized in the parameters.
Look at your constructor for Items:

1
2
3
4
5
6
Items::Items(std::string name, std::string desc, int val, int stackval){
    name = _name;
    desc = _desc;
    val = _val;
    stackval = _stackval;
}


Take a close look at those assignments. What variables are you assigning new values to?
I don't understand why that would be an issue. The code in question where the problem is displays what one of those variables are for that iteration. New values are only assigned in a completely different function that is called before the function that prints them.
Did you actually consider my question?

Take a close look at those assignments. What variables are you assigning new values to?
What MikeyBoy pointed out is actually the issue why (*lit).getName() returns an empty string.

gingy wrote:
I feel no need to change it because no harm is being done
So? Call printShop() twice and see the difference

By the way: in void setStack(int Stack){Stack = _stackval;} your made the same mistake
Last edited on
@coder777 the qoute was referring to what you said about int _i being a member variable instead of a local variable. What you said about initializing _i had solved that issue. However, i still dont understand why (*lit).getName() is a blank string because the function called before printShop adds an object to the list specifying what those values are supposed to be.. Therefore, (*lit).getName() should be "Placeholder item". Since calling the function to print the shop twice only duplicates the problem, i suspect the problem is with how the values are set. What i dont understand is where i went wrong with that. Please don't mistake what I'm saying to be out of a lack of effort. I have thoroughly read what you said and still don'tunderstand where i went wrong. I've tried to explain why I don't see why this doesn't work.
1
2
3
4
5
6
7
#include <iostream>
int main(){
   int a = 42;
   int b = 54;
   a = b;
   std::cout << a << ' ' << b << '\n';
}
It outputs
54 54


Now compare that code with
1
2
3
4
5
6
Items::Items(std::string name, std::string desc, int val, int stackval){
    name = this->_name;
    desc = this->_desc;
    val = this->_val;
    stackval = this->_stackval;
}
(the thiswere added for clarity, they do not change the meaning of the code)
Thank you. I understand what is wrong now, but am not quite sure how to fix it.
Well you should know what happens during assigning stufff:
1
2
3
4
5
6
Items::Items(std::string name, std::string desc, int val, int stackval){
    _name = name; // NOTE that the member variable must be on the left side!
    _desc = desc; // NOTE that the member variable must be on the left side!
    _val = val; // NOTE that the member variable must be on the left side!
    _stackval = stackval; // NOTE that the member variable must be on the left side!
}


You need to assign the parameters to the member variables! It makes no sense to do that the other way
I apologize for the confusion. The problem turned out to be what coder77 said it was and was merely a matter of me just writing the values to set backwards. Thank you, everyone for all the help with this. I'm also sorry that it took as long as it did to get back on this, I was having problems with my connection.
Topic archived. No new replies allowed.