Why will my C++ code not compile…?

Question by Lazerus: Why will my C++ code not compile…?
I’m working in Visual Studio 2008 and working on a syntax checker for HTML and C source files.

I’m getting the following 3 errors…
error C2057: expected constant expression
error C2466: cannot allocate an array of constant size 0
error C2133: ‘cToken’ : unknown size

The error is coming from the line with all the space around it which reads, “char cToken[text.size()];”

//Here is my code

#include
#include
#include
#include

using namespace std;
class stack {
int top;
string stackArray[10000]; //Large Enough For Files
public:
stack(){ top=-1; }
void printStack(){
for (int i=0;i<=top;i++) cout << "Stack Data: " << i << " " << stackArray[i] << endl; } void push(string j) { stackArray[++top] = j; } string pop() { return stackArray[top--]; } string peek() { return stackArray[top]; } bool isEmpty() { return (top == -1); } }; bool HTMLChecker(vector text){
const char *htmlToken;
string htmlvalue;
unsigned int slength;
unsigned int closer;
signed int i1,i2,i3,i4;

stack stackObject;

i1 = i2 = i3 = i4 = -1;
for (int i=0; i < text.size(); i++){ htmlToken = text[i].c_str(); slength = strlen(htmlToken); i1=i2=i3=-1; for ( int j = 0; j < slength; j++){ if ( htmlToken[j] == '<' ) i1 = j; if ( htmlToken[j] == '>‘ )
i2 = j;

if (i1 != -1 && i2 != -1){
htmlvalue.assign(text[i], i1, i2 + 1);
closer = htmlvalue.find(“ text ){

char cToken[text.size()];

const char *cvalue;
unsigned int tokens;
stack stackObject;
for (int i = 0; i < text.size(); i++){ cvalue = text[i].c_str(); tokens = 0; for (int ii=0;ii<=strlen(cvalue);ii++){ cToken[ii] = cvalue[ii]; tokens++; } for (int i=0; i<=tokens; i++){ if ( cToken[i] == '(' || cToken[i] == '{' || cToken[i] == '['){ std::string tokenData(1, cToken[i]); stackObject.push( tokenData ); } if ( cToken[i] == ')' || cToken[i] == '}' || cToken[i] == ']' ){ if ( !stackObject.isEmpty() ){ string cmatch = stackObject.pop(); // Pop and Get Popped if ( ( cToken[i] == '}' && cmatch != "{" ) || ( cToken[i] == ']' && cmatch != "[" ) || ( cToken[i] == ')' && cmatch != "(" ) ) { cout << "Character "; cout << i; cout << " invalid." << endl; return false; } } else { return false; } } } } return ( stackObject.isEmpty() ); } void pauseprogram(){ char a; cout << "Press 'e' to exit " << endl; while (a != 'e') a = getchar(); } void usage(){ cout << "-------------------------- Usage Section -------------------------" << endl; cout << "program extension determines method used” << endl; cout << "valid test files should end with ext .cpp, .c , .html, or .htm" << endl; cout << "other file extensions will terminate program" << endl; cout << "restart program to test again" << endl; cout << "------------------------------------------------------------------" << endl; cout << endl; pauseprogram(); } int main(int argc, char *argv[]) { char temp[256]; char str1[256]; char * filename = NULL; int i1; switch (argc){ case 1: printf("Enter filename: "); cin >> temp;
filename = temp;
break;
case 2:
if (strcmp(argv[1],”-usage”) == 0){
usage();
return EXIT_SUCCESS;
}
else{
filename = argv[1];
}
break;
default:
usage();
return EXIT_SUCCESS;
}

std::string fname;
fname = fname + filename;
i1 = fname.rfind(“.”);
if (i1 == string::npos){
cout << "Not A Valid Extension." << endl; usage(); return EXIT_SUCCESS; } string ext; ext.assign(fname, i1+1, fname.size() ); unsigned fileext; if ( ext == "html" || ext == "htm" ){ fileext = 0; } else if ( ext == "c" || ext == "cpp"){ fileext = 1; } else{ cout << "Not A Valid Extension." << endl; usage(); return EXIT_SUCCESS; } ifstream infile; infile.open ( filename, ifstream::in ); if( !infile.is_open() ){ cout << "Error while opening the file!" <&l Best answer:

Answer by Varun
Hi,

I have fully corrected your program. Run it now to see it in action. There were small mistakes like missing brackets etc.

#include
#include
#include
#include

using namespace std;
class stack {
int top;
string stackArray[10000]; //Large Enough For Files
public:
stack(){ top=-1; }
void printStack(){
for (int i=0;i<=top;i++) cout << "Stack Data: " << i << " " << stackArray[i] << endl; } void push(string j) { stackArray[++top] = j; } string pop() { return stackArray[top--]; } string peek() { return stackArray[top]; } bool isEmpty() { return (top == -1); } }; bool HTMLChecker(vector text){
const char *htmlToken;
string htmlvalue;
unsigned int slength;
unsigned int closer;
signed int i1,i2,i3,i4;

stack stackObject;

i1 = i2 = i3 = i4 = -1;
for (int i=0; i < int(text.size()); i++){ htmlToken = text[i].c_str(); slength = strlen(htmlToken); i1=i2=i3=-1; for ( int j = 0; j < int(slength); j++){ if ( htmlToken[j] == '<' ) i1 = j; if ( htmlToken[j] == '>‘ )
i2 = j;

if (i1 != -1 && i2 != -1){
htmlvalue.assign(text[i], i1, i2 + 1);
closer = htmlvalue.find(“ text ){
char *cToken = new char[text.size()];
const char *cvalue;
unsigned int tokens;
stack stackObject;
for (int i = 0; i < int(text.size()); i++){ cvalue = text[i].c_str(); tokens = 0; for (int ii=0;ii<=int(strlen(cvalue));ii++){ cToken[ii] = cvalue[ii]; tokens++; } for (int i=0; i<=int(tokens); i++){ if ( cToken[i] == '(' || cToken[i] == '{' || cToken[i] == '['){ std::string tokenData(1, cToken[i]); stackObject.push( tokenData ); } if ( cToken[i] == ')' || cToken[i] == '}' || cToken[i] == ']' ){ if ( !stackObject.isEmpty() ){ string cmatch = stackObject.pop(); // Pop and Get Popped if ( ( cToken[i] == '}' && cmatch != "{" ) || ( cToken[i] == ']' && cmatch != "[" ) || ( cToken[i] == ')' && cmatch != "(" ) ) { cout << "Character "; cout << i; cout << " invalid." << endl; return false; } } else { return false; } } } } return ( stackObject.isEmpty() ); } void pauseprogram(){ char a = ''; cout << "Press 'e' to exit " << endl; while (a != 'e') a = getchar(); } void usage(){ cout << "-------------------------- Usage Section -------------------------" << endl; cout << "program extension determines method used” << endl; cout << "valid test files should end with ext .cpp, .c , .html, or .htm" << endl; cout << "other file extensions will terminate program" << endl; cout << "restart program to test again" << endl; cout << "--------------------------------------" << endl; cout << endl; pauseprogram(); } int main(int argc, char *argv[]) { char temp[256]; char str1[256]; char * filename = NULL; int i1; switch (argc){ case 1: printf("Enter filename: "); cin >> temp;
filename = temp;
break;
case 2:
if (strcmp(argv[1],”-usage”) == 0){
usage();
return EXIT_SUCCESS;
}
else{
filename = argv[1];
}
break;
default:
usage();
return EXIT_SUCCESS;
}

std::string fname;
fname = fname + filename;
i1 = fname.rfind(“.”);
if (i1 == string::npos){
cout << "Not A Valid Extension." << endl; usage(); return EXIT_SUCCESS; } string ext; ext.assign(fname, i1+1, fname.size() ); unsigned fileext; if ( ext == "html" || ext == "htm" ){ fileext = 0; } else if ( ext == "c" || ext == "cpp"){ fileext = 1; } else{ cout << "Not A Valid Extension." << endl; usage(); return EXIT_SUCCESS; } ifstream infile; infile.open ( filename, ifstream::in ); if( !infile.is_open() ){ cout << "Error while opening the file!" <

What do you think? Answer below!

You can leave a response, or trackback from your own site.

Leave a Reply

You must be logged in to post a comment.

Powered by Yahoo! Answers