You forgot the +/- 'a' bit of the code to make 0..25 into 'a' to 'z'
You forgot the +/- 'a' bit of the code to make 0..25 into 'a' to 'z'
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
KK i finally understand all of this after playing around with it a bit however when i still try to decrypt this just prints the right amount of characters this time except it prints not "luke" like it should do.Anyway here's the code i was hoping someone could have a look at it and tell me whats the matter with the decrypt part as i thought it would work as it is similar to what Sifried showed me:
Code:#include <iostream> #include <fstream> using namespace std; int main() { string cyphertext; string plainchar = "luke"; int shift = 2; int alphabet_size = plainchar.length(); for (int i = 0; i < alphabet_size; ++i) { cyphertext += ( ( (plainchar[i] - 'a') + shift) %26) + 'a'; plainchar += ( ( (plainchar[i] + 'a') - shift) %26) - 'a'; } cout<< "Encrypted: " << cyphertext <<endl; cin.get(); cout<< "Decrypted: " << plainchar <<endl; cin.get(); return 0; }
*sigh* One last time...
Decompose the long expressions and you will be able to test it yourself!
Originally Posted by brewbuck:
Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.
decompose definiton = to decay, putrefy.(lol) look i have tried this before what makes you think that it will work this time?
Done it sorry 4 the arigance but it works except i cant remember how to flush a string?
orCode:std::cout << "flush me" << std::flush; //just flushes
orCode:std::cout << "flush me" << std::endl; //outputs a newline and flushes
orCode:std::cout << "flush me" << std::ends; //outputs a null and flushes
orCode:std::cout << "flush me"; std::cin >> val; //flushes cout by calling cin
orCode:int main() { std:: cout << "flush me"; return 0; } //flushes by function termination
Code:pull the lever or push the button
If you don't understand mathematical terms, I suggest you move out from cyphers and do something else in your code.Originally Posted by L_U_K_E
Originally Posted by brewbuck:
Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.
I sorted it anyway by just creating a new string and using that thanx for all of your help.Here is my code:
However I am still having trouble when it comes to capitals, spaces and the letters 'a' and 'z' is that because i havn't linked it round?Code:#include <iostream> #include <fstream> using namespace std; int main() { string cyphertext; string plainchar = "luke"; string plainchar2; int shift = 2; int alphabet_size = plainchar.length(); for (int i = 0; i < alphabet_size; ++i) { cyphertext += ( ( (plainchar[i] - 'a') + shift) %26) + 'a'; plainchar2 += cyphertext[i] - shift; } cout<< "Encrypted: " << cyphertext <<endl; cin.get(); cout<< "Decrypted: " << plainchar2 <<endl; cin.get(); return 0; }
Last edited by L_U_K_E; 06-28-2006 at 08:18 AM.
You don't need the fstream header.
Place a cin.ignore() before your last cin.get(). If you input test on your first cin.get(), the second will not execute as you expected.
It's also probably better you get used to replace the less-than operator in your for loops with the not-equal one. It's more intuitive. Although not necessarily for all situations.
As a curiosity, if you want to have the actual caeser's cypher, shift by 3, not 2. That's how he did it. Or shift by 13 to produce the ROT13 cypher made common in signatures and such.
Originally Posted by brewbuck:
Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.
Thanx for all your help and i ahve made those changes hopwever i get problems if i change "luke" to numbers or the letters 'a' or 'z' is that because i havn't linked it round?
Time you try to do it yourself... give it a shot
Originally Posted by brewbuck:
Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.
No, the problem is that your entire implementation relies on the assumption that there won't be anything but lower-case alphabetic characters in the string. It's the -'a' and +'a' parts.
To do this properly, you have to treat each character according to its class: differ between upper-case, lower-case, digits and special characters, changing the class shift for the classes and leaving specials alone.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
So do you mean that instead of typing:
I would type:Code:cyphertext += ( ( (plainchar[i] - 'a') + shift) %26) + 'a';
For numbers or is that like completely wrong?Code:cyphertext += ( ( (plainchar[i] - '1') + shift) %9) + '1';
'0', but the principle is right. Now you just need to find a way to abstract the common code out of it.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
what do you mean by 'abstract the common code out of it'?(sorry about this but by asking questions it makes it easier to learn and not make the same mistakes again).
Well, you could repeat the cipher code for every class of characters.
Or you can find a way to write the common code once in a way that allows you to reuse it.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law