c++ - Code compiles, however crashes after start(rot13) -
so, have been fiddling around code ctf competition. however, every time run actual console application, keeps crashing. please explain me why. thank in advance.
#include <iostream> #include <fstream> #include <string> using namespace std; int lowerconvert(char x) { int y; if (x == 'a') y = 1; if (x == 'b') y = 2; if (x == 'c') y = 3; if (x == 'd') y = 4; if (x == 'e') y = 5; if (x == 'f') y = 6; if (x == 'g') y = 7; if (x == 'h') y = 8; if (x == 'i') y = 9; if (x == 'j') y = 10; if (x =='k') y = 11; if (x == 'l') y = 12; if (x == 'm') y = 13; if (x == 'n') y = 14; if (x == 'o') y = 15; if (x == 'p') y = 16; if (x == 'q') y = 17; if (x == 'r') y = 18; if (x == 's') y = 19; if (x == 't') y = 20; if (x == 'u') y = 21; if (x == 'v') y = 22; if (x == 'w') y = 23; if (x == 'x') y = 24; if (x == 'y') y = 25; if (x == 'z') y = 26; return y; } int upperconvert(char x) { int y; if (x == 'a') y = 27; if (x == 'b') y = 28; if (x == 'c') y = 29; if (x == 'd') y = 30; if (x == 'e') y = 31; if (x == 'f') y = 32; if (x == 'g') y = 33; if (x == 'h') y = 34; if (x == 'i') y = 35; if (x == 'j') y = 36; if (x == 'k') y = 37; if (x == 'l') y = 38; if (x == 'm') y = 39; if (x == 'n') y = 40; if (x == 'o') y = 41; if (x == 'p') y = 42; if (x == 'q') y = 43; if (x == 'r') y = 44; if (x == 's') y = 45; if (x == 't') y = 46; if (x == 'u') y = 47; if (x == 'v') y = 48; if (x == 'w') y = 49; if (x == 'x') y = 50; if (x == 'y') y = 51; if (x == 'z') y = 52; return y; } char lowerback(int x) { char y; if (x == 1) y = 'a'; else if (x == 2) y = 'b'; else if (x == 3) y = 'c'; else if (x == 4) y = 'd'; else if (x == 5) y = 'e'; else if (x == 6) y = 'f'; else if (x == 7) y = 'g'; else if (x == 8) y = 'h'; else if (x == 9) y = 'i'; else if (x == 10) y = 'j'; else if (x == 11) y = 'k'; else if (x == 12) y = 'l'; else if (x == 13) y = 'm'; else if (x == 14) y = 'n'; else if (x == 15) y = 'o'; else if (x == 16) y = 'p'; else if (x == 17) y = 'q'; else if (x == 18) y = 'r'; else if (x == 19) y = 's'; else if (x == 20) y = 't'; else if (x == 21) y = 'u'; else if (x == 22) y = 'v'; else if (x == 23) y = 'w'; else if (x == 24) y = 'x'; else if (x == 25) y = 'y'; else if (x == 26) y = 'z'; return y; } char upperback(int x) { char y; if (x == 27) y = 'a'; if (x == 28) y = 'b'; if (x == 29) y = 'c'; if (x == 30) y = 'd'; if (x == 31) y = 'e'; if (x == 32) y = 'f'; if (x == 33) y = 'g'; if (x == 34) y = 'h'; if (x == 35) y = 'i'; if (x == 36) y = 'j'; if (x == 37) y = 'k'; if (x == 38) y = 'l'; if (x == 39) y = 'm'; if (x == 40) y = 'n'; if (x == 41) y = 'o'; if (x == 42) y = 'p'; if (x == 43) y = 'q'; if (x == 44) y = 'r'; if (x == 45) y = 's'; if (x == 46) y = 't'; if (x == 47) y = 'u'; if (x == 48) y = 'v'; if (x == 49) y = 'w'; if (x == 50) y = 'x'; if (x == 51) y = 'y'; if (x == 52) y = 'z'; return y; } void primaryrot13() { cout << "please enter name of file decrypted: "; string name; getline(cin, name); name += ".txt"; ifstream file; ofstream write; file.open(name); string message; file >> message; int converted[9999999]; char reconvert[9999999]; (int = 0; < message.length();++i) { if (message[i] == 0 || 1 || 2 || 3 || 4 || 5 || 6 || 7 || 8 || 9) message[i] += 53; if (message[i] == 'a' || 'b' || 'c' || 'd' || 'e' || 'f' || 'g' || 'h' || 'i' || 'j' || 'k' || 'l' || 'm' || 'n' || 'o' || 'p' || 'q' || 'r' || 's' || 't' || 'u' || 'v' || 'w' || 'x' || 'y' || 'z') converted[i] = lowerconvert(message[i]); converted[i] += 13; if (converted[i] > 26) converted[i] -= 26; if (message[i] == 'a' || 'b' || 'c' || 'd' || 'e' || 'f' || 'g' || 'h' || 'i' || 'j' || 'k' || 'l' || 'm' || 'n' || 'o' || 'p' || 'q' || 'r' || 's' || 't' || 'u' || 'v' || 'w' || 'x' || 'y' || 'z') converted[i] = upperconvert(message[i]); converted[i] += 13; if (converted[i] > 52) converted[i] -= 26; } (int = 0; < message.length(); ++i) { if (converted[i] == 52 || 53 || 54 || 55 || 56 || 57 || 58 || 59 || 60 || 61){ reconvert[i] = converted[i] - 53; continue; } if (converted[i] < 27){ reconvert[i] = lowerback(converted[i]); continue; } if (converted[i] < 51){ reconvert[i] = upperback(converted[i]); continue; } write.open("decode"); write << reconvert[i]; } }
edit , final solution several years later, here repost. ended doing adding desired amount character code, if greater upper limit set, subtracted 26 in order reset. ended using vector instead, did solve issue of crashing. can happily rot13 day long :) without myriad of if statements.
table lookup
first, introduction table or array ups.
given character array:
static const char letters[] = "abcdefghijklmnopqrstuvwxyz";
the index of 'a' 0, 'b' 1, ..., 'z' 25.
array can searched letter. index of letter can it's number. in case, index + 1.
example:
static const unsigned int letter_quantity = sizeof(letters) / sizeof(letters[0]); unsigned int index = 0; (index = 0; index < letter_quantity; ++i) { if (letters[i] == x) { break; } }
at end of loop statement, index
variable position of character in x
or length of array (if not found).
modulo arithmetic
modulo arithmetic, using %
operator, returns remainder. has behavior of wrapping around. can used array.
unsigned int new_char_index = index + 13; // may have gone past array. new_char_index = new_char_index % letter_quantity; // wrap around.
the new rot13 character
the converted character can found using new_char_index
index array.
char rot13 = letters[new_char_index];
covering characters
the remaining uppercase characters can added array account letters. other symbols can added also.
all without if
statements.
Comments
Post a Comment