0

I originally had all this code in one source file because I misread the assignment instructions. Then I saw it said please do NOT have all this code in one source file and I thought that made much more sense.

It compiled just fine when I had everything in my main.cpp, but now it's giving me:

error: invalid types 'char[int]' for array subscript"

In my main I have the array declared like:

#include "CaesarEncryptDecrypt.h"

using namespace std;
string pFile, cFile;
char textFile{1000};

int main()
{
//rest of main code...

and in my header I have it declared like:

// Globals
extern string pFile, cFile;
extern char textFile;

but then it gets to my two source code files and it shows errors here:

void encrypt (int shift, ifstream & plainTextFile, ofstream & cipherFile){
    char letter;
    int i = 0;
    while(!plainTextFile.eof()){
            plainTextFile.get(textFile[i]);
            /* 'error: invalid types 'char[int]' <--This error shows up at 
               every instance of me trying to use textFile 
               array. */

            i++;
    }

I'm sure I've missed something obvious here.

jhpratt
  • 6,841
  • 16
  • 40
  • 50

1 Answers1

2

In your code you have a variable of type char that is initialized with a value of 1000

char textFile{1000};

when it appears you want a char array of length 1000. To do this you would need to change your definition to

char textFile[1000];

to create a char array (note the square braces). Then in your header you need to declare textFile as an extern array:

extern char textFile[1000];

mjc449
  • 114
  • 1
  • 9
  • I did that and it fixed my other errors, but now I'm getting 'error: conflicting declaration 'char textFile [1000]' – ScoobyDont May 27 '18 at 03:36
  • I fixed my previous post. The header declaration needs to match the definition. See [this post](https://stackoverflow.com/questions/7670816/create-extern-char-array-in-c) for more info. – mjc449 May 27 '18 at 03:41
  • Thank you! This fixed it. I can't believe I didn't notice the brackets lol, feel like an idiot. – ScoobyDont May 27 '18 at 03:44