這裏是短代碼片段,如果在給定的字符串中的任何空間哪個錯誤在下面的代碼中?
#include<string>
#include<iostream>
#include<cctype>
using namespace std;
//performs string operations
void string_get()
{
string text;
cout<<" enter string "<<endl;
getline(cin,text);
string::size_type position=text.find(' ');
if(position!=string::npos)
{
if(text.find(' ',position+1)!=string::npos)
{
cout<<" contains at least two spaces "<<endl;
}
else
{
cout<<" contains less then two spaces "<<endl;
}
}
else
{
cout<<" no spaces "<<endl;
}
}
int main()
{
string_get();
return 0;
}
當我運行這段代碼,輸入一些字符串,它工作正常,但有這樣的問題,即它說,該試驗這個代碼中有bug,我被要求修復它,但是我看不到哪個bug在這裏?可能string是NULL?或者string不包含任何空格?哪種情況我不得不考慮?
stooooooooooooooooop downvoting – 2012-04-17 07:59:37
爲什麼選票過低? – Nick 2012-04-17 08:00:39
聽起來更像是一個煉獄怪物(或一羣綿羊!) – Nick 2012-04-17 08:03:27