()函数说明
说明:
()的返回值是一个char型的字符,其返回值是指针指向的当前字符,但它只是观测
指针停留在当前位置并不后移。
举个栗子:
#include <iostream>
#include <string>
using namespace std;
int main()
{
cout<<"Enter a Word or Number:";
();
cin>>ws;
int c = ();
if(c == EOF) return 1;
if(isdigit(c))
{
int n;
cin>>n;
cout<<"number:"<<n<<endl;
}
else
{
string str;
cin>>str;
cout<<"word:"<<str<<endl;
}
return 0;
}
在这个程序里,我们需要完成的任务是,判断输入的是数字还是字符串,因为()函数返回的是当前指针所指的char类型字符,因此c中存储的就是输入的字符串的第一个字符,通过这样的方式可以获取缓冲区中的值,并且指针不后移,这样不会影响后面对缓冲区中字符的输出。