Yoyokolo Yoyokolo - 2 months ago 20
C++ Question

C++ SimpleVector Using Templates

I am trying to get my program to let the user choose which data type they would like to use, 1 for int, 2 for double, and 3 for string. Take that type and make it the type for our dynamic array. Allow the user to say how many inputs of the data they would like, and then allow the user to enter the data.

For some reason not clear to me, my program crashes right after the user enters any number for the type they want to use.

(I also have some other methods to implement, but I wanted to get this fixed first. So that's why there are unused methods.)

Is there something I am not seeing here? Any help is greatly appreciated. Thank you so much.

#include <iostream>
#include <cstdlib>

using namespace std;

template <class T>
class SimpleVector
{
private:
T *tempPointer;
int lengthOfArray;

public:
SimpleVector();
~SimpleVector();
SimpleVector(int lengthOfArray);
SimpleVector(const SimpleVector& copy);
int getArraySize();
T getElementAt(int n);
T & operator[](int index);
};

// default no-arg constructor
template <class T>
SimpleVector<T>::SimpleVector()
{
tempPointer = NULL;
lengthOfArray = 0;
}

// destructor for deallocating memory
template <class T>
SimpleVector<T>::~SimpleVector()
{
delete [] tempPointer;
}

// single argument constructor
template <class T>
SimpleVector<T>::SimpleVector(int dynamicArray)
{
lengthOfArray = dynamicArray;
tempPointer = new T[lengthOfArray];
}

// Copy constructor
template <class T>
SimpleVector<T>::SimpleVector(const SimpleVector& copy)
: lengthOfArray(copy.lengthOfArray), tempPointer(new int[copy.lengthOfArray])
{
int newSize = copy->size();
tempPointer = new T[newSize];

for(int i = 0; i < newSize; i++){
tempPointer[i] = copy.tempPointer[i];
}
}

// gets the size of the dynamic array
template <class T>
int SimpleVector<T>::getArraySize()
{
return lengthOfArray;
}

// returns element from array at specified position
template <class T>
T SimpleVector<T>::getElementAt(int n)
{
return *(tempPointer + n);
}

// returns reference to the element in array indexed by subscript
template <class T>
T & SimpleVector<T>::operator[](int index)
{
return this->tempPointer[index];
}

int main()
{
int dataType;
int dataSize = 0;
char keepGoing;

do{
cout << "What type of data do you want to enter?\n(1 for integer, 2 for double and 3 for strings)" << endl;
cin >> dataType;
cout << "How many data inputs? " << endl;
cin >> dataSize;

SimpleVector <int> list1(dataSize);
if (dataType == 1) {
SimpleVector <int> list1(dataSize);
}
else if (dataType == 2) {
SimpleVector <double> list1(dataSize);
}
else if (dataType == 3) {
SimpleVector <string> list1(dataSize);
}
else {
cout << " That's not an available option. Bye! " << endl;
return 0;
}

cout << "Please enter the data:" << endl;
for (int i = 0; i <= dataSize; i++) {
cin >> list1[i];
}


cout << "Do you want to enter data again? (y/n?)" << endl;
cin >> keepGoing;
}while((keepGoing == 'Y') | (keepGoing == 'y'));
return 0;
}

Answer

For some reason not clear to me, my program crashes right after the user enters any number for the type they want to use.

I would suggest, that you test your program multiple times, when you are writing it. As @Jarod42 said in his comment, your if statements don't really do much, because your

SimpleVector <type> list1(dataSize);

gets destroyed after { }.
So basically whatever number user types, your SimpleVector will always be of the type int.

Now when you try to:

cout << "Please enter the data:" << endl;
for (int i = 0; i <= dataSize; i++) {
cin >> list1[i];
}

you are calling function:

template <class T>
T & SimpleVector<T>::operator[](int index)
{
    return this->tempPointer[index];
}

At this point, you pointer is pointing to NULL, but you are trying to access some memory, which does not belong to you and that's why you program crashes.

EDIT:
I'm not sure if this is what you want, but i'll give it a try :

do{
    cout << "What type of data do you want to enter?\n(1 for integer, 2 for double and 3 for strings)" << endl;
    cin >> dataType;

    cout << "How many data inputs? " << endl;
    cin >> dataSize;

    if (dataType == 1) {
        SimpleVector <int> list1(dataSize);
        cout << "Please enter the data:" << endl;
        for (int i = 0; i <= dataSize; i++) {
            cin >> list1[i];
        }
    }
    else if (dataType == 2) {
        SimpleVector <double> list1(dataSize);
        cout << "Please enter the data:" << endl;
        for (int i = 0; i <= dataSize; i++) {
            cin >> list1[i];
        }
    }
    else if (dataType == 3) {
        SimpleVector <string> list1(dataSize);
        cout << "Please enter the data:" << endl;
        for (int i = 0; i <= dataSize; i++) {
            cin >> list1[i];
        }
    }
    else {
        cout << " That's not an available option. Bye! " << endl;
        return 0;
    }

    cout << "Do you want to enter data again? (y/n?)" << endl;
    cin >> keepGoing;
}while((keepGoing == 'Y') | (keepGoing == 'y'));