
February 15th, 2006, 09:07 PM
|
|
Registered User
|
|
Join Date: Feb 2006
Posts: 1
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.
|