How do I have an outputs individual characters be converted into numbers and symbols

I'm doing a project for school where I have to make a password generator, I have all the RNG and input/output frame completed. The only problem I have is that I want the input characters to be converted into different characters. Example:
Input: Password
Output: P@550rd
The current code just adds RNG before and after the input, example:
Input: Password
Output: 321344Password321344
I want the final project to be like this:
Input: Password
Output: 231344P@55w0rd231344
Please help, my current code

// Password complexicator
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
//q = the word
//cant figure out how to convert letters from varaible q, using the listed char varaibles: a, b, c, e, h, o, s
//What I want is for it to output the entered word variable q, but then for it to converrt the letters in that word to the listed numbers and symbols
// Also need "RNG" to be a randomly generated number string 5-7 intergers
//Would I have to write another code for the letter conversion and RNG? or would i be able to write it all in the primary source code?
int main()
{

ofstream out_data("filename.dat");
srand(time(0));
char a, b, e, h, i, o, s;
char q[16];
int r, p;
r = rand();
for (p = 01; r <= 29427; p++)
r = rand();

a = '@';
b = '8';
e = '3';
h = '4';
i = '1';
o = '0';
s = '5';
cout << "Enter word:" << "\n";
cin >> q;
cout << "Your password is: " << "\n" << r << q << r << "\n";
; ;out_data << "Password" << "\n" << r << q << r << "\n";
cout << "Numbers generated on try " << p << "\n";
return 0;



;
}





Last edited on
You can set up a std::map<char,char> with the first template parameter corresponding to keyboard entry and the second as the secret letter:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <map>
#include <string>

std::map<char, char> myMap{{'a', '@'}, {'p', 'P'}, {'s', '5'}, {'w', 'W'}, {'o', 'O'}, {'r','r'}, {'d','d'}};
//add to this map the whole set of eligible characters;
int main()
{
    std::cout << "Enter word: \n";
    std::string line;
    getline(std::cin, line);//try 'password'
    std::string cipher{};
    for (auto& elem : line)
    {
        cipher += myMap.find(elem)->second;
    }
    std::cout << cipher << '\n';
}

To add random numbers:
1. generate random numbers (google if you need help), save in a double variable
2. use std::stringstream to convert the number(s) to std::string(s)
3. add string(s) from step 2 to the string cipher from above program
Last edited on
I had already gotten the random numbers, just forgot to remove the comments, I figured everything out, the build worked fine but when I got to the out put part I came across an error saying "Deubug Assertion Failed!
Program: C:\Widnows\SYSTEM32\MSVCp140D.dll
File: c:\program files (x86)\microsoft visual studio 14.0\vc\include\xtree
Line: 238

Expression: map/set iterator not dereferencable"

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
// Password complexicator
#include <iostream>
#include <fstream>
#include <stdio.h>     
#include <stdlib.h>     
#include <time.h>
#include <map>
#include <string>

std::map<char, char> myMap{ { 'a', '@' },{ 'b', '8' },{ 's', '$' },{ 'e', '3' },{ 'o', '0' },{ 'i','1' },{ 'h','4'},{ 'A', '@' },{ 'B', '8' },{ 'S', '$' },{ 'E', '3' },{ 'O', '0' },{ 'I','1' },{ 'H','4' } };
using namespace std;
int main()
{

	ofstream out_data("filename.dat");
	srand(time(0));
	char a, b, e, h, i, o, s;
	char q[16];
	int r, p;
	r = rand();
	for (p = 01; r <= 29427; p++)
		r = rand();


	cout << "Enter word:" << "\n";
	string line;
	getline(cin, line);
	string cipher{};
		for (auto& elem : line)
		{
			cipher += myMap.find(elem)->second;
	}
	cout << "Your password is: " << "\n" << r << cipher << r << "\n";
	; ;out_data << "Password" << "\n" << r << cipher << r << "\n";
	cout << "Numbers generated on try " << p << "\n";
	
	return 0;



	;
}


What if myMap.find() does not find the requested value? The return value is myMap.end().

1
2
3
4
5
6
7
8
9
10
    for (auto& elem : line)
    {
        auto f = myMap.find(elem);
        cipher += (f == myMap.end()) ? elem : f->second;
    }

    std::ostringstream oss;
    oss << r << cipher << r ;
    string password = oss.str();
    cout << "Your password is: \n" << password << "\n";


http://www.cplusplus.com/reference/map/map/find/
Last edited on
I was able to get it, had to change a few lines and debugged it as x64 not x86
Topic archived. No new replies allowed.