繁体   English   中英

std :: string :: append(std :: string)错误输出

[英]std::string::append(std::string) erroneous output

我正在尝试制作一个简单的文本加密器(并非所有功能都完整),但是我有一个具体的问题需要帮助。 我在此时停止了编码,并添加了一些评论文档进行测试:

在函数algorithm()中:第16-25行在嵌套循环内将std :: string ck []的索引附加到std :: string KEY。 运行外部循环以测量PASSWORD.size(),并对照所有ck []检查每个PASSWORD.substr(...)。

但是,输出结果只是PASSWORD的第一个和最后一个字符的索引值。 例如。 如果PASSWORD = abc KEY输出0002。我希望整个PASSWORD成为KEY(根据指示)。

#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
}

没有任何语法错误,只有逻辑错误。

字符串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. 

因此您的支票必须为:

PASSWORD.substr(x,1)==ck[y] /* instead of PASSWORD.substr(x,x+1) */

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM