【发布时间】:2017-11-23 23:34:09
【问题描述】:
我在哪里犯错了?
#include "stdafx.h"
#include <iostream>
#include <string.h>
#include <sstream>
using namespace std;
string input = "";
string slogovi = "";
int broj = 0;
void main()
{
printf("Please enter a valid name:\n>");
getline(cin, input);
int c = input.length();
for (int i = 0; i < c; i++) {
if (input[i] == 'a' || input[i] == 'e' || input[i] == 'i' || input[i] == 'o' || input[i] == 'u') {
slogovi[broj] = input[i];
slogovi[broj + 1] = '-';
broj += 2;
}
else {
slogovi[broj] = input[i];
broj++;
}
}
printf("%s", slogovi);
system("pause");
}
线程 0x14d0 已退出,代码为 -1073741510 (0xc000013a)。 程序“[5452] Strings.exe”已退出,代码为 -1073741510 (0xc000013a)。
【问题讨论】:
-
哦忘了我更新了代码,我用 c 代替(定义 c = input.length();)
-
slogvi[broj] 的值超出其当前大小
-
您可以使用
string::at()而不是[ ]来检测错误。然后,将显示std::out_of_range异常,而不只是崩溃,让您知道错误。