Program not exiting. Prompts to end now

I have a working program. But for some reason I cannot exit properly. I keep getting warning codes. I know i'm just missing something easy from working on this for so long.
These are the error codes i'm getting:

Unhandled exception at 0x104cee04 in languageTranslator.exe: 0xC0000005: Access violation writing location 0x108e6bf0.

There is no source code available for the current location.

As well as an End Now prompt for my solution.

I have both ways I have tried to get out. the comments out was other option I thought would work. This is the end of my do-if-else.
Thank you again!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
		else 
			cout << "Thank you for using Kyle Cutler's translation system! " << endl;
		string reply;
		cout << "Press q to quit: ";
		cin >> reply;

	} while (n==1 || n==2);
}

void loadArrays() {	
	int i = 0;
	while (dataIn >> EnglishArray[i] >> FrenchArray[i]) {			
		i = i + 1;
	}
}
//void endProgram() {
//	string reply;
//	cout << "Press q to quit: ";
//	cin >> reply;
//} 
Have you tried a call to exit?
exit(0);
Where might I exit? Im not sure where to apply it
I have tried this with no good result.
How might I go about this? I am still very new to this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
		else 
			cout << "Thank you for using Kyle Cutler's translation system! " << endl;
		Sleep(2000); 
		exit(0);
	/*	string reply;
		cout << "Press q to quit: ";
		cin >> reply;
		exit(0);*/

	} while (n==1 || n==2);
}

void loadArrays() {	
	int i = 0;
	while (dataIn >> EnglishArray[i] >> FrenchArray[i]) {			
		i = i + 1;
	}
}
//void endProgram() {
//Sleep(2000); 
//} 
Forget about the exit(0).

The problem lies in line 15 of the above code. Here you are reading into EnglishArray and FrenchArray over and over. There is no guarantee that you'll stop reading before hitting the end of the array. In fact, I don't think it'll ever stop reading guaranteeing that you'll hit the end of the array.

I'm guessing that dataIn is a std::ifstream object.

Replace that while loop from lines 14 to 17 with this:

1
2
for( int i = 0; dataIn.good(); ++i) // Checks if there is an end of file or error
    dataIn >> EnglishArray[i] >> FrenchArray[i]; // Reads the data 


You may also want to check if the file is too big to fit into the array. If your arrays are defined like so:
1
2
std::string EnglishArray[256];
std::string FrenchArray[256];


Then make your code like so:
1
2
for( int i = 0; dataIn.good() && i < 256; ++i) // Checks if there is an end of file or error
    dataIn >> EnglishArray[i] >> FrenchArray[i]; // Reads the data 
Last edited on
Topic archived. No new replies allowed.