Hey everyone. I'm brand new to C++ and programming in general and this is my first post here. I've found lots of good info here and these forums helped me a lot last semester. I've got a problem here though. This is a small bit of code I had to turn in yesterday, but for the life of me I couldn't understand why VC++ kept giving me compile errors but g++ compiled it just fine. Here it is:
/*CSIS 297
* Chapter 10 Challenge 5: Pie a la Mode
*/
#include <iostream>
#include <algorithm>
using namespace std;
/***********************************************
* *
* Function Prototype *
* *
***********************************************/
int pieSort(int*, const &int);
/***********************************************
* *
* MAIN *
* *
***********************************************/
int main()
{
int mode;
const int SIZE = 31;
int *piesPtr = new int[SIZE];
cout << "This program will accept the number of pies eaten by\n"
<< "30 individuals, calculate and display the mode.\n" << endl;
for (int count = 1; count < SIZE; count++) {
cout << "Person #" << count << ": ";
cin >> *(piesPtr + count);
}
mode = pieSort(piesPtr, SIZE);
delete [] piesPtr;
piesPtr = 0;
cout << "Mode = " << mode << endl;
return 0;
}
/**************************************************
* pieSort() function: *
* sorts pie slices eaten from lowest to highest *
* and returns the mode. *
**************************************************/
int pieSort(int *p, const int &S) {
int m = 0;
int modeCounter[S]; // This is where the compiler in VC++ complains
modeCounter[1] = 0; // It wants a const value for S.
sort( (p + 1), (p + S) );
for (int count = 1; count < S; count ++) {
cout << *(p + count ) << endl;
}
for (int index = 1; index < 31; index++) {
for (int count = index; count < 31; count++) {
if ( *(p + count) == *(p + count +1) ) {
modeCounter[count] += 1;
}
if (modeCounter[count] > m) {
m = *(p + count);
}
}
}
return m;
}
No matter what I do, whether it's being passed as a pointer, by reference, or just copy it simply won't compile in Visual Studio but it works great with Geany and g++. Why does the "const int" assignment to SIZE not transfer when passing the value to the function? And yes, I'm sure my code is a mess. Any and all criticism is welcome. Thanks!