c++ - 错误处理 C++:程序永远不会停止
问题描述
我刚刚开始使用 C++,所以请原谅我的误解。
我正在制作一个程序,其中下一个辅音被添加到每个辅音上。
例如,如果输入为joy,则结果为jkoyz。
因为 k 出现在 j 之后,所以它被插入到 j 之后,o 是元音,所以在 o 之后没有插入任何东西,而 z 在 y 之后插入,因为 z 在字母表中 y 之后。
#include <iostream>
#include <string>
#include <typeinfo>
using namespace std;
int main ()
{
string str = "joy";
string constant = "bcdfghjklmnpqrstvwxyzz";
for(int i = 0; i < str.length(); i++){
if (constant.find(str[i]) != string::npos) {
int index = constant.find(str[i]);
char closestConstant = constant[index + 1];
char *closestConstantPointer = &closestConstant;
str.insert(0, closestConstantPointer);
}
}
}
问题出在str.insert(0, closestConstant);
一线。有什么指导吗?
解决方案
当你添加一个辅音,然后i
只增加一次,你的光标最终会落在新的辅音上,所以你总是在添加新的辅音。像这样:
joy
jkoy
jkloy
jklmoy
等等。
i
解决方案是在添加辅音时递增。for
保留循环内的增量;你只需要在命令i
之后再次增加。str.insert(...)
推荐阅读
- r - 如何制作具有两个显示比例的分类变量的分组条形图?
- python - 过滤包含非 ascii 值的 pandas 数据帧行
- python - datetime.strptime 给了我错误的月份
- python - 如何在两个函数中传递一个值而不显示错误?
- webstorm - JetBrains 如何在 WebStorm IDE 上打开多项目
- javascript - 在 vue 组件中添加 html
- javascript - 在 nodejs 中添加新/替换元素
- python - 使用 Python 和 pandas 计算两个日期之间的 Delta
- kubernetes - GKE:如何使用 API 获取节点和 pod 的数量
- google-apps-script - Google Sheets App Script - 从公式中获取值而不是写入单元格