SRL Archive - C/C++ Help and Tutorials

New, delete and runtime error help!


Macrosoft

2007-10-30 16:06:02

so im making a script that converts srl 3 scripts to srl 4 scripts

i want to know how i can use new and delete to get the exact amout of lines needed in the scar script

some help with windows.h include would be appreciated as well

I also get an error when i use the builded version on the script

#include <iostream>
#include <fstream>
#include <string>
#include <new>
using namespace std;

int main ()
{
char File[150], re[4];
string line[5000];

string response;
int x;
int counter=0;
size_t found;
string strtofind[]={"oolean1", "oolean2", "oolean3", "nteger1", "nteger2", "nteger3",
"tring1", "tring2", "tring3"};
string changestr[]={"ooleans[0]", "ooleans[1]", "ooleans[2]", "ntegers[0]", "ntegers[1]",
"ntegers[2]", "trings[0]", "trings[1]", "trings[2]"};
int index;


cout<<" Script Converter\n";
cout<<" By Macrosoft\n\n";
cout<<"Would you like to convert a SRL 3.81 script to SRL 4? (yes or no) \n";
cin>>re;
_strupr_s(re);
response=re;
if(response== "YES") {
cout<<"Please enter the name of the file (C:/Program Files/example.scar)"<<endl;
cout<<"the location is not needed if this program and the script are in the same folder\n";
cin.ignore ( 150, '\n' ); //flush the input stream
cin.getline(File, 150);
cout<<"Now Converting "<< File<< " to SRL 4 ..."<<endl;
ifstream fin;
ofstream fout;
fin.open(File);
if(fin){
while (getline(fin, line[counter])){
counter++;
}
x=counter;
for(counter=0; counter<x+1; counter++){
for(index=0; index<9; index++){
found=line[counter].find(strtofind[index]);
if (found!=string::npos){
line[counter].replace(int(found), strtofind[index].length(), changestr[index]);
}
}
}
fout.open(File);
fout<<"{Script Converted to SRL 4 by Macrosoft's Script Converter}\n";
for(counter=0; counter<5000; counter++){
fout<<line[counter]<<"\n";
}
cout<<"Finished converting script\n";
}
else {
cout<<"Failed to open file\n";
}
fin.close();
fout.close();
}
else if(response == "NO"){
cout<<"Closing Program...\n";
}
else{
cout<<"Response not understood. Closing Program...\n";
}
system("PAUSE");
return 0;
}

R0b0t1

2007-10-30 22:01:38

I believe that there is documentation at MSDN about text file manipulation :p.

Macrosoft

2007-11-01 17:17:13

msdn????

i knwo file streams already, just need help with new and delete and my runtime error

R0b0t1

2007-11-01 18:37:55

http://www.msdn.net


Are you talking about this(?):


SomeClass MyClass = new SomeClass(Params); //Do you mean that "new"?

delete MyClass;

If thats it, you can't use them for files - they are for creating and destroying objects.

Macrosoft

2007-11-02 21:52:47

yeah, the one you need pinters for. basicly i have a 2d array of chars that has some amount of chars for the line, and then the amount of lines. I am not sure about what these values will be so i want to use new so i dont need an array with 1 million parts

R0b0t1

2007-11-03 00:32:24

ohh?


char MyFileArr[Linesinfile][Lengthofline];

Would that work? How big is the file?

Macrosoft

2007-11-11 11:57:35

thats the point, i need to use new and delete to add new parts of the array based on how big the file is. I dont know how big the file is

R0b0t1

2007-11-11 14:12:34

Well, Ok.


CHAR *MyFileArr = new CHAR[LinesOfFile][CharsInLine];

delete[][] MyFileArr; //You need one more '[]' for every array of array

CHAR MyFileArr = new CHAR[NewLength][NewLength];

Thats if you want to change its length… But you could do this also.


INT Lines = GetFileLines; //Thats not a build in procedure, just example
INT Chars = GetLineChars; //Again, not a real procedure.

CHAR *MyFileArr = new CHAR[Lines][Chars];

[..]

delete[][] MyFileArr;

Or you could make one with an undefined lines length, but you would need to know the amount of characters in a line.


CHAR *MyFileArr = new CHAR[][CharsInLine];

[..]

delete[][] MyFileArr;


Buy you would need to use pointers for these arrays, as you can only delete pointer objects.


error C2541: 'delete' : cannot delete objects that are not pointers

Macrosoft

2007-11-11 14:19:14

so i would add to an array of strings called la by doing:


string la[2];
*la= new string[1];

is that correct?

R0b0t1

2007-11-11 16:12:05


string* la = new string[1];

But that creates a string array with two (2) indexes.

Accesses like:

&la[I] = "String here FTW!/n";


But to add to it you would most likely have to delete it. But you can create one with unlimeted values:


string* UnlimArr = new string[];

You simply place your values in UnlimArr[NumberHere]. You might get errors with this though… Try it :)

Macrosoft

2007-11-11 21:52:53

#include <iostream>
#include <fstream>
#include <string>
#include <new>
using namespace std;

int main ()
{
char File[150], re[4];
string line[1];

string response;
int x;
int counter=0;
size_t found;
string strtofind[]={"oolean1", "oolean2", "oolean3", "nteger1", "nteger2", "nteger3",
"tring1", "tring2", "tring3"};
string changestr[]={"ooleans[0]", "ooleans[1]", "ooleans[2]", "ntegers[0]", "ntegers[1]",
"ntegers[2]", "trings[0]", "trings[1]", "trings[2]"};
int index;


cout<<" Script Converter\n";
cout<<" By Macrosoft\n\n";
cout<<"Would you like to convert a SRL 3.81 script to SRL 4? (yes or no) \n";
cin>>re;
_strupr_s(re);
response=re;
if(response== "YES") {
cout<<"Please enter the name of the file (C:/Program Files/example.scar)"<<endl;
cout<<"the location is not needed if this program and the script are in the same folder\n";
cin.ignore ( 150, '\n' ); //flush the input stream
cin.getline(File, 150);
cout<<"Now Converting "<< File<< " to SRL 4 ..."<<endl;
ifstream fin;
ofstream fout;
fin.open(File);
if(fin){
while (getline(fin, line[counter])){
*line = new string[0];
counter++;
}
x=counter;
for(counter=0; counter<x+1; counter++){
for(index=0; index<9; index++){
found=line[counter].find(strtofind[index]);
if (found!=string::npos){
line[counter].replace(int(found), strtofind[index].length(), changestr[index]);
}
}
}
fout.open(File);
fout<<"{Script Converted to SRL 4 by Macrosoft's Script Converter}\n";
for(counter=0; counter<x+1; counter++){
fout<<line[counter]<<"\n";
}
cout<<"Finished converting script\n";
}
else {
cout<<"Failed to open file\n";
}
fin.close();
fout.close();
}
else if(response == "NO"){
cout<<"Closing Program...\n";
}
else{
cout<<"Response not understood. Closing Program...\n";
}
system("PAUSE");
return 0;
}

c:\documents and settings\***\my documents\visual studio 2005\projects\srl 3.81 to srl 4\srl 3.81 to srl 4\script converter.cpp(40) : error C2679: binary '=' : no operator found which takes a right-hand operand of type 'std::string *' (or there is no acceptable conversion)

R0b0t1

2007-11-12 18:34:01

Sorry, I forgot that using brackets is like dereferencing it.


Remove the &:


la[I] = 'LOLOLOLOLO';

Macrosoft

2007-11-14 16:16:24

i dont have a & in my code, read it and tell me why i get the error