2014-09-20 74 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]);