2014-09-20 70 views
0

我正在編寫一個基本代碼來對字符串執行一些操作。當我嘗試運行我的程序時,它會掛起並且不會輸出。任何人都可以指出錯誤,並建議進行必要的更改/優化。由於無法顯示輸出字符串

#include<iostream> 
#include<string> 
#define MAX 100 
using namespace std; 
int main(){ 

int i=0,j=0; 

string ch,out; 
cin>>ch; 

while(ch[i]!='\0'){ 
string dot="."; 
if(ch[i]=='A'||ch[i]=='E'||ch[i]=='I'||ch[i]=='O'||ch[i]=='U' 
||ch[i]=='a'||ch[i]=='e'||ch[i]=='i'||ch[i]=='o'||ch[i]=='u'){ 
    i++; 
    break; 
    } 
    else{ 
    if(isupper(ch[i])){ 
     out+=dot; 
     out+=tolower(ch[i]); 
    } 
    else {out+=dot; 
     out+=ch[i]; 
    } 

    } 
    } 
cout<<out; 


} 
+0

你可以發佈您收到的完整的錯誤? – 2014-09-20 19:46:58

+2

'out.append(1,ch [i])'。或者只是'out + = ch [i];' – 2014-09-20 19:47:03

+0

@IgorTandetnik感謝您的編輯。我的程序現在編譯好了,但它不顯示輸出? – johnripper 2014-09-20 19:54:06

回答

0

如果你看一下documentationstd::basic_string::append,你會看到,接受單個字符作爲其參數的函數的唯一版本是:

basic_string& append(size_type count, CharT ch); 

而且還預計size_type count指定要追加到字符串的字符數。

在你的情況,因爲你只是想追加一個單個字符,你應該使用:

out.append(1, ch[i]); 

out.append(1, tolower(ch[i])); 
0

使用out.append不能追加char。如果你想添加單個字符,嘗試

out.append(1, ch[i]);