I'm trying to make encryption program but when i run the program it gives me this error:


Debug Assertion Failed!

Program:...nts\visual studio 2010\projects\encrypter\debug\encrypter.exe

File:c:\program files\microsoft visual studio 10.0\vc\include\xstring

Line:1441

Expression: string subscript out of range

For information on how your program can cause assertion failure, see the Visual C++ documentation on asserts.

#include <iostream>
#include <stdlib.h>
#include <sstream>
#include <string>
#include "windows.h"
using namespace std;
void encrypt(string words);


int main()
{   
	string Emptywords; 
	cout << "What shall i encrypt?" << endl;
	cin >> Emptywords;
	encrypt(Emptywords);  // calling encryption funtion and passing  user input

}



void encrypt(string words)
{
	char A [100];  //char array for letters that user typed in
	char B [100];  //char array for encrypted letters
	string C;      //string what will contain in the end encrypted message

	for (int n=0;n<=99 ;)  // this for loop is going to encrypt the letters 
	{			
		A[n] = words[n];   // words[n] = checking for what letters user entered
		switch( A[n] )     // switch statement to encrypt the letter that a has
		{
		case 'a':
			B[n] = 'c';
			break;
		case 'b':
			B[n] = 'd';
			break;
		case 'c':
			B[n] = 'e';
			break;
		case 'd':
			B[n] = 'g';
			break;
		case 'e':
			B[n] = 'j';
			break;
                case 'f':
			B[n] = 'l';
			break;
		case 'g':
			B[n] = 'รถ';
			break;
		case 'h':
			B[n] = 'a';
			break;
		case 'i':
			B[n] = 'k';
			break;
		case 'j':
			B[n] = 'y';
			break;
		}
		n++; // increasing n by 1
	}




		for(int n = 0; n <= 10  ;) // for loop that will but the message inside C
	{
		stringstream ss;
		string encrypt;
		ss << B[n];
		n++;
		if(n = 99)
		{
			ss >> encrypt;
			string C = encrypt;
		}
         n++; // increasing n by 1
	}

   cout << C << endl; // showing the result to the user

	Sleep(5000);

}

The error message comes right after i type in couple letters and press enter and yes i have checked that they are all letters that the program knows

is there a rule that im not aware?

i have tried to figure out what is the problem with out any success for 3 hours now

Pardon my imperfect english.
i wish someone who is wiser than me could tell me what am i doing wrong
thanks in advance

>> for (int n=0;n<=99 ;)

What makes you think there will be 99 characters in the string? You should use this: for(int n = 0; n < words.size(); ) Also not that you should use <, not <=

>> for (int n=0;n<=99 ;)

What makes you think there will be 99 characters in the string? You should use this: for(int n = 0; n < words.size(); ) Also not that you should use <, not <=

thank you it worked

I'm guessing this is a homework assignment?

I'm guessing this is a homework assignment?

I wish it would be.. well perhaps in future... but for now I'm only programming for fun:twisted:

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.