User:Barrraka/Pastes

Pastes
\#1


 * 1) include
 * 2) include
 * 3) include

using namespace std;
 * 1) define FILENAME "save.txt"

int main(int argc, char* argv[]) {	string name, age, username; ofstream file(FILENAME, ios_base::app); cout << "Name: "; cin >> name; cout << "Age: "; cin >> age; cout << "Username: "; cin >> username; string output = "Your name is " + name + ", you are " + age + " years old, and your username is " + username; if(file.is_open) file << output << endl; else { cout << "Could not write to file " << FILENAME << endl; return 1; }	cout << output << endl; file.close;

return 0; }

\#3
 * 1) include
 * 2) include
 * 3) include
 * 4) include
 * 5) include
 * 6) include


 * 1) define DEC_FILENAME "decrypted.txt"
 * 2) define ENC_FILENAME "encrypted.txt"

using namespace std;

int main(int argc, char* argv[]) {	bool decrypt = false, help = true;

if(argc == 2) {		if(string(argv[1]) == "-d") {						decrypt = true; help = false; }			}	else if(argc == 1) help = false;

if(help) {		cout << "Usage: \t" << argv[0] << " [-d][ decryption mode ]\n" << endl; cout << "\t - place decrypted text n \'decrypted.txt\',\n\t  the result will be saved in \'encrypted.txt\' in format [  ]" << endl; cout << "\t - place encrypted text n \'encrypted.txt\' [ ],\n\t   the result will be saved in \'decrypted.txt\'" << endl; return 1; }

fstream dec_file(DEC_FILENAME); fstream enc_file(ENC_FILENAME); fstream* in; fstream* out;

if(!dec_file.is_open || !enc_file.is_open) {		cout << "Cannot open files for read/write operations!" << endl; return 1; }

srand ((unsigned int)time(NULL)); char factor = 1; int key; char c;

if(decrypt) {				factor = -1; in = &enc_file; out = &dec_file;

enc_file >> key; cout << "Decrypting with key: " << key << endl; enc_file.get(c);  // space }	else {				in = &dec_file; out = &enc_file; key = rand % 24 + 1; // 1-25 cout << "Encrypting with key: " << key << endl; }

out->close; out->open((decrypt ? DEC_FILENAME : ENC_FILENAME), ios_base::trunc | ios_base::out); if(!decrypt) *out << key << " "; while(in->get(c)) {			c = (char)toupper(c);

if(c == ' ' || c == '\n') *out<<(c); else *out<<((char)(( c - (factor * 'A') + (factor*key))%26 + 'A')); }

cout << "Done." << endl; enc_file.close; dec_file.close;

return 0; }

\#5
 * 1) include
 * 2) include
 * 3) include

using namespace std;

const string psswd_file = "passwd.txt";

int main(int argc, char* argv[]) {

string user, passwd, given_username, given_password; ifstream file(psswd_file); if(!file.is_open) { cout << "Could not open file: " << psswd_file << endl; return 1; }

file >> user; file >> passwd;

cout << "Enter username: ";	cin >> given_username; cout << "Enter password: "; cin >> given_password;

if(given_username == user && given_password == passwd) cout << "Welcome!" << endl; else cout << "Access denied!" << endl; return 0; }

\#7
 * 1) include
 * 2) include
 * 3) include
 * 4) include

using namespace std;

map morse_map; string filename = "code.txt";

void init_map { morse_map[".-"] = 'A'; morse_map["-..."] = 'B'; morse_map["-.-."] = 'C'; morse_map["-.."] = 'D'; morse_map["."] = 'E'; morse_map["..-."] = 'F'; morse_map["--."] = 'G'; morse_map["...."] = 'H'; morse_map[".."] = 'I'; morse_map[".---"] = 'J'; morse_map["-.-"] = 'K'; morse_map[".-.."] = 'L'; morse_map["--"] = 'M'; morse_map["-."] = 'N'; morse_map["---"] = 'O'; morse_map[".--."] = 'P'; morse_map["--.-"] = 'Q'; morse_map[".-."] = 'R'; morse_map["..."] = 'S'; morse_map["-"] = 'T'; morse_map["..-"] = 'U'; morse_map["...-"] = 'V'; morse_map[".--"] = 'W'; morse_map["-..-"] = 'X'; morse_map["-.--"] = 'Y'; morse_map["--.."] = 'Z'; morse_map["/"] = ' '; }

char get_morse_letter(ifstream& file) { if(file.eof) return (char)0; string tmp;	file >> tmp; return morse_map[tmp]; }

int main(int argc, char* argv[]) {

ifstream file; char c;

if(argc == 2) filename = argv[1]; else if(argc != 1)	{ cout << "Usage: " << argv[0] << " [filename] \n\n\t- [filename] defaults to code.txt\n\t- use \'/\' for spaces" << endl; return 1; }

file.open(filename, ios_base::in);

if(!file.is_open) { cout << "Could not open file: " << filename << endl; return 1; }

init_map;

cout << "Decoded message: "; while(c = get_morse_letter(file)) cout << c;	cout << endl;

file.close; return 0; }