If you grab this code and run it, you'll see what I mean. The second half doesn't run, and when I can get it to run, it's all completely wrong. I'm trying, but I'm tired and just want to get it done...lol. The encrypt and decrypt are a separate cpp file, but I'll post them here as well.

#include "freefun.h"
using namespace std;


int main()
{
	ofstream streamout;
	ifstream streamin;
	char infile[16];
	char instring[100];
	int i=0;
	
	
	cout << "Please input the filename you would like to open up to 15 characters." << endl;
	cin >> infile;
	streamout.open (infile);

	if (!streamout)
		{
			cout << "Unable to open file: " << infile << endl;
			exit (1);
		}
	else
		{
			cout << "File " << infile << " opened." << endl;
		}
	while (i <=3)
		{	i++;
			cout << "Please enter your text." << endl;
			cin >>instring;
			encrypt(instring,5);
			cout << "You entered: "<< instring << endl;
			streamout<<instring<<endl;
		}
		
	cout << instring << endl;

	streamout.close();


	streamin.open(infile);
	
	if (!streamin)
		{
			cout << "Unable to open file: " << infile << endl;
			exit (1);
		}
		else
		{
			cout << "File " << infile << " opened." << endl;
		}

		while (i <=3)
			{	i++;
				cout << "Please enter your text." << endl;
				cin >>instring;
				encrypt(instring,5);
				cout << "You entered: "<< instring << endl;
				streamout<<instring<<endl;
				decrypt(instring,5);
				cout << "You entered: "<< instring << endl;

			}

		streamin.close();

return 0;

}

encrypt and decrypt

void encrypt(char *s,int n)//simple encryption method
{
	for(int i=0;s[i]!=0;i++)
	{
		s[i] += n;
	}
}


void decrypt(char *s,int n)
{
	for(int i=0;s[i]!=0;i--)
	{
		s[i] += n;
	}
}

Recommended Answers

All 6 Replies

#include "freefun.h"
using namespace std;


int main()
{
	ofstream streamout;
	ifstream streamin;
	char infile[16];
	char instring[100];
	int i=0;
	
	
	cout << "Please input the filename you would like to open up to 15 characters." << endl;
	cin >> infile;
	streamout.open (infile);

	if (!streamout)
		{
			cout << "Unable to open file: " << infile << endl;
			exit (1);
		}
	else
		{
			cout << "File " << infile << " opened." << endl;
		}
	while (i <=3)
		{	i++;
			cout << "Please enter your text." << endl;
			cin >>instring;
			encrypt(instring,5);
			cout << "You entered: "<< instring << endl;
			streamout<<instring<<endl;
		}
		
	cout << instring << endl;

	streamout.close();


	streamin.open(infile);
	
	if (!streamin)
		{
			cout << "Unable to open file: " << infile << endl;
			exit (1);
		}
		else
		{
			cout << "File " << infile << " opened." << endl;
		}

		while (i <=3)
			{	i++;
				cout << "Please enter your text." << endl;
				cin >>instring;
				encrypt(instring,5);
				cout << "You entered: "<< instring << endl;
				streamout<<instring<<endl;
				decrypt(instring,5);
				cout << "You entered: "<< instring << endl;

			}

		streamin.close();

return 0;

}

You'll never execute lines 54 - 63. By line 53, i equals 4, so the condition on line 53 will never be true. Lines 54 through 63 are therefore skipped. You need to re-initialize i after line 34 if you want lines 54 through 63 to execute.

Re-initialized i back to 0, but there's something wrong with the decryption, and it wants to abort at the end with some kind of heap corruption error.

The symptom of the decryption is that it is not decrypting the first letter. For example, if you type in today, it encrypts fine, but when you decrypt, it prints yoday.

Do I have something not allocated correctly?

Re-initialized i back to 0, but there's something wrong with the decryption, and it aborts at the end with a corruption error.

Is there something wrong with the way I'm decrypting it?

void decrypt(char *s,int n)
{
	for(int i=0;s[i]!=0;i--)
	{
		s[i] += n;
	}
}

Line 3: i is initialized as 0. The next time through this loop, i is decremented by i-- , so i = -1. So in lines 3 and 5 you are trying to access an array element with a negative index (-1).

bah!....what a dork, thank you vernon

The other is a run time check failure #2 which deals with the stack, not the heap, my mistake. stack around "instring" corrupted

bah!....what a dork, thank you vernon

The other is a run time check failure #2 which deals with the stack, not the heap, my mistake. stack around "instring" corrupted

I don't get a run time error. Where do you get it?

I'm not sure why you're not getting it, maybe we're using different compilers, but I found it, it was in my decryption (again).

I changed i back to 0, made i increment, and then changed the +=n to -=n and all worked just fine.

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.