0
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <cmath>
#include <cstdlib>
#include <iomanip>

using namespace std;

#define ARRAYSIZE  15;


int main(void)
{
    //things needed
    ifstream infile;
    ofstream outfile;

    double xArray[ARRAYSIZE];


}

As you can see, my code should be in order but my program keeps telling me it expects a '[' where the xArray[ARRAYSIZE] is. By the way I'm using microsoft visual studio 2013.

Merc.Go
  • 1
  • 1

3 Answers3

2
#define ARRAYSIZE 15

Take the ; out of the #define.

With your #define written as is,

double xArray[ARRAYSIZE];

translates to

double xArray[15;];

The compiler expects a ] before the first ;.


Doing this:

const int ARRAYSIZE 15;

might be better...

nhgrif
  • 61,578
  • 25
  • 134
  • 173
  • +1 for suggesting using a const int instead of a define. Much better C++ practice. – Nathan Ernst Dec 05 '13 at 01:41
  • Yea. I mainly work with `Objective-C` and as such, I actually use `#define` on occasion, but no matter what the language, when I'm doing something simple like this, I use a `const int` (or `enum` in some cases, depending on situation). – nhgrif Dec 05 '13 at 01:47
  • nhgrif, `#defines` should be a last resort in C++ because of how they're applied (I agree that they are sometimes useful and/or useful). C/C++ are somewhat unique due to the phases of translation/compilation that occur. Say a `#define pi 3.14` for a constant may seem innocuous, but later in an unrelated place, someone else may declare a `double pi = 3.14159`. But, if the preprocessor version occurs first, the second, innocent instance will be translated to `double 3.14 = 3.14159`, which will obviously be an error. – Nathan Ernst Dec 06 '13 at 02:51
  • Oh, I agree. What I meant by my comment was that as I code most often in Objective-C, I use `#define` on occasion... but those occasions are almost always when writing Objective-C code. I almost never `#define` in `C++`. – nhgrif Dec 06 '13 at 02:53
0

after preprocess, your code is kind of

int main(void)
{
    //things needed
    ifstream infile;
    ofstream outfile;

    double xArray[15;];  // replace ARRAYSIZE with 15;
}

so you have to remove ; in #define

#define ARRAYSIZE  15
David G
  • 94,763
  • 41
  • 167
  • 253
Bryan Chen
  • 45,816
  • 18
  • 112
  • 143
0

#define ARRAYSIZE 15;

Define will substitute ARRAYSIZE with whatever is there next to it.

So in this case it will substitute ARRAYSIZE with 15;

So all you need to do is just remove the semi-colon in the define statement

Nathan
  • 674
  • 8
  • 22