gpt4 book ai didi

C++ 在抛出 'std::out_of_range' 实例后调用终止

转载 作者:行者123 更新时间:2023-11-30 02:51:38 29 4
gpt4 key购买 nike

我在运行时遇到这个错误:

在抛出“std::out_of_range”实例后调用终止 什么():基本字符串::在中止(核心转储)

我是全新的,任何其他提示也将不胜感激。

1 #include <iostream>
2 #include <string>
3 #include <cstdlib>
4 #include <cstring>
5 #include <cctype>
6
7 using namespace std;
8
9 bool isvowel(char);
10 string pigl(string);
11 string rotate(string);
12
13 int main (int argc, char *argv[])
14 {
15 string sentance; //spelled sentence wrong the whole time so im gonna go with it
16 pigl(sentance);
17
18 if (argc != 2)
19 {
20 cout << "useage: ./pig [string of text]" << endl;
21 return 0;
22 }
23 while (--argc) pigl (*++argv);
24 return 0;
25 }
26
27 bool isvowel(char ch)
28 {
29 switch (ch)
30 {
31 case 'a':
32 case 'e':
33 case 'i':
34 case 'o':
35 case 'u':
36 case 'y':
37 return true;
38
39 default:
40 return false;
41 }
42 }
43
44 string pigl(string sentance)
45 {
46 int length;
47 int counter;
48 bool found_vowel;
49
50 if (isvowel(sentance.at(0)))
51 {
52 sentance = sentance += "way";
53 }
54 else
55 {
56 sentance = sentance += " ";
57
58 sentance = rotate(sentance);
59 length = sentance.size();
60 found_vowel = false;
61 for (counter = 1; counter < length -1; counter++)
62 if (isvowel(sentance.at(0)))
63 {
64 found_vowel = true;
65 break;
66 }
67 else
68 {
69 sentance = rotate(sentance);
70 }
71 if (!found_vowel)
72 {
73 sentance = sentance.substr(1,length) += "ay";
74 }
75 else
76 sentance = sentance += "way";
77 }
78 return sentance;
79 }
80
81 string rotate(string sentance)
82 {
83 int length = sentance.size();
84 string jumble;
85 jumble = sentance.substr(1, length) += sentance.at(0);
86 return jumble;
87 }

最佳答案

您在第 50 行对 sentence.at(0) 的第一次调用将抛出异常,因为输入字符串为空(第 15 行)。

关于C++ 在抛出 'std::out_of_range' 实例后调用终止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19583214/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com