gpt4 book ai didi

c++ - std::string::append(std::string) 错误输出

转载 作者:行者123 更新时间:2023-11-28 08:01:07 27 4
gpt4 key购买 nike

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

在函数algorithm()中:第 16-25 行将 std::string ck[] 的索引 append 到 nested 循环内的 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
}

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

最佳答案

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.

所以你的支票必须是:

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

关于c++ - std::string::append(std::string) 错误输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11450364/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com