为什么我会出现记忆错误
Why am i getting memory errors?
当我运行这个c++程序并执行它的函数时,我得到了这个内存错误
TESTER 12345.exe中0x769DC41F处未处理的异常:内存位置0x0042F558处的Microsoft C++异常:std::invalid_argument。
我在Visual studio 2013上运行这个。
#include <iostream>
#include <string>
#include <algorithm>
#include <iomanip>
#include <cmath>
using namespace std;
string encrypt(string, int);
string decrypt(string source, int key);
int main(int argc, char *argv[])
{
string Source;
string userInput;
string keyString;
int Key;
int locationSpace = 0;
int locationOfFirstKeyIndex = 0;
int choice;
/*locationSpace = userInput.find(" ");
keyString = userInput.substr(locationOfFirstKeyIndex, locationSpace);
Source = userInput.substr(locationSpace + 1);
Key = stoi(keyString);*/
cout << "To encode a message type 1, to decode a message type 2: ";
cin >> choice;
if (choice == 1)
{
cin.ignore();
cout << "Enter a message to decode: ";
getline(cin, Source);
locationSpace = userInput.find(" ");
keyString = userInput.substr(locationOfFirstKeyIndex, locationSpace);
Key = stoi(keyString);
Source = userInput.substr(locationSpace + 1);
encrypt(Source, Key);
cout << "Encrypted: " << encrypt(Source, Key) << endl;
}
else if (choice == 2)
{
cin.ignore();
cout << "Enter the message To decode: ";
getline(cin, userInput);
locationSpace = userInput.find(" ");
keyString = userInput.substr(locationOfFirstKeyIndex, locationSpace);
Key = stoi(keyString);
Source = userInput.substr(locationSpace + 1);
decrypt(Source, Key);
cout << "Decrypted: " << decrypt(Source, Key) << endl;
}
else
{
cout << "Invalid Input";
}
system("pause");
}
string encrypt(string source, int key)
{
string Crypted = source;
for (int Current = 0; Current < source.length(); Current++)
Crypted[Current] = ((Crypted[Current] + key) - 32) % 95 + 32;
return Crypted;
}
string decrypt(string source, int key)
{
string Crypted = source;
for (int Current = 0; Current < source.length(); Current++)
Crypted[Current] = ((Crypted[Current] - key) - 32 + 3 * 95) % 95 + 32;
return Crypted;
}
请参阅以下链接了解这一点:
http://en.cppreference.com/w/cpp/string/basic_string/stol
当无法执行转换时,stoi方法抛出*std::invalid_argument*异常。您可能需要在将其传递到stoi()之前进行打印,并验证字符串是否有效。
std::cout<<keyString<<std::endl;
我假设错误是在取消注释代码时引起的。让我们运行该错误(您应该是使用调试器执行此操作的人):
keyString = userInput.substr(locationOfFirstKeyIndex, locationSpace);
//userInput is a blank string, lOFKI == 0 and so does locationSpace
stoi(keyString); //keyString is invalid, empty string
在解析之前,您应该尝试获取用户输入…
相关文章:
- 警告处理为错误这里有什么问题
- "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时出现分段错误
- 我是否以错误的方式使用记忆屏障
- 加载图像约500次后,此错误显示在Visual Studio Qimage:不记忆,返回空图像
- 记忆错误结果的说明[编译器行为]
- 为什么我会出现记忆错误