【发布时间】:2012-08-24 16:51:03
【问题描述】:
我正在创建这个 revese 字符串 App,但如果我在字符串中包含空格,则会出现错误!
#include <iostream>
#include <string>
using namespace std;
int main()
{
int inputa;
cout<<"%%%%%%%%%%%%%%%%%%String Reversing App%%%%%%%%%%%%%%%%%%%%%%%%"<<endl<<endl;
cout<<"\nEnter 1 to continue and 0 to exit"<<endl<<endl;
cin>>inputa;
if(inputa!=0)
{
do
{
string a,c="";
cout<<"\nEnter the string you want to Reverse : ";
cin>>a;
for(int x=a.length()-1; x>=0; x--)
{
c=c+a.substr(x,1);
}
cout<<"\nThe Reverse String is : "<<c<<endl;
cout<<"\nEnter 1 to continue and 0 to exit"<<endl<<endl;
cin>>inputa;
}
while(inputa!=0);
}
//not my home work
}
如果我键入以下字符串,例如“abc def”,则会出现错误。但除此之外它完美无缺!代码有没有错误!我是 CPP 的新手,所以如果你能帮助我会很有帮助!
【问题讨论】:
-
为什么不在
algorithm库中使用std::reverse? -
您遇到的错误是什么?
-
@Scott Hunter 我有时会陷入无限循环,而下次会发生一些其他事情!
-
循环可能是因为 cin 数据的解析在第一个空格处停止 - 剩下的内容将在下次读取