// 检查输入.cpp : Defines the entry point for the console application.
  //使用换行函数来检查输入

  #include "stdafx.h"
  #include <iostream>
  using namespace std ;

  void newLine();
  //忽略当前行剩余的所有输入,包括最后的'\n'

  void getInt(int &number);


  int main(int argc, char* argv[])
  {
     int n = 0 ;

     getInt(n);

     cout <<"Final value read in = " << n <<endl
         <<"End of demonstration.\n";

     return 0;
  }

  void newLine()
  {
     char symbol = '\0';

     do      //读取当前行所有剩余的输入,但不进行任何处理,相当于抛弃该行剩余的输入
     {
        cin.get(symbol);
     }while(symbol != '\n');
  }

  void getInt(int &number)
  {
     char ans = '\0';

     do
     {
        cout <<"Enter input nubmer: ";
        cin >>number;
        cout <<"You entered  " << number
            <<" Is that correct?(yes/no): ";
        cin >>ans;
        newLine();
     }while( (ans == 'N') || (ans == 'n') );
  }

 

 

 

 posted on 2012-04-15 21:14  飞翔@骑士  阅读(145)  评论(0)    收藏  举报