I am trying to write a console stats program for fun that can calculate a chi-square one-way goodness of fit. When prompting for user inputs after a handful of inputs my program crashes and windows tries to find issues. What do I need to change in order for the program not to crash? I have a strange idea that C++ does not like the way that I tried to create an ever expanding array.
int UserInput()
{
int StatLength = 0;
int StatData[StatLength];
int LoopLimit = 0;
int StatInput;
int Limit;
while (LoopLimit <1)
{
cout << "\nPlease enter a interger for calculation\n";
cin >> StatInput;
cout << "\nYou Entered\n" + StatInput;
StatData[StatLength] = StatInput;
cout << "\nWould You Like To Enter More Data Please Type 1 For Yes Or 2 For No";
cin >> Limit;
if (Limit == 1)
{
StatLength++;
}
else
{
LoopLimit++;
return 0;
}
}
}
The issue is line 4. You can't create an array off the stack with a non constant integral. Not only that, but you are also trying to create an array of size 0 which is illegal as well. To fix this give the array a constant size(an integer literal like 10) . However, if you need the array to grow you can use std::vector http://www.cplusplus.com/reference/vector/vector/ and ask us for help with that. Also, from what it seems like, you don't need the condition while(LoopLimit < 1) considering that you exit the function and loop with return 0.