Circuit Negma

C++, C, VB.NET, PCB, Electronics, Circuit Design

Reading and Processing Numbers

Posted by Circuit Negma on March 13, 2006


One of the C++ properties is that C++ do not tolerate any kind of mismatch of
data type and their variables. For example, entering a char value into an int
or double variable causes serious errors, called Input Failure. 
/*
 *  Copyright © 1997-2003 Metrowerks Corporation.  All Rights Reserved.
 *
 *  Created By: Circuit Negma
 *
 *  Description: 
 *               This Function checks if the input is a number (integer)
 *               or not. Then, The Function demands the the user input
 *               numbers only and not characters. This function make use
 *               of [ Input Failure ] to check the input stream. 
 */

#include <iostream>
using namespace std;

int check()
{
	int i;
	bool flag;
	
	cout << "Please Enter a number: ";
	
	cin.clear();                        // Line 6
 	cin.ignore(500, '\n');              // Line 7
 	
 	flag = cin >> i;
 
	 while (!flag)
	 {
	       cout << endl;
	       cin.clear();
	       cin.ignore(500, '\n');
	       cout << "this is not a number" << endl;
	       cout << " you entered :  " << i << endl;
	       cout << "please enter a number again: ";
	       flag = cin >> i;
	       
	}
	       cout << "this is n:  " << i << endl << endl;
		
	return i;
}


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: