Hi guys, I'm working on this class file program that asks a user to create an array, fill/append it with integers, and then find the input using a findElement function. My problem occurs when the program actually tries to find the value being passed in the findElement function. It does not seem like the copy constructor is being called, although I'm confused on how that works exactly. The run of the program shows the function working, although it returns the wrong information, or at least not the information I need it to. After creating an array, and filling it in with random values, this is what I get when I try to find the values:
What value would you like to append? (-1 to Quit): 34
Append Succeeded.
What value would you like to append? (-1 to Quit): 35
Append Succeeded.
What value would you like to append? (-1 to Quit): 36
Append Succeeded.
What value would you like to append? (-1 to Quit): 32
Append Succeeded.
What value would you like to append? (-1 to Quit): 44
Append Failed.
What value would you like to append? (-1 to Quit): -1
1. Create
2. Change
3. Append
4. Find
5. Print
6. Clear
7. Quit
Choice: 5
[0] 34
[1] 35
[2] 36
[3] 32
1. Create
2. Change
3. Append
4. Find
5. Print
6. Clear
7. Quit
Choice: 4
Enter a value to find it (-1 to Quit): 34
4
0Enter a value to find it (-1 to Quit): 33
4
0Not Found.Enter a value to find it (-1 to Quit): -1
Thanks in advance, here's my code:
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
|
#include <iostream>
using namespace std;
#ifndef INTEGERLIST_H
#define INTEGERLIST_H
class integerList
{
public:
integerList(); //Default constructor, 0 size array
integerList(int sz); //Constructor create array and initialize values to 0
integerList(const integerList &aList); //Copy constructor
~integerList(); //Destructor
void create(int sz); //Allocates the array and initializes value to 0
void clear(); //Removes the array space
int getSize() const; //Returns the numElements
bool append(int value); //Appends a value to end of array
bool find(int value) const; //Finds value and returns true, otherwise
//returns false
bool setElement(int loc, int value); //If loc valid, assigns value at index loc and
//returns true, otherwise returns false
bool getElement(int loc, int &value) const; //If loc valid, assign value from there and
//return true, otherwise returns false
integerList operator=(const integerList &bList); //Assignment operator used for copying
//from one object to another
private:
int *list; //Pointer to array
int numElements; //Number of elements
int size; //Array count size
bool isValid(int loc) const; //Private function used to make sure the loc is in range
};
ostream &operator<<(ostream &stream, const integerList &list); //Array output
#endif
|
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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
|
#include "integerList.h"
#include <iomanip>
using namespace std;
integerList::integerList()
{
numElements = 0;
}
integerList::integerList(int sz) //Constructor creates array and initializes all to 0
{
numElements = sz;
list = new int[sz];
for (int i=0; i<sz; i++)
list[i] = 0;
}
integerList::integerList(const integerList &aList) //Copy constructor, for making deep copies of
{ //arrays
numElements = aList.numElements;
size = aList.size;
list = new int[numElements];
for(int g=0; g<numElements; g++)
list[g] = aList.list[g];
cout << "copy constructor" <<endl;
}
integerList::~integerList() //Destructor
{
clear();
}
void integerList::create(int sz) //Creates a new array, with seperate array and
{ //element sizes
numElements = sz;
size = 0;
list = new int[sz];
for (int i=0; i<sz; i++)
list[i] = 0;
}
bool integerList::append(int value) //Appends a value to an element at the end
{ //of an array
if(size == numElements)
{
cout << "Append Failed." <<endl;
return false;
}
else
{
list[size] = value;
cout << "Append Succeeded." << endl;
size++;
}
return true;
}
bool integerList::find(int value) const
{
int count = 0;
cout << size << endl;
cout <<count;
while(count < size && value != list[count])
{
if(value == list[count])
{
cout << "Found.";
return true;
}
else
{
cout << "Not Found.";
count++;
return false;
}
}
}
void integerList::clear() //Removes the array
{
size = 0;
numElements = 0;
delete [] list;
list = NULL;
}
int integerList::getSize() const //Returns the array size
{
return numElements;
}
bool integerList::setElement(int loc, int value) //Assigns value at index loc
{
if (isValid(loc))
{
list[loc] = value;
size++;
return true;
}
else
return false;
}
bool integerList::getElement(int loc, int &value) const //Assigns value the number at loc
{
if (isValid(loc))
{
value = list[loc];
return true;
}
else
return false;
}
integerList integerList::operator=(const integerList &bList) //Overloaded assignment operator
{ //copies from one object to another
if(this != &bList)
{
delete [] list;
numElements = bList.numElements;
size = bList.size;
list = new int[numElements];
for(int j=0; j<numElements; j++)
list[j] = bList.list[j];
}
return *this;
}
bool integerList::isValid(int loc) const //Private function used to make sure the loc is n range
{
return (loc >= 0 && loc <= size);
}
ostream &operator<<(ostream &stream, const integerList &list) //Overload of << to output array
{
int number;
for (int j=0; j<list.getSize(); j++)
{
stream << "[" << j << "]" << setw(5);
list.getElement(j, number);
cout << number << endl;
}
return stream;
|
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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
|
int main()
{
integerList myList;
int choice;
while ((choice = menu()) != 7)
{
switch(choice)
{
case 1: createArray(myList); break;
case 2: changeElements(myList); break;
case 3: appendElements(myList); break;
case 4: findElements(myList); break;
case 5: printArray(myList); break;
case 6: myList.clear(); break;
}
}
return 0;
}
//Name: menu
//Purpose: prints the menu and asks the user for a choice
//Parameters: none
//Return: choice
int menu()
{
int c;
cout << endl;
cout << "1. Create" << endl;
cout << "2. Change" << endl;
cout << "3. Append" << endl;
cout << "4. Find" << endl;
cout << "5. Print" << endl;
cout << "6. Clear" << endl;
cout << "7. Quit" << endl;
cout << "Choice: ";
cin >> c;
return c;
}
void createArray(integerList &array)
{
int size;
cout << endl;
cout << "What size would you like the array to be?: ";
cin >> size;
array.create(size);
}
void changeElements(integerList &array)
{
int eOld, eNew;
cout << endl;
cout << "What element(index) would you like to change? (-1 to Quit): ";
cin >> eOld;
while(eOld != -1)
{
cout << "What value would you like to change this element to?: ";
cin >> eNew;
if(array.setElement(eOld, eNew))
cout << "\tChange Succeeded." << endl;
else
cout << "\tChange Failed. Please Try Again." << endl;
cout << "What element(index) would you like to change? (-1 to Quit): ";
cin >> eOld;
}
}
void appendElements(integerList &array)
{
int eNum;
cout << endl;
cout << "What value would you like to append? (-1 to Quit): ";
cin >> eNum;
while(eNum != -1)
{
array.append(eNum);
cout << "What value would you like to append? (-1 to Quit): ";
cin >> eNum;
}
}
void findElements(const integerList &array)
{
int findVal;
cout <<endl;
cout << "Enter a value to find it (-1 to Quit): ";
cin >> findVal;
while(findVal != -1)
{
array.find(findVal);
cout <<"Enter a value to find it (-1 to Quit): ";
cin >> findVal;
}
}
void printArray(const integerList &array)
{
cout << endl;
cout << array;
}
|