Hard Light Productions Forums
Off-Topic Discussion => General Discussion => Topic started by: Liberator on June 07, 2004, 11:27:23 am
-
Okay I'm supposed to write a program that generates a skeleton of another program for me to customize and write other programs from. I think it's a waste of my time, but anyway.
Just ignore the comments and tell me why it's throwing errors at the red section when I have the green sections as they are? Specifically:
c:\windows\desktop\codegen.cpp(21) : error C2057: expected constant expression
c:\windows\desktop\codegen.cpp(21) : error C2466: cannot allocate an array of constant size 0
c:\windows\desktop\codegen.cpp(21) : error C2133: 'libraries' : unknown size
#include
#include
#include
#include
using namespace std;
void main()
{
int[color=green] ls=1[/color], cls=1, ss=1, cs=1, vs=1, lps=1;//these are array size variables and loop controls
string filename;
[color=red] string libraries[ls];[/color]
// string classes[cls];
// string structures[ss];
// string constants[cs];
// string variables[vs];
// string loops[lps];
cout << "What would you like to name the generated source file" << endl;
cin >> filename;
//initialization of output file
ofstream out_stream;
out_stream.open(filename);
//interactivity
cout << "How many C++ libraries are be to used?" << endl;
cin >> ls;
for(int x=0; x < ls; x++)
{
cout << "What is the name of the library?" < cin >> libraries[x];
}
}
Don't tell me string arrays can't have variable sizes cause the entire program is based on that.
-
Anybody?
-
Originally posted by Liberator
Okay I'm supposed to write a program that generates a skeleton of another program for me to customize and write other programs from.
Template design pattern?
-
I guess. If I can get that one section of code working I copy/paste my way to victory. I forgot to mention it's due tonight and I have to leave in about 45 minutes.
-
EDIT- not that. Dunno, then
-
try "const int", first of all, after that I'd say try using a different name than "libraries" for your array. Not personally using the string datatype I couldn't tell you to much about it, but you might try doing a 2d array of chars...(I.E. char libraries [ls] [256] would have a similar effect as string [ls] if I remember right, and would play roughly the same when using string functions, strcat, strcpy, etc...)
-
you must use a CONSTANT of somesort
a CONST INT may be acceptable, however it's better to use a typodef
-
The thing of it is, it should work like I have it. I mean if you can treat a string type variable like any other type variable, why should arrays be any different?
-
You're declaring an array, you must give it a size
that size must be either literal (ie "10") a CONST INT, or a preprocessor directive translating to a literal or a const int
-
There's a reason for this, I don't know what it is, but the reason you can't do that is because when you declare an array, it cannot have a variable size; every time the code is run the array size must be the same. The only way to be absolutely sure this is true is by using a constant, in one of these ways:
//Using the const operator
const int ls = 1;
...
string libraries[ls];
//Using a macro
#define ls 1
...
string libraries[ls];
//Doing it manually
string libraries[1];
If you want to use a non-constant operator, you have to create the array like this:
string *libraries = new string[ls];
...
delete[] string;
delete[] removes the memory space allocated for the array, and must be called or else memory will remain allocated after your program closes (So other programs can't use it). So if you use new[], always call delete[]
Another way is by using vectors, I won't go into this but if you use 'em it means you don't have to worry about memory leaks.
Edit: and incidentally, use better variable names. If you write much more code with that, it'll be hell to go back a month or two from now and try to modify it.
-
But I got an extention on that project. Anyway, I'm stuck again, something is wrong with the output file initializer on line 13:
#include
#include
#include
#include
using namespace std;
class Menu
{
public:
//initialization of output file
ofstream fileout;
fileout.open ("codegen.dat");
//loop control variables
int s,x;
string lib;
string cls;
string str;
string convar;
string var;
string loops;
string type;
string retrn;
void LibQuery()
{
cout << "How many C++ libraries are be to used?" << endl;
cin >> s;
for(x=0; x < s; x++)
{
cout << "What is the name of the library?" < cin >> lib;
out_stream << "#include <" << lib << ">" << endl;
}
out_stream << "using namespace std;" << endl << endl;
}
void ClassQuery()
{
cout << "How many Classes are be to used?" << endl;
cin >> s;
for(x=0; x < s; x++)
{
cout << "What is the name of the Class?" < cin >> cls;
out_stream << "Class " << cls << endl;
out_stream << "{" << endl << "public:" << endl << endl;
out_stream << "private:" << endl << endl << "};";
}
}
void StructQuery()
{
cout << "How many Structures are be to used?" << endl;
cin >> s;
for(x=0; x < s; x++)
{
cout << "What is the name of the Class?" < cin >> str;
out_stream << Struct << str << endl;
out_stream << "{" << endl << "public:" << endl << endl;
out_stream << "private:" << endl << endl << "};";
}
}
void ConstantQuery()
{
cout << "How many Constants are be to used?" << endl;
cin >> s;
for(x=0; x < s; x++)
{
cout << "What is the name of the Constants?" < cin >> convar;
cout << "What is it's type?"< cin >> type;
out_stream << type << convar << endl;
}
}
private:
};
void main()
{
int loopcon = 99;
Menu Choice;
//interactivity
while (loopcon !=0)
{
cout << "Welcome to Code Generator" << endl;
cout << "Please enter a selection" << endl << endl;
cout << "Select 1 to add C++ Library/Header files" << endl;
cout << "Select 2 to add C++ Class declarations" << endl;
cout << "Select 3 to add C++ Structure declarations" << endl;
cout << "Select 4 to add constant variable declarations" << endl;
cout << "Select 5 to add variable declarations" << endl;
cout << "Select 6 to add loop declarations" << endl;
cin >> loopcon;
switch (loopcon)
{
case 1:
{
Choice.LibQuery();
break;
}
case 2:
{
// Choice.ClassQuery();
break;
}
case 3:
{
// Choice.StructureQuery();
break;
}
case 4:
{
// Choice.ConstantQuery();
break;
}
case 5:
{
// Choice.VariableQuery();
break;
}
}
}
}
syntax error : missing ';' before '.'
'fileout' : missing storage-class or type specifiers
syntax error : '.'
unexpected token(s) preceding ';'
All 4 are occuring in the same line and I am at a loss. 6 years as a CIS student(pre-bachelor level, - 1 year for an unanticipated sabatical) and I can't debug a simple C++ error, I suck worse than I thought.
If that is the wrong way to open the file I'm sorry, but that's the way I learned how from a guy that had been writing code for 20 years.
Anyway, any help or suggestions would be appreciated. Thanks ahead of time. :D
-
that's a class - you run code outside of functions
create a constructor for your class and move the open code into it
Menu()
{
fileout.open ("codegen.dat");
}
-
BTW, if a class member doesn't need to or shouldn't be accessed from outside a class, it is good coding practice to make it private instead of public.
-
yeah, that too :P