variable not incrementing?

I'm making thise database program and I have a class item. I need to call it with a name, price, amount and ID. For the ID i use int i as a side variable.
Yet, when I cal getId() it will ALWAYS be 15...

This is my code::



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void addItem(vector<item>& data, int& i)
{
    system("CLS");
    string naam;
    string buffer;
    int prijs;
    int aantal;
    cout<<"Name of product: ";
    getline(cin, naam);
    cout<<"Price of product: ";
    cin>>prijs;
    cout<<"Amount in stock: ";
    cin>>aantal;
    getline(cin, buffer);

    i++;

    item temp(naam, prijs, aantal, i);
    data.push_back(temp);
}


and

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
void database(vector<item>& data)
{
    int i = 0;
    while(true){
    database:
    system("CLS");
    cout<<setw(10)<<left<<"ID"<<setw(15)<<"Product: "<<setw(15)<<"Price"<<"Amount in stock\n";

    int begin = 1;


    for(; begin < data.size(); ++begin)
    {
        cout<<"\n"<<setw(10)<<data[begin].getId()<<setw(15)<<data[begin].getNaam()<<setw(15)<<data[begin].getPrijs()<<data[begin].getAantal();
    }

    cout<<"\n\n\n\n1: Add an item";
    cout<<"\n2: Delete an item";
    cout<<"\n3: Save";

    int keuze = getch() - '0';

    if (keuze != 1 && keuze != 2){
        cout<<"\n\nERROR, try again.";
        Sleep(1000);
        system("CLS");
        goto database;
    }

    if(keuze == 1){
    addItem(data, i);
    }

    if (keuze == 2){
    delItem(data);
    }

    }

}
Could you post the code for item::getId() or maybe just the whole item class? That is most likely where the error could reside.
Ok this is the class:

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
47
48
49
50
51
class item
{
    private:
    int id;
    string naam;
    int prijs;
    int aantal;

    public:

    item(string NAAM, int PRIJS, int AANTAL, int id)
    {
        naam = NAAM;
        prijs = PRIJS;
        aantal = AANTAL;
        id = id;

    }




    //getters & setters
    int getId(){
    return id;
    }
    string getNaam(){
    return naam;
    }
    int getPrijs(){
    return prijs;
    }
    int getAantal(){
    return aantal;
    }

    void setId(int temp){
    id = temp;
    }
    void setNaam(string temp){
    naam = temp;
    }
    void setPrijs(int temp){
    prijs = temp;
    }
    void setAantal(int temp){
    aantal = temp;
    }


};
id = id;

You do realize your mistake?

Also, your getter functions should be const.
Last edited on
I'll test with another name for the argument!
This is why teachers should teach the this operator sooner...
Not to forget initializer lists...
1
2
3
    item(const string& naam, int prijs, int aantal, int id) : naam(naam), prijs(prijs), aantal(aantal), id(id)
    {
    }
What is the "this" operator?
And I don't follow a class, I'm learning C++ for fun ;)
This is why teachers should teach the this operator sooner...


IMO, using 'this' as a qualifier is no excuse for name collisions.

It's better if you just don't have name collisions.

Not to forget initializer lists...

item(const string& naam, int prijs, int aantal, int id) : naam(naam), prijs(prijs), aantal(aantal), id(id)
{
}


Hi reference to the above initializer list, I read from Scott Meyers there is a catch to doing above syntax. The explanation is long but the idea is the ORDER you define above should CORRESPOND to the order they are declared in the class?!?! (Or maybe I read it wrongly)

clasclass item
{
private:
int id;
string naam;
int prijs;
int aantal;
...
}

Then your member initializer list must follow above order.

item(const string& naam, int prijs, int aantal, int id) : id(id), naam(naam), prijs(prijs), aantal(aantal)
{
}

Can anyone confirm on this? This was a reason till today I did not use initializer list also :(
In theory, you can have them in any order in the initializer list, however they will be initialized in the order they appear in the class.
So even if id stands at the end of the initializer list, it will be initialized first.

In theory, you can have them in any order in the initializer list, however they will be initialized in the order they appear in the class.
So even if id stands at the end of the initializer list, it will be initialized first.


Thanks a lot! Now I can use them with confidence! But to play safe, I better follow the order as recommended by Scott Meyers.

Above is important cuz usually in our constructor we do initialization and what if some initialization variables is dependent on other variables being initialized FIRST? So if we follow the above order we are guaranteed safety.
The order of the variables in the initializer lists does not matter - you cannot change the order of initializations by switching them around. If one initialization depends on the initialization of another object, you have to make sure the object it depends on is declared before it.
That being said, the order in the initializer list should of course match the order of declaration - but just for clarity, since it does not affect the program behavior at all.
Topic archived. No new replies allowed.