读书人

cin.get(字符数组数组大小分隔符)

发布时间: 2012-03-17 19:06:27 作者: rapoo

cin.get(字符数组,数组大小,分隔符)与cin.getline(字符数组,数组大小,分隔符)区别
#include <iostream.h>
以下两个程序输出是一样的,那两个方法cin.get与cin.getline(都带三个参数的时候)是一样的吗?
程序一:
#include <string.h>
void main()
{
const int size=80;
char buffer1[size];
cout < < "input a sentence: " < <endl;
cin.getline(buffer1,size, ' ');
char c=buffer1[0];
for(static int n=0;c!= '\0 ';n++)
{
c=buffer1[n];
cout < < "the string read with cin.get is: " < <buffer1[n] < <endl;
}
}
程序二:
#include <string.h>
void main()
{
const int size=80;
char buffer1[size];
cout < < "input a sentence: " < <endl;
cin.get(buffer1,size, ' ');
char c=buffer1[0];
for(static int n=0;c!= '\0 ';n++)
{
c=buffer1[n];
cout < < "the string read with cin.get is: " < <buffer1[n] < <endl;
}
}
麻烦解释的详细点。



[解决办法]
自己看吧
istream_type&
get(streambuf_type& sb,char_type delim);

Extracts characters and inserts them in the output sequence controlled by sb. Characters are extracted and inserted until any of the following occurs:

An end-of-file on the input sequence
A failure when inserting in the output sequence
The next available input character == delim.
An exception

If the function stores no characters, it calls the basic_ios member function setstate(failbit), which may throw ios_base::failure. If failure was due to catching an exception thrown while extracting characters from sb and failbit is on in exception(), then the caught exception is rethrown.
----------------------------------------------
istream_type&
getline(char_type* s, streamsize n, char_type delim);

Extracts characters and stores them into successive locations of an array whose first element is designated by s. Characters are extracted and stored until any of the following occurs:

n-1 characters are stored
An end-of-file on the input sequence
The next available input character == delim.

If the function stores no characters, it calls the basic_ios member function setstate(failbit), which may throw ios_base::failure. In any case, it stores a null character into the next successive location of the array.
[解决办法]
cin.get()第一个用法,是读入一个字符。
cin.get()第二个用法,也是输入一行(同cin.getline()),但是区别就是,不输出分隔符~
getline默认情况下以回车( '\n ')作为结束,输入一行

读书人网 >C++

热点推荐