system error when program finishes
Hallo.
I have written a simple program to encrypt text. It compiles and works, but after doing the operations it creates a system error.
"this program has made a illegal error and will now be closed" you all know it I guess:p
I am using windows XP sp 2
any ideas why is causing it?
thanks
Code:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
string getInput();
string writeOutput(string output);
string encrypt(int seedVal);
string decrypt(int seedVal);
int main()
{
int option;
//small menu for choosing what to do
cout << "<<Welcome to Oles Encrypter 1.0>>\n\n\n";
cout <<"What would you do: \n";
cout <<" 1 - Encrypt text \n";
cout <<" 2 - Decrypt text \n";
cout <<" 3 - Read file code.text\n\n";
cout <<"Please enter your chooise: ";
do
{
cin >> option;
if (option == 1)
{
cout << "your text is encrypted now\n\n";
encrypt(10);
break;
}
if (option == 2)
{
cout <<"your text is now decrypted, happy reading\n\n";
decrypt(10);
break;
}
if (option == 3)
{
cout << getInput();
break;
}
else
{
cout <<"You entered a not valid option. Plase pick a valid option:\n";
cin >> option;
}
} while (option != 1 || option != 2);
//End the program
system ("PAUSE");
return 0;
}
//write to file
string writeOutput(string output)
{
string write = output;
ofstream myfile ("code2.txt");
if (myfile.is_open())
{
myfile << write;
myfile.close();
}
}
//get input from file
string getInput()
{
string input;
string tempInput = "";
//get input from file
ifstream myFile ("code.txt");
if (myFile.is_open())
{
while (! myFile.eof())
{
getline(myFile, tempInput);
input += tempInput + "\n";
}
myFile.close();
}
return input;
}
string encrypt(int seedVal)
{
//encrypt the string by adding a random number to it
string input = getInput();
string tempInput = "";
string output = "";
char tempChar;
char newChar;
int seedNo = seedVal;
int randNo;
int charNo;
int encryptedNo;
//loop troug the string and create an encrypted version
for (int x = 0; x < input.size(); x++)
{
tempChar = input[x];
srand(seedNo);
randNo = rand() % 10;
charNo = tempChar;
encryptedNo = charNo + randNo;
newChar = encryptedNo;
//add the new number to the output string
output += newChar;
}
//write the output to a file
writeOutput(output);
return output;
}
string decrypt(int seedVal)
{
//encrypt the string by adding a random number to it
string input = getInput();
string tempInput = "";
string output = "";
char tempChar;
char newChar;
int seedNo = seedVal;
int randNo;
int charNo;
int encryptedNo;
//loop troug the string and create an encrypted version
for (int x = 0; x < input.size(); x++)
{
tempChar = input[x];
srand(seedNo);
randNo = rand() % 10;
charNo = tempChar;
encryptedNo = charNo - randNo;
newChar = encryptedNo;
//add the new number to the output string
output += newChar;
}
//write the output to a file
writeOutput(output);
return output;
}