Array unknown size (error C2133)

Hey all.

I have to write a program that tests whether or not an array of chars is a palindrome (where a word is read the same backwards as it is forwards - Radar, Madam, etc), but ignoring spaces and chars.

I'm pretty sure my code is right, but am having trouble with my copied array.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
using namespace std;

bool intPair(int first, int second);
bool testPalindrome(char phrase[], int length);

bool testPalindrome(char phrase[], int length) {
    int i;
	char newArray[];
        newArray[length]; //this seems to be the problem
        
		if (length < 1) {return 1;}
        		
		for (i = 0; i<length; i++) {
			if(phrase[i]!=' '){
				newArray[i] = phrase[i];
			}
		}

        if (tolower(newArray[0]) == tolower(newArray[length-1]))
            return testPalindrome (newArray+1, length-2);        
        else {
            return 0;
        }
}


bool intPair(int first, int second) {
    return (second%first == 0);
}

void main(){
    const int SIZE = 80;
    char c;
    char string[SIZE];

    int count = 0;
    int i;

    cout << "Enter a sentence: \n";

    while ( ( c = cin.get() ) != '\n' && count < SIZE) {
        string[count++] = c;

        string[count] = '\0';
    } //ends while loop
    
    int result = testPalindrome(string,count);

    if (result == 1) {cout << "It's a palindrome." << endl; }
    else { cout << "Not a palindrome" << endl; }

} 
You can not create a variable on stack with unkown size in compiler time, of course it includes arrays too.
You can use constants for array size or use dynamic memory(heap).

About dynamic memory please google malloc/free, new/delete
Last edited on
I've been trying to work out what you mean for the last few hours, Denis, but no result. Anyways, here's the updated code. I caught one or two other errors. I still could use some help.

Someone told me something about 'default'. I'm a little confused. Maybe if someone could clear that up?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
using namespace std;

bool intPair(int first, int second);
bool testPalindrome(char phrase[], int length);

bool testPalindrome(char phrase[], int length) {
    int i, skip = 0;
	char newArray[];
	newArray[length];    

if (length < 1) {return 1;}
   
	for (i = 0; i<length; i++) {
		if(phrase[i]!=' ') {
			++skip;
		}
		newArray[i-skip] = phrase[i];
		}

    if (tolower(newArray[0]) == tolower(newArray[length-1]))
            return testPalindrome (newArray+1, length-2);        
        else {
            return 0;
        }
}


bool intPair(int first, int second) {
    return (second%first == 0);
}

void main(){
    const int SIZE = 80;
    char c;
    char string[SIZE];

    int count = 0;
    int i;

    cout << "Enter a sentence: \n";

    while ( ( c = cin.get() ) != '\n' && count < SIZE) {
        string[count++] = c;

        string[count] = '\0';
    } //ends while loop
    
    int result = testPalindrome(string,count);

    if (result == 1) {cout << "It's a palindrome." << endl; }
    else { cout << "Not a palindrome" << endl; }

} 
Use int main().

1
2
	char newArray[];
	newArray[length];


You cannot do this. You will either have to use dynamic allocation or just use an std::string.
Last edited on
Topic archived. No new replies allowed.