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

Dani AI

Generated

Good back-and-forth so far: caught the loop-control problem and tracked down the memory-corruption symptom. A few practical follow-ups and hardening tips that weren’t spelled out in the thread will help avoid similar headaches.

Think of the program as two distinct phases—produce (get input, transform, write) and consume (read, reverse-transform, display). During the consume phase, read from the file stream, not from the user again, and operate on the data you read. Reusing counters or the wrong stream object between phases is a common source of logic errors that make the second half never run or do the wrong thing.

The “stack corrupted” runtime check almost always means an out-of-bounds write. Avoid fixed-size C buffers for filenames and user text; prefer std::string and std::getline so input size is managed. When doing byte arithmetic on characters, cast to unsigned char or an int for the calculation so you do not rely on implementation-defined signedness and accidental wrap-around. Make the decrypt operation the exact inverse of encrypt and test with short, edge-case, and maximum-length strings.

Use compiler warnings and runtime tools to catch problems early: enable -Wall -Wextra (and treat warnings as errors during development), and run builds under AddressSanitizer or Valgrind to locate buffer overflows and use-after-free bugs (AddressSanitizer, Valgrind). Step through a debugger and inspect the buffer contents before and after transformation to confirm every byte stays in bounds.

These practices reduce one-off fixes and make the code robust: separate the two phases, use safe string types, validate sizes, and employ sanitizers while testing.

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.