维杰尼雷密码错误在C++
Vigenere Cipher error in C++
我正在用c ++创建一个vigenere密码,当我运行代码时,它有一个错误说:(按重试调试应用程序)控制台应用程序 2.exe 触发了断点。调试断言失败!程序: C:\Windows\system32\MSVCP140D.dll文件: c:\program files (x86)\Microsoft Visual Studio 14.0\vc\include\xstring行: 1681表达式:字符串下标超出范围有关程序如何导致断言的信息失败,请参阅有关断言的视觉C++文档。(按重试以调试应用程序)控制台应用程序 2.exe 触发了断点。程序"[3668] ConsoleApplication2.exe"已退出,代码为 -1073741510 (0xc000013a)。这是代码:
#include <iostream>
#include <string>
#include "stdafx.h"
using namespace std;
int main()
{
string plaintext, key, Result;
int k = 0;
cout << "Enter the plain text: ";
cin >> plaintext;
cout << "Enter the key word: ";
cin >> key;
for (int i=0; i<plaintext.length(); i++)
{
Result[i] = (((plaintext[i] - 97) + (key[k] - 97)) % 26) + 97;
k++;
if (k == 6)
(k = 0);
}
cout << " nnn";
for (int i=0; i<plaintext.length(); i++)
cout <<" "<< Result[i];
cout << "nnnn";
return 0;
}
错误出在 for 语句中for (int i=0; i<plaintext.length(); i++)
它说了一些关于<符号的信息,我不知道为什么。有什么帮助吗?谢谢:)>
您从不为Result
设置大小,i
因此您在
Result[i] = (((plaintext[i] - 97) + (key[k] - 97)) % 26) + 97;
无效。 您需要首先设置Result
的大小。 你可以使用类似的东西
string plaintext, key, Result;
int k = 0;
cout << "Enter the plain text: ";
cin >> plaintext;
cout << "Enter the key word: ";
cin >> key;
Result.resize(plaintext.size());
//...
在语句中
key[k]
你如何检查索引 k
相关文章:
- 警告处理为错误这里有什么问题
- "error: no matching function for call to"构造函数错误
- boost::进程间消息队列引发错误
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- QT在错误的班级中寻找空位
- vector.resize()中的分配错误
- 代码在main()中运行,但在函数中出现错误
- 释放错误后堆使用
- (C++)分析树以计算返回错误值的简单算术表达式
- Project Euler问题4的错误解决方案
- 我的字符计数代码计算错误.为什么
- 从"int*"强制转换为"unsigned int"会丢失精度错误
- 尝试导入pybind-opencv模块时出现libgtk错误
- CMake项目Boost库错误:Boost/config/compiler/gcc.hpp:165:10:致命错误:cs
- 在某些循环内使用vector.push_back时出现分段错误
- MSVC多行宏编译器错误
- 静态数据成员的问题-修复链接错误会导致编译器错误
- 为什么在运行时没有向我们提供有关分段错误的更多信息?
- 错误:未在此范围内声明'reverse'