0

运行时出现此错误:

在抛出 'std::out_of_range' 实例后调用终止 what(): basic_string::at Abort (core dumped)

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

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 }
4

1 回答 1

4

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

于 2013-10-25T07:06:26.563 回答