Basic encryption help

So I've been working on a mini project that turns a phrase into a binary Like sequence but it has some major issues though
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>
Using namespace std;

Int main() {
 String phrase;
 String output;
 Char library[26] { a,b,c ..shortened..y,z);
 Int bin[26] {1,10,11,101.. 110010,110011};
 Int var1;
 Int var2;
 Cout << "enter in your encryption phrase" <<endl;
 ;cin >> phrase;
 For (var1=0; phrase[var1]; var1++) {
  For (var2=0; library[var2];var2++) {
   While (phrase[var1] == library[var2]) {
    Output[var1] = bin[var2]
   }
  }
 }
}


It has some major flaws but I seem to be unable to put my finger on it
Any help is greatly appreciated
A problem is that the output string is empty when you try to write to it using indices on line 17.
Not sure if this is just from some mistake while copy-pasting, but if you're using a document writer like MS Word or OpenOffice Writer, etc, well, don't do that. You don't want it auto-correcting every other phrase in your sentence.

"Int" means nothing, "int" is type integer. "While (...)" means nothing "while (...)" signifies a while loop. You should at least use a program like Notepad, or Notepad++, assuming Windows, or an IDE.

Second, a is the name of a possible variable. 'a' is a character.
Char library[26] { a,b,c...};
should be
char library[26] { 'a', 'b', 'c'...};

Also, you should look up how to correct do a for loop, your condition is most likely not what you want. And as Peter said, you would go over the bounds of your output string, I would instead use the += operator, and get rid of the [] for the string.
Last edited on
Okay, I'll start you off. Look at the while loop condition first, look at both variable types for a clue. Next, figure out how you're going to display the string.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>
using namespace std;

int main() {
	string phrase;
	string output;
	char library[26] { 'a', 'b', 'c', 'y', 'z'};
	int bin[26] {1, 10, 11, 101, 110010, 110011};
	int var1;
	int var2;
	cout << "enter in your encryption phrase" << endl;
	cin >> phrase;
	for (var1 = 0; phrase[var1]; var1++) {
		for (var2 = 0; library[var2]; var2++) {
			while (phrase[var1] == library[var2]) {
				output[var1] = bin[var2];
			}
		}
	}
}


Last edited on

"Int" means nothing, "int" is type integer. "While (...)" means nothing "while (...)" signifies a while loop. You should at least use a program like Notepad, or Notepad++, assuming Windows, or an IDE.

he/she probably submit this question from a cell phone
I submitted it from a phone because I was in a rush
I forgot to add at the end it prints the strong output so you can see the encryption and The capitals are because of that
I'll fix it using your advice
Thank you

To print it I had the idea of just doing

1
2
 
cout << output <<endl;
Last edited on
while (phrase[var1] == library[var2]) { isn't what you think. You're using string objects then trying to compare a char value. This would work if you used c-style strings though.
Topic archived. No new replies allowed.