c++ - 如何修复 std::stoi 中的“std::invalid_argument”
问题描述
我正在尝试做一个套接字服务器,但是当我尝试传递我得到的数字时std::invalid_argument
,我不知道为什么。我重新编写了代码以使其更简单,这里是:
std::string a = std::to_string(32);
char texto[5];
strcat(texto,a.c_str());
printf("%s", texto);//Isn't printing a string that you can convert to a number
std::string b = texto;
int s = std::stoi(texto);
std::cout << s;
我希望输出为 32,但我得到了错误。
解决方案
char texto[5];
strcat(texto,a.c_str());
texto
未初始化,因此它包含垃圾值。strcat
必须首先阅读它们以寻找 NUL 终止符,从而为您提供未定义的行为。
您可能正在寻找strcpy
而不是strcat
.
推荐阅读
- javascript - React useParams:在反应路由器中链接“用户名”时遇到问题
- javascript - 不和谐机器人(discord.js)中的错误消息?
- javascript - 不知道为什么 Portal 在我的代码中不起作用
- javascript - 选择的插件 - 在 .trigger("chosen:updated") 之后删除特定选项的“x”按钮(删除选项)
- processing - 处理 - 指定鼠标点击图像按钮的确切位置
- angular - 如何像使用 Promises 或 async/await 一样链接 observables?
- asp.net-core - B2C 注册和登录政策 - 自助重置密码重定向回更改密码
- python - sqlite3.OperationalError:靠近“<”:语法错误
- reactjs - 在 react-three-fiber/drei/Three.js 中两次使用相同的 GLTF 模型
- javascript - 带有引导程序,ajax laravel 的进度条