DEBUG ASSERTION FAILED!

Write your question here.
Why do I get the DEBUG ASSERTION FAILED message (string substring out or range)?
The program quits after encrypting the second character.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
  /*
*	ceasar
*
*	Get the key
*	Get the plaintext 
*	Encipher the text
		One character
		Entire plaintext
		isapha
		isupper
		islower
*	Print cipher text message
*	Start: ASCII values
*	Encipher: Alphabetical index
*	Print: ASCII values
*
*	c[i] = (p[i] + k) % 26
*
*/

#include <cstdio>	// printf_s scanf_s
#include <cstdlib>  // atoi
#include <cctype>	// isupper, islower
#include <iostream> // cin, cout
#include <string>	// getline

using namespace std;

int main(int argc, char* argv[])
{
	// error check
	if (argc != 2)
	{
		printf_s("Illegal Usage: ./ascimath key");
		return 1;
	}
	// key is the second command line argument and is converted from char to integer
	int key = atoi(argv[1]);
	// variable
	string cstr ("This is C++!");
	string encrpt;
	int incr;
	int len = cstr.length();

	// Get message from user
//	printf_s("Enter the message: ");
//	printf_s("%i\n", cstr);
//	getline(cin, cstr);

	for (incr = 0; incr < len; incr++)
	{
		cout << "working character: " << cstr[incr] << endl;

		// encrypt message
		if (isalpha(cstr[incr]))
		{
			// Work with lower case
			if (islower(cstr[incr]))
			{
				printf_s("incr is %i\n", incr);
				int x = ((cstr[incr] - 'a') + key) % 26;
				printf_s("x in islower is: %i\n", x);
				encrpt[incr] = x + 'a';
			}
			else
			{
				printf_s("incr is %i\n", incr);
				// change ASCII value to encrypt value
				int x = ((cstr[incr] - 'A') + key) % 26;
				printf_s("x in isupper is: %i\n", x);
				encrpt[incr] = x + 'A';
			}
		}
		else
		{
			encrpt[incr] = cstr[incr];
			printf_s("incr is %i\n", incr);
		}
	printf_s("incr back to while is %i\n", incr);
	}

// Print encrytion.
	printf_s("Encrypted string is: ");
	for (int i = 0; i < len; i++)
	{
		printf_s("%c", encrpt[i]);
	}
	printf_s("\n");

// Print decrytion.
	printf_s("Decrypted string is: ");
	for (int i = 0; i < len; i++)
	{
		printf_s("%c", cstr[i]);
	}
	printf_s("\n");
}




working character: T
incr is 0
x in isupper is: 21
incr back to while is
working character: h
incr is 1
x in islower is: 9
The length of encrypt string is zero on creation. You need to append the new character while adding to the string.

encrypt.append( x+'A' );

Otherwise, you will be trying to access out of bounds string.
Thank you. However, instead of using append(), I used push_back(). I found this in another post: append() basically takes a string-like object and appends a copy of it to the current std::string. If you want to add a character, just use std::string::push_back().

Now, I am trying to make the program more general, where the user enters the message. I have a problem implementing the getline function. Here is the part I am having problem with:

1
2
3
4
// Get message from user
	printf_s("Enter the message: ");	
	getline(cin, cstr, '\n');				// PROBLEM IS HERE.  HOW COME NOT ACCEPTING STRING?
	printf_s("cstr: %c\n", cstr);


Here is my output:

Enter the message: This is C++!
cstr: Ç
Encrypted string is:
Decrypted string is:
Press any key to continue . . .


I got the program working. I moved the calculation of the string length after the input and changed the syntax of getline. The code is below. Thank you for your help.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
*	ceasar
*
*	Get the key
*	Get the plaintext 
*	Encipher the text
		One character
		Entire plaintext
		isapha
		isupper
		islower
*	Print cipher text message
*	Start: ASCII values
*	Encipher: Alphabetical index
*	Print: ASCII values
*
*	c[i] = (p[i] + k) % 26
*
*/

#include <cstdio>	// printf_s scanf_s
#include <cstdlib>  // atoi
#include <cctype>	// isupper, islower
#include <iostream> // cin, cout
#include <string>	// getline

using namespace std;

int main(int argc, char* argv[])
{
	// error check
	if (argc != 2)
	{
		printf_s("Illegal Usage: ./ascimath key");
		return 1;
	}
	// key is the second command line argument and is converted from char to integer
	int key = atoi(argv[1]);
	// variable
	string cstr;
	string encrpt;
	int incr;
	
	// Get message from user
	printf_s("Enter the message: ");	
	getline(cin, cstr);				
	int len = cstr.length();

	for (incr = 0; incr < len; incr++)
	{
		// encrypt message
		if (isalpha(cstr[incr]))
		{
			// Work with lower case
			if (islower(cstr[incr]))
			{
				int x = ((cstr[incr] - 'a') + key) % 26;
				encrpt.push_back(x + 'a');
			}
			else
			{
				// change ASCII value to encrypt value
				int x = ((cstr[incr] - 'A') + key) % 26;
				encrpt.push_back(x + 'A');
			}
		}
		else
		{
			encrpt.push_back(cstr[incr]);
		}
	}

// Print encrytion.
	printf_s("Encrypted string is: ");
	for (int i = 0; i < len; i++)
	{
		printf_s("%c", encrpt[i]);
	}
	printf_s("\n");

// Print decrytion.
	printf_s("Decrypted string is: ");
	for (int i = 0; i < len; i++)
	{
		printf_s("%c", cstr[i]);
	}
	printf_s("\n");
}
You're welcome! Glad you got it working.
Topic archived. No new replies allowed.