Hey guys. just a disclaimer this is a homework question. The program takes user input and the user sets an array size as well as the numbers in the array. I think i have everything else working but it seems that if the user decides to input around 8 or higher as an array size I get the smashing error. It's supposed to take each array[value] and % 3 to determine if it's divisible by 3. It always works with the first few numbers but then stops for some reason with the error.
EDIT:
Thanks! I moved my initialization statement and it fixed that. The only thing left to figure it out is why I will get a zero occasionally as a listed number. My equation is if index[x] % 3 == 0 print index[x].. Is there something obvious I'm missing here? I'll keep plugging away at it.
EDIT:
Hey guys I got it and I figured I'd note it for future problem havers.
In the display multiples function I have it set to run until newArraySize hits its max and if you set it instead to newArraySize - 1 you wont get the extra number thrown in
Any tips would be appreciated.
#include <iostream>
usingnamespace std;
void getSize(int&);
//void print(int);
void getList(int, int[]);
void displayMultiples(int, int[]);
int getSize();
int getList();
int displayMultiples();
int main()
{
int arraySize;
int newArray[arraySize];
getSize(arraySize);
getList(arraySize, newArray);//added
displayMultiples(arraySize, newArray);//added
// print(arraySize);
return 0;
}
void getSize(int& newArraySize)
{
int arraySize;
cout << "Enter the size of the list: " ;
cin >> newArraySize;
cout << endl;
//int sizeArray[arraySize - 1 ];
}
void getList(int newArraySize, int ultraNewArray[])//added array in arguments
{
int newArray[newArraySize];//makin this -1 does not fix it
for (int x = 0 ; x <= newArraySize-1; x++)
{
int placeholder;
cout << "Enter number for index "<< x << ":" << endl;
cin >> placeholder; cout << endl;
ultraNewArray[x] = placeholder;
cout << endl;
}
}
void displayMultiples(int newArraySize, int ultraNewArray[])
{
cout << "The following are divisible by 3:\n";
for (int y = 0 ; y <= newArraySize ; y++)//adding - 1 lets the program run all the way but still causes stack overflow
{
if (ultraNewArray[y] % 3 == 0)
{
cout << ultraNewArray[y] << endl;
}
}
}