读书人

C++ cin.get(s,100)与cin.getline(s,1

发布时间: 2013-04-02 12:35:26 作者: rapoo

C++ cin.get(s,100)与cin.getline(s,100)问题:
下面代码中用了cin.get(s,100),要2个cin.get();而用cin.getline(s,100),只需一个cin.get();
代码如下:
#include <iostream>
using namespace std;
int count(char *s,char letter)
{
int count=0;
while(*s)
if(*s++==letter)
count++;
return (count);
}
int main()
{
char str[100],c;int n;
cout<<"Please input a integer:"<<endl;
cin>>n;cin.get();
while(n--)
{
cout<<"input a string:";
cin.get(str,10);
cin.get();/*这里一个*/
cout<< "input a letter:";
cin>>c;
cout<<"the count is:"<<count(str,c)<<endl;
cin.get();/*这里又一个,才使程序正常*/
}
return 0;
}

#include <iostream>
using namespace std;
int count(char *s,char letter)
{
int count=0;
while(*s)
if(*s++==letter)
count++;
return (count);
}
int main()
{
char str[100],c;int n;
cout<<"Please input a integer:"<<endl;
cin>>n;cin.get();
while(n--)
{
cout<<"input a string:";
cin.getline(str,10);/*这里用了cin.getline(str,100),下面只需一个cin.get(),就正常了*/
cout<< "input a letter:";
cin>>c;
cout<<"the count is:"<<count(str,c)<<endl;
cin.get();
}
return 0;
}


C++
[解决办法]


/**
* @brief Simple multiple-character extraction.
* @param __s Pointer to an array.
* @param __n Maximum number of characters to store in @a s.


* @return *this
*
* Returns @c get(__s,__n,widen('\\n')).
*/
__istream_type&
get(char_type* __s, streamsize __n)
{ return this->get(__s, __n, this->widen('\n')); }

/**
* @brief Extraction into another streambuf.
* @param __sb A streambuf in which to store data.
* @param __delim A "stop" character.
* @return *this
*
* Characters are extracted and inserted into @a __sb until one of the
* following happens:
*
* - the input sequence reaches EOF
* - insertion into the output buffer fails (in this case, the
* character that would have been inserted is not extracted)
* - the next character equals @a __delim (in this case, the character
* is not extracted)
* - an exception occurs (and in this case is caught)
*
* If no characters are stored, failbit is set in the stream's error
* state.
*/
__istream_type&
get(__streambuf_type& __sb, char_type __delim);


cin.get()有多个重载,其中两个见上面。第一种是你是用的形式,内部加上一个参数(换行符)之后调用第二种形式。
第二种形式在解释什么时候停止提取字符的时候,第三条说道:the next character equals @a __delim(in this case, the character is not extracted)。也就是说,当流中的下一个字符等于第三个参数(默认是换行符)的时候,就不再读入数据了,并且特别说明,此时,这个字符(也就是换行符)并不会被提取出来。

getline同理。

/**
* @brief String extraction.
* @param __s A character array in which to store the data.
* @param __n Maximum number of characters to extract.
* @param __delim A "stop" character.
* @return *this
*
* Extracts and stores characters into @a __s until one of the


* following happens. Note that these criteria are required to be
* tested in the order listed here, to allow an input line to exactly
* fill the @a __s array without setting failbit.
*
* -# the input sequence reaches end-of-file, in which case eofbit
* is set in the stream error state
* -# the next character equals @c __delim, in which case the character
* is extracted (and therefore counted in @c gcount()) but not stored
* -# @c __n-1 characters are stored, in which case failbit is set
* in the stream error state
*
* If no characters are extracted, failbit is set. (An empty line of
* input should therefore not cause failbit to be set.)
*
* In any case, a null character is stored in the next location in
* the array.
*/
__istream_type&
getline(char_type* __s, streamsize __n, char_type __delim);

/**
* @brief String extraction.
* @param __s A character array in which to store the data.
* @param __n Maximum number of characters to extract.
* @return *this
*
* Returns @c getline(__s,__n,widen('\\n')).
*/
__istream_type&
getline(char_type* __s, streamsize __n)
{ return this->getline(__s, __n, this->widen('\n')); }


下面的形式加上一个参数(换行符)之后调用上面的形式。上面形式第二条注释说:the next character equals @c __delim, in which case the character is extracted (and therefore counted in @c gcount()) but not stored。意思是,当下一个字符等于第三个参数的时候就不再提取数据,此时,这个字符(换行符)会被读出并且会被gcount()函数统计,但是并不存储到参数中。

因此呢,这两者的区别就在于行尾的换行符而已。get(str, 10)不读取换行符,所以需要一个额外的get()读取并抛弃之。而getline(str, 10)形式默认就读取了换行符,所以可以少一个get()。最后那个get()两种情况都是需要的,因为输入letter之后还多的一个换行符还存在流中呢,需要抛弃。

读书人网 >C++

热点推荐