2013-10-25 63 views
1

我收到此錯誤的一個實例後終止叫:C++運行時拋出「的std :: out_of_range」

終止叫做拋出「的std :: out_of_range」 什麼()的一個實例後:basic_string的::在 中止(核心轉儲)

我是全新的,任何其他技巧也將不勝感激。

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 } 

回答

3

在50行至sentence.at(0)你的第一個電話會拋出異常,因爲輸入字符串爲空(第15行)。

+0

好的,我把sentence.at(0)改成了句子。現在,我有新的問題:pig.cpp:50:錯誤:無法將'std :: string'轉換爲'char'作爲參數'1'到'bool isvowel(char)' pig.cpp:62:錯誤:無法將'std :: string'轉換爲'char'作爲參數'1'到'bool isvowel(char)' – guesswho

+0

這不是我的意思。您將一個空字符串傳遞給'pigl'。只需將該字符串初始化爲'main'中的某些明顯的內容,例如'string sentance =「foo」;'在第15行。或者刪除第一個呼叫go pigl(爲什麼它有呢?) – arne

+0

謝謝你的錯誤消失了。但代碼仍然無法正常工作?!?你還看到其他什麼嗎? – guesswho

相關問題