I have run into a problem with dynamic memory when dealing with vectors. How I know this is that I ran my program via the debugger, and it has at the top of the call stack a method from __cxa_throw(). //In fact, there are methods from new_allocator.h on top of methods from vector!
The weird thing is that this problem only happens for one case, though I think I should post my entire main() where all this code is (I tried to avoid global functions that handle any type of data except for user input to close the program):
int main()
{
bool keepLooping = true; //this will make the program keep asking for a choice when a valid one is not entered
bool exitCommand = false; //this will keep track of the user wanting to exit the program
char userInput;
vector<int> data;
int elementCount; //the amount of elements in data
cout << "Binary Search Tree Creator" <<endl;
cout << "==========================\n" << endl;
while (keepLooping)
{
cout << "How would you like to create a binary search tree?" << endl;
cout << "(1) Direct input" << endl;
cout << "(2) Random numbers (you decide how many)" << endl;
cout << "(3) Random numbers (random amount of random numbers" << endl;
cout << "(0) Exit this program" << endl;
cout << "\nPlease enter a selection: ";
userInput = cin.get(); //attempt to get only one character from the user
//exit the loop on a valid condition
if ((userInput >= '0') && (userInput <= '3')) keepLooping = false;
if (userInput == '0') exitCommand = true;
}
if (userInput > '0')
{
if (userInput < '3')
{
//get the specified elementCount from the user
while (elementCount <= 0)
{
cout << "How many random numbers should be in the data? ";
cin >> elementCount;
if (elementCount == 0) cout << "Enter a non-zero integer, please..." << endl;
}
if (userInput == '1')
{
//get that many integers from the user
data = vector<int>(elementCount, 0); //initalizing data with elementCount amount of zeroes
for (int x = 0; x < elementCount; x++)
{
cout << "Enter value for data[" << x << "] (must be an integer): ";
cin >> data[x];
}
}
else
{
//generate that many random integers
//data = vector<int>(elementCount, 0); //initializing data with elementCount amount of zeroes
srand(time(0)); //seeding the random number generator
for (int x = 0; x < elementCount; x++)
{
//generating random number between 1 and 1000 and assigning it to data
data.push_back(rand() % 1000 + 1);
}
}
}
else
{
srand(time(0)); //seed the random number generator
elementCount = rand() % 100 + 1; //specify a random elementCount for the data [1,100]
for (int x = 0; x < elementCount; x++)
{
data.push_back(rand() % 1000 + 1); //push a random number into data [1,1000]
}
}
//make a binary search tree of all this data!
BSTTest test(data);
test.printBST(); //print the data
}
else
{
pressEnterToContinue();
}
return 0;
}
I have initially tried to say data = vector<int>(elementCount,0); , but changed it to exactly what I was doing in the random case (too bad I didn't have it to where the same code block executes for either case; I guess you can't have "the best of both worlds"), but for some reason, it still throws some exception! Does anyone have any idea why this might be happening?
I think the problem occurs when you choose the cases 1 and 2 ,the reason for that is that you did not initial elementCount which takes at the first place a huge number
I tried to think about what are you thinking and I think! You want something like this
# include <iostream>
# include <vector>
# include <ctime>
# include <cstdlib>
# include <conio.h>
usingnamespace std;
class BSTTest
{
vector<int> myVect;
public :
BSTTest(vector<int>&l){myVect = l;}
void printBST();
};
void BSTTest::printBST(void)
{
for(size_t i=0;i<myVect.size();i++)
cout << myVect[i] << endl;
}
void pressEnterToContinue()
{
while(getch()=='\n')
return ;
}
int main()
{
bool keepLooping = true; //this will make the program keep asking for a choice when a valid one is not entered
bool exitCommand = false; //this will keep track of the user wanting to exit the program
char userInput;
vector<int> data;
int elementCount = 5; //the amount of elements in data
cout << "Binary Search Tree Creator" <<endl;
cout << "==========================\n" << endl;
while (keepLooping)
{
cout << "How would you like to create a binary search tree?" << endl;
cout << "(1) Direct input" << endl;
cout << "(2) Random numbers (you decide how many)" << endl;
cout << "(3) Random numbers (random amount of random numbers" << endl;
cout << "(0) Exit this program" << endl;
cout << "\nPlease enter a selection: ";
userInput = getche(); //attempt to get only one character from the user
//exit the loop on a valid condition
if ((userInput >= '0') && (userInput <= '3')) keepLooping = false;
if (userInput == '0') exitCommand = true;
}
if (userInput > '0')
{
if (userInput < '3')
{
//get the specified elementCount from the user
while (elementCount <= 0)
{
cout << "How many random numbers should be in the data? ";
cin >> elementCount;
if (elementCount == 0) cout << "Enter a non-zero integer, please..." << endl;
}
if (userInput == '1')
{
//get that many integers from the user
data = vector<int>(elementCount, 0); //initalizing data with elementCount amount of zeroes
for (int x = 0; x < elementCount; x++)
{
cout << "Enter value for data[" << x << "] (must be an integer): ";
cin >> data[x];
}
}
else
{
//generate that many random integers
//data = vector<int>(elementCount, 0); //initializing data with elementCount amount of zeroes
srand(time(0)); //seeding the random number generator
for (int x = 0; x < elementCount; x++)
{
//generating random number between 1 and 1000 and assigning it to data
data.push_back(rand() % 1000 + 1);
}
}
}
else
{
srand(time(0)); //seed the random number generator
elementCount = rand() % 100 + 1; //specify a random elementCount for the data [1,100]
for (int x = 0; x < elementCount; x++)
{
data.push_back(rand() % 1000 + 1); //push a random number into data [1,1000]
}
}
//make a binary search tree of all this data!
BSTTest test(data);
test.printBST(); //print the data
}
else
{
pressEnterToContinue();
}
return 0;
}