A Class, an int array and a for loop walked into the bar....then they got stuck.

This is a discussion on A Class, an int array and a for loop walked into the bar....then they got stuck. within the C++ Programming forums, part of the General Programming Boards category; Hello everybody! I've got a problem with the code below and would appreciate any help that you could give me. ...

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    9

    A Class, an int array and a for loop walked into the bar....then they got stuck.

    Hello everybody!

    I've got a problem with the code below and would appreciate any help that you could give me.
    The problem is that I have a class called "Class_1" and inside I have multiple int arrays which will store the values. (I know this is a bad way to do it but its the easiest way for me so far so I'm trying to stick with it).
    I have the user input the entries for each int and it goes into the array and everytime they finish the position in the array increases.
    This is where the screwy-ness occurs. When I attempt to display them in a for loop it doesnt show the values, instead it shows what I assume to be the memory address or something except it seems to be the same value....

    Code:
    class Class_1{
        private:
            
        public:
            ~Class_1(){};
            Class_1(){};
    //        void set_values(...);
            void disp_values(Class_1 object);
            int _one[50], _two[50], _three[50], _four[50];
            string _five[50];
    
    
    
    /* ..............................
    */
    
    void new_entry(Class_1 object){
        cout << "\n One: ";
        cin >> object._one[num];
        cin.ignore();
    
    
        cout << "\n Two: ";
        cin >> object._two[num];
        cin.ignore();
    
    
        cout << "\n Three: ";
        cin >> object._three[num];
        cin.ignore();
    
    
        cout << "\n Four: ";
        cin >> object._four[num];
        cin.ignore();
    
    
        cout << "\n Five: ";
        cin >> object._five[num];
        cin.ignore();
    
    
        num++;
        cout << "\nEntry " << nPacket << " created.";
    }
    
    /*...........................................
    */
    
    
    void Class_1::disp_values(Class_1 object){
        int row=0;
    
    
        cout << "\nOne \tTwo \tThree \tFour \tFive\n"
                "------------------------------------";
    
    
        for (row = 0; row < num; row++)
        {
            cout << endl << object._one[num] << "\t" << object._two[num];
            cout << "\t" <<object._three[num] << "\t" << object._four[num];
            cout <<"\t" << object._five[num];
        }
        cout << "\n------------------------------------\n";
        cout << endl <<  num << " number of values.\n\n";
    }
    
    //For loop loops my array and displays the values in the corresponding position
    
    //also the global variable "num" simply keeps track of the number of entries
    So if anyone could help me out it would be brilliant! Also, if I havent given enough information just say and I'll try and provide some more info. Thanks in advance everybody!

    Shini

  2. #2
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    First thing is that num has to be a member variable of your class. After all you want to keep track of how many entries there are for a certain instance of Class_1.

    The next mistake is that you pass object by value to the function new_entry().
    This way you pass a copy to that functions. In new_entry() you modify the member arrays but at the end of the function that copy is destroyed and your new values are lost.
    You have to pass object by reference like
    Code:
    void new_entry(Class_1 & object)
    The function disp_values() is a member function of Class_1, so you don't have to pass anoter object, just print the values of that instance.

    If you think you have to pass an object then you should pass it by const reference. That would avoid a lot of unnecessary copying.

    Kurt

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    9
    Hi Kurt,

    Thanks for that, I always keep making mistakes with the minor differences with stuff like that. I've applied the changes that you have written about (i.e. moving disp_values() and num into Class_1 and changed the parameters of new_entry()) but it seems to be causing a similar problem. Do i need to alter how I call the variables within the function? i.e. do I still do it as

    Code:
    void new_entry(Class_1 & object){ cout << "\n One: "; cin >> & object._one[object.num]; cin.ignore(); .... }
    So I pass forward the address of these variables? I'll probably try it to see what happens but I'm gonna post it just to see what other's think...

    However, you also mentioned something about passing using a const reference, does that mean that I should use

    Code:
    void new_entry(const Class_1 & object)
    {
    ...
    }
    Really appreciate the quick reply Kurt, I didnt post back until now because I posted before I had to head out so I didnt have time to post a reply until now.

    Thanks again,
    Shini

  4. #4
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    When I mentioned const refernce then I was talking about disp_values().
    But it's anyway wrong to pass anything to that funcion.
    I would implement it like that
    Code:
    void Class_1::disp_values(){
        cout << "\nOne \tTwo \tThree \tFour \tFive\n"
                "------------------------------------";
        for (int row = 0; row < num; row++) {
            cout << endl << _one[row] << "\t" << _two[row];
            cout << "\t" << _three[row] << "\t" << _four[row];
            cout <<"\t" << _five[row];
        }
        cout << "\n------------------------------------\n";
        cout << endl <<  num << " number of values.\n\n";
    }
    main could look like this

    Code:
    int main() {
        Class_1 & object;
        new_entry(object);  // first entry
        new_entry(object);  // second entry
        object.disp_values();
        return 0;
    }
    if you have implemented new_entry() correctly, the right values should be shown.

    Kurt

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    9
    Thanks again Kurt for the help! I've changed the disp_values() to have no parameters and that's working perfectly.

    There's still a problem with the int main() though, the compiler is saying that since 'object' is declared as a reference but not initialised. I understand that that is because I havent initialised it and that I'm attempting to get it to reference the address of 'object' but I'm not too sure on how I should proceed in regards to that except maybe to declare something like this....

    Code:
    int main(){
         Class_1 *ptr_obejct, object;
         ptr_object = &object;
    
         new_entry(*ptr_object);
         ......
    }
    Not sure if that would work, but once again, gonna try it and see what happens....

    Thanks,
    Shini

  6. #6
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    I'm very sorry. My example main() was all wrong
    should have been
    Code:
    int main() {
        Class_1 object;
        new_entry(object);  // first entry
        new_entry(object);  // second entry
        object.disp_values();
        return 0;
    }
    Kurt
    Last edited by ZuK; 05-06-2012 at 02:08 PM.

  7. #7
    Registered User
    Join Date
    May 2012
    Posts
    9
    Ah right! Well I've just tried this in a brand new project and it works. But for some reason it wont seem to work in the context of this current program I'm trying out. Think I'll be able to solve it from here though, I really appreciate the help Kurt!

    EDIT:
    Okay turns out I was just being an idiot, the problem that I was going wrong was because in the disp_values() I was using the value of 'num' in the for loop with _one[num] instead of _one[row]. Think its completely working now! Thanks again!
    Last edited by Shini; 05-06-2012 at 03:20 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help.. newbie for loop..stuck with the loop..
    By jochen in forum C Programming
    Replies: 15
    Last Post: 10-01-2007, 12:31 AM
  2. Stuck in a FOR loop
    By xp5 in forum C Programming
    Replies: 2
    Last Post: 09-04-2007, 08:15 PM
  3. stuck in a loop
    By sweetly in forum C++ Programming
    Replies: 4
    Last Post: 10-14-2003, 01:24 PM
  4. Stuck in a loop!.....Get me out of here!!
    By rabmaz in forum C Programming
    Replies: 3
    Last Post: 09-01-2002, 09:16 AM
  5. I am stuck in a loop!!!
    By cujo77 in forum C++ Programming
    Replies: 2
    Last Post: 03-24-2002, 08:52 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21