Thread: system error when program finishes

  1. #1
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485

    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

    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;    
           
    }

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    It compiled and ran fine after fixing a few warnings:
    should be void writeOutput(string)
    because it doesn't return anything.

    Code:
    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 (size_t 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;    
    }
    The red bit is a misunderstanding how rand works. Because you srand it with the same number over and over, randNo will always be the same value. If you meant it like that you could just seed and draw the random number once and use it in the loop. Otherwise, move srand out of the loop.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Otherwise, move srand out of the loop.
    Better out of the function. It shoudl be called somewhere in the main during program initialization
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    Thank you, works fine now.

    Why should I not have the srand in the function? Would it make a difference? I have moved it out of the loop, but it is still in the function

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    if you call your function twice in the program it will reset the random sequence to the same slot as the previous call. You will receive exactly the same random sequences when you enter your function again and again
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    yeah, but that is the whole point.

    when encrypting, add a random number

    when decrypting, subtract the random number.

    Or am I missunderstanding something?

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    yeah, I see your point... never thought about using rand that way...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  8. #8
    The larch
    Join Date
    May 2006
    Posts
    3,573
    yeah, but that is the whole point.

    when encrypting, add a random number

    when decrypting, subtract the random number.

    Or am I missunderstanding something?
    Yes, but I thought you wanted to shift each letter by a different value.
    If you seed the random generator before encrypting each character, the next call to rand() would return the same value over and over. You won't get a sequence of random numbers, but just one number all the time.
    If that was your intention, just call rand() once before the loop, otherwise seed the random number generator once each time you start a new encryption. (But not once in a program, because you want to manipulate the state of the RNG each time you call the encrypt/decrypt function.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 06-13-2005, 07:28 AM
  2. using system command results in a program
    By finnepower in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2005, 08:58 AM
  3. Date program starts DOS's date
    By jrahhali in forum C++ Programming
    Replies: 1
    Last Post: 11-24-2003, 05:23 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM