Template's acting up...

Can someone tell me why while i'm adding units to this coreArray, the program pauses. Here's it is:
__________________________________________________________________
#ifndef LOTTOCLASS_H_INCLUDED
#define LOTTOCLASS_H_INCLUDED

using namespace std;
template<typename T>
class Lottery
{
public:
void Swap(T&a, T&b);
void arrRef(int num);
void createArray(int n);
int varArrayCounter;
void addVar(const T& value, int num);
void resetArray();
void delArray();
void showVar(int num);
private:
T* coreArray;
T* varArray;
};

template<typename T>
void Lottery<T>::addVar(const T& value, int num)
{
coreArray[num]= value;
}
template<typename T>
void Lottery<T>::createArray(int n)
{
delArray();
T *coreArray= new T [n];
T *varArray=new T [n];
}

template<typename T>
void Lottery<T>::arrRef(int num)
{
for (int i; i<num; i++)
{
varArray[num]=coreArray[num];
}
}
template<typename T>
void Lottery<T>::Swap(T&a, T&b)
{
T c;
c=a;
a=b;
b=c;
}
template<typename T>
void Lottery<T>::delArray()
{
delete[] coreArray;
delete[] varArray;
}
template<typename T>
void Lottery<T>::showVar(int num)
{
cout <<"{ ";
for (int i=0; i<num; i++)
{
cout<< varArray[num]<<" ";
}
cout <<" }"<<endl;
}
#endif // LOTTOCLASS_H_INCLUDED

________________________________________________________

#include <iostream>
#include <cstdlib>
#include <ctime>
#include "numonly.h"
#include "LottoClass.h"



int main()
{
string ans, n, n2;
int ans2, n1, b2;
cout << "Welcome to R-Lot!" << endl;
while (true)
{
cout << "R-Lot for Numbers (1) or Names (2)? ";
ans=EnterOnlyNumbers();
ans2=stringtonumber(ans);
cout<<endl;
if (ans2==1 || ans2==2)
break;
}

while (true)
{
cout<<"How many units are in the R-Lot? "; n=EnterOnlyNumbers();
n1=stringtonumber(n);
cout << endl;
if (n1>15)
cout << "R-Lot is too big, try another size!"<<endl;
else
break;
}
if ( ans2 == 1)
{
cout<<"Numbers"<<endl;
Lottery<int> Lotto;
Lotto.createArray(n1);
for (int b=0; b<n1; b++)
{
cout<<"Enter value for ["<<b+1<<"]: ";
n2=EnterOnlyNumbers();
b2=stringtonumber(n2);
Lotto.addVar(b2,b);
cout<<endl;
}
cout<<"Done!"<<endl;
Lotto.arrRef(n1);
Lotto.showVar(n1);
Lotto.delArray();
}
else
{
cout<<"Names!"<<endl;
Lottery<string> Lotto;
Lotto.createArray(n1);
Lotto.delArray();
}
return 0;
}
_________________________________________________________________
I'm working on the <int> version of the Lottery<int>Lotto, and whenever I'm at this part:

for (int b=0; b<n1; b++)
{
cout<<"Enter value for ["<<b+1<<"]: ";
n2=EnterOnlyNumbers();
b2=stringtonumber(n2);
Lotto.addVar(b2,b);
cout<<endl;
}

The damn C++ program i use just crash...

-And the Lotto.showVar(n1) won't show the Array correctly... it keeps showing
just this : { }

If anyone could just take a minute to read the code, and help me i'd really appreciate it.
[code] "Please use code tags" [/code]
You are making a delete [] of uninitialized pointers.
You need to set them to NULL in the constructor.

But you shouldn't reinvent the square wheel.
There is std::vector that you could use.
There is std::swap in algorithm.
Oh... Thanks for the advice for the pointers.
I'm just doing this to see if I can successfully make a template class program, not really try to reinvent anything...

Code tags... Are you talking about the //
Last edited on
¿what browser are you using?

code tags: http://www.cplusplus.com/articles/z13hAqkS/
That way you will have indentation, line numbers and sintax highlighting
Last edited on
Hello,may I join.....I need help,can someone tell me about an array...Cause I do my program using array..but when I want to write it say "invalid type"....can someone tell me about an array...
Think of an array as a variable that can store multiple units at once. In order to create an array : it has to be "int arrOne[]"
Topic archived. No new replies allowed.