ok so i made a code that makes something compatible with a never version ( it is not important). This is my code:

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

int main () 
{
	char File[150], re[4]; 
	string line[5000];
	string response;
	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<<"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++;
			}
			for(counter=0; counter<5000; counter++){		
				for(index=0; index<11; 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;
}

it works fine when i debug (i use ms visual C++). But when i use the build version, it always "encounters a problem" and has to close. If someone knows why this is happening, that would be great.

Thanks in advance

what "problem" does it say it's having? Copy the error log and post it here so that we can see it.

no, you dont understand

it compiles.

but when i run the build version, it either can open the file, or a window pops up saying


SRL 3.81 to SRL 4.exe has encountered a problem and needs to close. We are sorry for the inconvenience.

>SRL 3.81 to SRL 4.exe has encountered a problem and needs to close.
Classic runtime error. It means you're probably overrunning a buffer somewhere. Run your program in debug mode and step through it until you hit the error. That'll give you a good idea of where the error is being manifested, and you can trace it back the cause from there.

i dont get error when i run it in debug mode...

>i dont get error when i run it in debug mode...
Then sprinkle debug messages everywhere and run it in release mode. Are you really this helpless or are you just trying to get us to do your troubleshooting for you?

> If someone knows why this is happening, that would be great.
I've got two pretty good ideas where the problem is (ok, 3 actually).

> while (getline(fin, line[counter]))
What stops this over stepping the array?

> for(counter=0; counter<5000; counter++)
Why 5000 and not the number of lines you read?.
Since this is the same variable, why did you even bother with counting them?

> for(index=0; index<11; index++)
How many entries are there in strtofind ?

Come to think of it, why do you even bother storing the whole file in memory at all. "Read a line, do the subs, write out the line" would work just as well in a loop.

>i dont get error when i run it in debug mode...
Then sprinkle debug messages everywhere and run it in release mode. Are you really this helpless or are you just trying to get us to do your troubleshooting for you?

i didn't get what you just said, yes i am that helpless

> If someone knows why this is happening, that would be great.
I've got two pretty good ideas where the problem is (ok, 3 actually).

> while (getline(fin, line[counter]))
What stops this over stepping the array?

> for(counter=0; counter<5000; counter++)
Why 5000 and not the number of lines you read?.
Since this is the same variable, why did you even bother with counting them?

> for(index=0; index<11; index++)
How many entries are there in strtofind ?

Come to think of it, why do you even bother storing the whole file in memory at all. "Read a line, do the subs, write out the line" would work just as well in a loop.

>What stops this over stepping the array?
huh?
>Why 5000 and not the number of lines you read?.
>Since this is the same variable, why did you even bother with counting them?
already fixed it, but im trying to use new and delete for the array of line, but running through some problems :S

>How many entries are there in strtofind ?
9, dk why i put 11...

>Come to think of it, why do you even bother storing the whole file in memory at all. "Read a line, do
> the subs, write out the line" would work just as well in a loop.
well, i think that if you open a ofstream, everything else in the file is deleted, am i correct?

ill post the problem with new and delete on another thread in a little bit, my moms guna kill me :(
(i need to do hw)

> >What stops this over stepping the array?
> huh?
What happens if the file has more than 5000 lines?

> but im trying to use new and delete for the array of line
Consider using a std::vector then, and save yourself a lot of confusion.
Besides, you should concentrate on getting this one to work rather than trying to rewrite it whilst it still has bugs.

> well, i think that if you open a ofstream, everything else in the file is deleted, am i correct?
Personally, I would create a temp file, then only replace the old file when I know the new file is created successfully.
If for some reason your program crashed soon after the fout.open(File); you'd lose the lot.

how do i use vectors?

temp file? i would run into the same problem, wouldn't i?

> how do i use vectors?

std::vector<std::string> lines;
std::string aLine;
while ( getline(fin, aLine) ) lines.push_back(aLine);

You can then use lines.size() and a for loop to iterate over the vector, exactly like you would do using an array.


> temp file? i would run into the same problem, wouldn't i?
No.
You read from the input file, and write to the temp file.
You then close both files.
You then delete the original input file.
You then rename the temp file back to the original file.
The only danger is that you MAY end up with all your data in the temp file, but you will never outright lose all your data.

little more info on vectors please

i dont have a book...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.