C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old February 15th, 2006, 09:07 PM
Drifter81 Drifter81 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2006
Posts: 1 Drifter81 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 46 m 20 sec
Reputation Power: 0
Need "Class" Help

Ive written a program for a C++ class using sort and search methods, it compiles fine but I feel Im leaving something out because when I try and Print the list using a method, some how my counter "size" and the elements in the list which are in an array always print out as being 0 (zero) items in the array when the other methods to enter elements show that the elements have been entered. Here is the basics of my code, thanks in advance for the help.

Code:
 

#include <iostream.h>
#include <stdlib.h>
using namespace std;

const int MLS = 50;
typedef int element;

class AList {

        private:

                element items[MLS];
                int size;

        public:

                void Print ();
                void RandomListGenerator (int desired_size, int lower_limit, int upper_limit);
                int UserDefinedList ();
                void Swap (int pos_a, int pos_b);
                void BubbleSort ();
                void Initialize ();
                void Test ();
};

int Menu ();
int ReadInt ();
int Selection (int menu_choice, bool sorted, AList B);
void GenListParameters (int& desired_size, int& lower_limit, int& upper_limit);


int main () {

        sranddev ();

        int menu_choice;

        bool sorted = false;

        AList B;

        B.Initialize ();

        do {

                B.Test ();

                B.Print ();

                menu_choice = Menu ();

                B.Print ();

                menu_choice = Menu ();

                Selection (menu_choice, sorted, B);

        } while (menu_choice != 8);

}

void AList::Initialize () {

        size = 0;

}

void AList::Test () {

        cout << "there are " << size << " elements!!"<< endl;

}

int Menu () {

        int menu_choice;

        cout << " " << endl;
        cout << " " << endl;
        cout << "**************************************************  ***********" << endl;
        cout << "*                                                           *" << endl;
        cout << "*           Sort and Search Program Version 1.00            *" << endl;
        cout << "*                                                           *" << endl;
        cout << "*  1. Reset current list from the keyboard                  *" << endl;
        cout << "*  2. Reset current list using randomly generated elements  *" << endl;
        cout << "*  3. Perform Bubble Sort on the current list               *" << endl;
        cout << "*  4. Perform Insertion Sort on the current list            *" << endl;
        cout << "*  5. Perform Selection Sort on the current list            *" << endl;
        cout << "*  6. Perform Linear Search on the current list             *" << endl;
        cout << "*  7. Perform Binary Search on the current list             *" << endl;
        cout << "*  8. Quit the Program                                      *" << endl;
        cout << "*                                                           *" << endl;
        cout << "**************************************************  ***********" << endl;
        cout << " " << endl;
        cout << "Please select from the menu above: ";

        menu_choice = ReadInt ();

        while ((menu_choice < 1) || (menu_choice > 8)) {

                cout << "Invalid input, Range Violation, Please select a menu value from 1 to 8: " << endl;
                menu_choice = ReadInt ();
        }

        return menu_choice;

}

int ReadInt () {

        int val;

        cin >> val;

        while (!cin.good()) {

                cin.clear ();
                cin.ignore (80, '\n');
                cout << "Invalid Input, Type Violation, Please try again: " << endl;
                cin >> val;
        }

        return val;
}

int Selection (int menu_choice, bool sorted, AList B) {

        int desired_size, lower_limit, upper_limit;

        switch (menu_choice) {

                case 1: B.UserDefinedList ();
                break;

                case 2:
                GenListParameters (desired_size, lower_limit, upper_limit);

                B.RandomListGenerator (desired_size, lower_limit, upper_limit);
                break;

                case 3:
                B.BubbleSort ();
                break;


        }
}

void AList::Print () {

        int i;

        if (size == 0)

                cout << "Current list: (empty)" << endl;

        else {
                cout << "Current list: ";

                for (i = 0; i < size; i++)

                        cout << items[i] << " ";
        }

}

int AList::UserDefinedList () {

        element val;

        cout << " " << endl;
        cout << "Resetting the list from the keyboard." << endl;
        cout << " " << endl;

        cout << "Enter a series of elements, to stop enter -1: ";

        val = ReadInt ();
        size = 0;

        while ((val != -1) && (size < MLS)) {

                items[size] = val;
                size++;

                if (size < MLS)

                        val = ReadInt ();

                else
                        cout << "The current list is now too full to accept anymore elements." << endl;
        }

        cout << size << " elements entered." << endl;

}


That should be enough to find a problem, thanks again.

Reply With Quote
  #2  
Old February 16th, 2006, 01:45 AM
Icon's Avatar
Icon Icon is offline
Command Line Warrior
Click here for more information. Click here for more information
 
Join Date: Sep 2005
Posts: 735 Icon User rank is Private First Class (20 - 50 Reputation Level)Icon User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 3 Days 12 h 16 m 2 sec
Reputation Power: 4
If you change the Selection function
int Selection (int menu_choice, bool sorted, AList B);
to
int Selection (int menu_choice, bool sorted, AList &B);
then it works. This means you are passing the list by reference instead of by value (it's not exactly the same as a pointer by the way). If you do no do that than when you are calling Selection( .., .., B ) it is making a copy of the list, in selection you are filling that list but after Selection has ended it is destroyed again and you are back with your original empty list.
Comments on this post
Itsacon agrees: Knew you'd take this one :-)
Drifter81 agrees: Thank you very much

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Need "Class" Help


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
Stay green...Green IT