Issues with encription

I have this code that scrambles a given char array and gives a code to unscramble it. There are 2 issues:
- the encryption bugs out with 10 chars
- it only reads 10 chars

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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include <iomanip>
#include <string>
#include <string.h>
#include <iostream>
#include <conio.h>
#include <windows.h>
#include <cstdlib>
#include <fstream>
#include <bits/stdc++.h>
using namespace std;

HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE); // used for goto
COORD CursorPosition; // used for goto

void gotoXY(int,int); // function defined below if this is new to you.

int main()
{
	int menu_item=0, run, x=7,sub_item=0;
	bool running = true;
	int i,z=0,r,calc,strl,enstrl;
	long long int rtotal=0;
    char str[1000],ch[1000],enstr[1000],plus[1000];
    long double a,b;
    string test,agh;
    ifstream afile;
    srand (time(NULL));
	gotoXY(18,5); cout << "Main Menu";
	gotoXY(18,7); cout << "->";
	while(running)
	{
		gotoXY(20,7);  cout << "1) Encrypt";
		gotoXY(20,8);  cout << "2) Decrypt";
		gotoXY(20,9); cout << "Quit Program";
		system("pause>nul"); // the >nul bit causes it the print no message
		if(GetAsyncKeyState(VK_DOWN) && x != 9) //down button pressed
			{
				gotoXY(18,x); cout << "  ";
				x++;
				gotoXY(18,x); cout << "->";
				menu_item++;
				continue;
				
			}
			
		if(GetAsyncKeyState(VK_UP) && x != 7) //up button pressed
			{
				gotoXY(18,x); cout << "  ";
				x--;
				gotoXY(18,x); cout << "->";
				menu_item--;
				continue;
			}
		if(GetAsyncKeyState(VK_RETURN)){ // Enter key pressed
		rtotal=0;
		sub_item=0;
		strl = 0;
			switch(menu_item){
				case 0: {
					system("cls");
					cout << "Please enter a string:\t";
					scanf(" %[^\n]s",str);
					strl = strlen(str);
					
					for(i = 0; i < strl; i++){
            			if(str[i]==' '){
            				str[i]='_';	
						}
						ch[i]=str[i];
         			}
					if(strl>9){
						for(i = 10; i < strl; i++){
            				plus[i]=str[i];
            				str[i]='\0';
						}
					}
					cout << strl << endl;
         			for(i = 0;i < strl; i++){
            			rtotal=rtotal*10;
            			r= rand() % 9 +1;
            			str[i] = str[i] - r;
            			rtotal=rtotal+r;
					}
					for(i = 0;i < strl; i++){
            			enstr[i]=str[i];
         			}
         			if(strl>10){
						for(i = 10;i < strl; i++){
            			rtotal=rtotal*10;
            			r= rand() % 9 +1;
            			plus[i] = plus[i] - r;
            			rtotal=rtotal+r;
					}
					for(i = 9;i < strl; i++){
            			enstr[i]=plus[i];
         			}
					}
        			a=rtotal;
        			while (a>10){
					a=a/10;
					}
         			for(i = 0;i <strl; i++){
						str[i] = str[i] + trunc(a);
						a=a-trunc(a);
						a=a*10;
						if(int(str[i])==int(ch[i])){
							cout << str[i] << " checks out with " << ch[i] << "\n" << endl;
						}else{
							cout << str[i] << " misses "<< ch[i] <<" by " << (int(ch[i])-int(str[i])) << "\n" << endl;
							enstr[i]=enstr[i]+(int(ch[i])-int(str[i]));		
						}
					}
					for(i = 10;i <strl; i++){
						plus[i] = plus[i] + trunc(a);
						a=a-trunc(a);
						a=a*10;
						if(int(plus[i])==int(ch[i])){
							cout << plus[i] << " checks out with " << ch[i] << "\n" << endl;
						}else{
							cout << plus[i] << " misses "<< ch[i] <<" by " << (int(plus[i])-int(str[i])) << "\n" << endl;
							enstr[i]=enstr[i]+(int(ch[i])-int(plus[i]));		
						}
					}
					enstrl = strlen(enstr);
					for(i = strl;i < enstrl; i++){
            			enstr[i]='\0';
            		}
        			cout << "\nEncrypted string: " << enstr << endl;
         			cout << "\nEncryption key: " << rtotal << endl;
         			ofstream afile;
 		 			afile.open (".str");
 		 			afile << enstr <<" "<< rtotal ;
 		 			afile.close();
 		 			for(i = 0;i < strl; i++){
            			enstr[i]='\0';
					}
 		 			system("pause");
 		 			system("cls");
 		 			gotoXY(18,5); cout << "Main Menu";
					gotoXY(18,7); cout << "->";
					break;
				}
				case 1: {
					system("cls");
					ifstream afile (".str");
 					afile >> test >> a;
					int TempNumOne=test.size();
					for (int d=0;d<=TempNumOne;d++){
        				str[d]=test[d];
       				}
					cout << "\nEncrypted string: " << str <<endl;
					cout << "\nEncryption key: " << a <<endl; 
 					strl = strlen(str);
      				while (a>10){
						a=a/10;
					}
       				  for(i = 0;i < strl; i++){
					str[i] = str[i] + trunc(a);
					a=a-trunc(a);
					a=a*10;
					}
					for(i = 0; i < strl; i++){
            			if(str[i]=='_'){
            				str[i]=' ';	
						}
         			}
      				cout << "\nDecrypted string: " << str <<endl;
      				system("pause");
 		 			system("cls");
 		 			gotoXY(18,5); cout << "Main Menu";
					gotoXY(18,8); cout << "->";
					break;
				}	
				case 2: {
					gotoXY(20,16);
					cout << "The program has now terminated!!";
					running = false;
				}	
			}
		}			
	}
	gotoXY(20,21);
}

void gotoXY(int x, int y) 
{ 
	CursorPosition.X = x; 
	CursorPosition.Y = y; 
	SetConsoleCursorPosition(console,CursorPosition); 
}
If you posted a simple program that demonstrates the problem instead of this unholy (Window's-centric) mess you'd be more likely to get help.
Do everyone a favour and rip out all this code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
	gotoXY(20,7);  cout << "1) Encrypt";
		gotoXY(20,8);  cout << "2) Decrypt";
		gotoXY(20,9); cout << "Quit Program";
		system("pause>nul"); // the >nul bit causes it the print no message
		if(GetAsyncKeyState(VK_DOWN) && x != 9) //down button pressed
			{
				gotoXY(18,x); cout << "  ";
				x++;
				gotoXY(18,x); cout << "->";
				menu_item++;
				continue;
				
			}
			
		if(GetAsyncKeyState(VK_UP) && x != 7) //up button pressed
			{
				gotoXY(18,x); cout << "  ";
				x--;
				gotoXY(18,x); cout << "->";
				menu_item--;
				continue;
			}
		if(GetAsyncKeyState(VK_RETURN)){ // Enter key pressed 


Replace it with
1
2
3
4
cout << "1) Encrypt";
cout << "2) Decrypt";
cout << "3) Quit Program";
cin >> menu_item;


Eye candy / pretty user interface code is done LAST, not first.
All it does is distract you from solving the real problems in your code (you know, the problem that it doesn't work).

Then write some functions!
So main is little more than
1
2
3
4
5
6
7
8
9
case 0:
    doEncrypt();
    break;
case 1:
    doDecrypt();
    break;
case 2:
    running = false;
    break;

Topic archived. No new replies allowed.