Reputation: 1
I'm trying to make a simple text encrypter (not all functions are complete) but I have a specific problem that I would like some help with. I stopped at this point in coding and added some commented documentation for testing:
In the function algorithm(): lines 16-25 are appending the index of std::string ck[] to std::string KEY inside the nested loop. The outside loop is running for the measure of PASSWORD.size() and checking each PASSWORD.substr(...) against all ck[].
The output, however, only turns out to be the index values of the first and last character of PASSWORD. eg. If PASSWORD=abc KEY outputs 0002. I want the whole PASSWORD to become KEY (as dictated).
#include <string.h>
#include <iostream>
#include <stdlib.h>
#include <sstream>
std::string PASSWORD, KEY, ENCRYPTED_TEXT;
void algorithm(std::string note){
ENCRYPTED_TEXT.append(note);
/**STANDARD RULESET**/
std::string ck[] = { "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", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
for(int x=0;x<PASSWORD.size();x++){
for(int y=0;y<sizeof(ck)/4;y++){
if(PASSWORD.substr(x,x+1)==ck[y]){
std::stringstream ind;
if(y>9) ind << y+1;
else ind << "0" << y;
KEY.append(ind.str());
}
}
}
std::cout << "\nKey is " << KEY;
//make_file(ENCRYPTED_TEXT);
}
void qn_setup(){
bool ask=true;
while(ask=true){
std::string check1="", check2="";
std::cout << "Type a password:\n";
std::cin >> check1;
std::cout << "Confirm password:\n";
std::cin >> check2;
if(check1==check2) {
PASSWORD=check1;
ENCRYPTED_TEXT=check1;
ask=false;
break; }
else { std::cout << "\nPasswords did not match.\n"; }
}
}
int main(){
qn_setup();
algorithm(""); //testing key
}
There aren't any syntax errors, just logic errors.
Upvotes: 0
Views: 263
Reputation: 97918
string substr ( size_t pos = 0, size_t n = npos ) const;
Generate substring Returns a string object with its contents initialized to a substring of the current object. This substring is the character sequence that starts at character position pos and has a length of n characters.
so your check has to be:
PASSWORD.substr(x,1)==ck[y] /* instead of PASSWORD.substr(x,x+1) */
Upvotes: 1