2013-10-25 3 views
1

나는이 오류를 얻고을의 인스턴스를 던지는 후 호출 종료 :C는 + + 표준 : : out_of_range '

호출 종료'표준 : : out_of_range ' 무엇을()의 인스턴스를 던지는 후 : basic_string :: at Abort (코어 덤프)

나는 아주 새로운 팁이 있습니다.

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

입력 문자열이 비어 (라인 15)이기 때문에 라인 (50)에 sentence.at(0)에 대한 첫 번째 호출이 예외가 발생합니다.

+0

좋아요, 나는 sentence.at (0)을 문장으로 변경했습니다. 이제, 새로운 문제가 생겼다 : pig.cpp : 50 : error : '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에. 또는 첫 번째 호출을 제거하십시오 pigl (어쨌든 거기에 있습니까?) – arne

+0

감사합니다 오류가 사라졌습니다. 그러나 코드는 여전히 작동하지 않습니다?!? 다른 건 보이니? – guesswho