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);
/**
* @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')); }