6.3w+
社区成员
void main(){
int i;
cin>>i;
cin.ignore();
cout<<"Please enter a double"<<i;
}
while (!(cin>>i))
{
cin.clear(); //重置输入
while (cin.get() != '\n')
continue; //解除错误的输入
cout<<"Please enter a double"<<"("<<(i+1)<<")"<<": ";
}
首先告诉你:学好英文很重要哦!
istream::sync public member function
int sync ( );
Synchronize input buffer with source of characters
Synchronizes the buffer associated with the stream to its controlled input sequence. This effectively means that the unread characters in the buffer are discarded.
这里的sync就是英文单词Synchronize的缩写,意思是“v. 使同时,同时发生”。
就是把输入缓冲区内没有被读取的全部都扔掉,这样才能“同步”。
Example:
// read a file into memory
#include <iostream>
using namespace std;
int main () {
char first, second;
cout << "Please, enter a word: ";
first=cin.get();
cin.sync();
cout << "Please, enter another word: ";
second=cin.get();
cout << "The first word began by " << first << endl;
cout << "The second word began by " << second << endl;
return 0;