4 Replies - 15750 Views - Last Post: 10 January 2007 - 03:39 AM Rate Topic: -----

#1 Reehad  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 09-January 07

Integer Input Validation - Need Help

Posted 09 January 2007 - 03:48 AM

Ok, I've browsed google, and searched older topics in this forum but none have seemed to help. I'm new to c++ and currently don't know the while statement. I need help to validate that an input is a number, no decimals. Xing's code sinppet hasn't worked, I got 3 compiling errors
#include <iostream.h>
#include <limits>

using namespace std;

int main()
{
	int iNum;

		cout << "Please enter a Number \n";
		cin >> iNum;
		cin.ignore(numeric_limitations<int>::max(), '\n');

		if (!cin || cin.gcount() != 1)
		{
			cout << "Not a numeric value.";
		}
		else if (iNum == 5)
		{
			cout << "Number is equal to 5";
		}
		else if (iNum > 5)
		{
			cout << "Number is greater than 5";
		}
		else if (iNum < 5)
		{
			cout << "Number is less than 5";
		}

	return 0;
}


The 3 errors are:
5.cpp(16) : error C2065: 'numeric_limitations' : undeclared identifier
5.cpp(16) : error C2062: type 'int' unexpected
5.cpp(16) : error C2039: 'max' : is not a member of '`global namespace'

I haven't copied and pasted the three comments and a single line space at the start of the code.

This post has been edited by Reehad: 09 January 2007 - 03:56 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Integer Input Validation - Need Help

#2 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 289
  • View blog
  • Posts: 1,898
  • Joined: 25-October 06

Re: Integer Input Validation - Need Help

Posted 09 January 2007 - 03:58 AM

the statement
		cin.ignore(numeric_limitations<int>::max(), '\n');


should be
		cin.ignore(numeric_limits<int>::max(), '\n');


have a look at
http://www.augustcou...ial/iotips.html

This post has been edited by horace: 09 January 2007 - 04:00 AM

Was This Post Helpful? 0
  • +
  • -

#3 Xing  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 19
  • View blog
  • Posts: 725
  • Joined: 22-July 06

Re: Integer Input Validation - Need Help

Posted 09 January 2007 - 04:25 AM

@Reehad
This is the exact code
http://www.dreaminco.../snippet591.htm

<iostream.h> is a non-standard header. Use new templated header <iostream>. Also <limits.h> is standard C header which is supported in C++ but is deprecated since .h forms of C headers are deprecated. Instead use cname headers like <climits> <cctype> etc
I guess you are unaware of namespace concept. So, it will be a good idea to go through namespace tutorial
http://www.cplusplus...namespaces.html

This post has been edited by Xing: 09 January 2007 - 04:25 AM

Was This Post Helpful? 0
  • +
  • -

#4 Reehad  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 09-January 07

Re: Integer Input Validation - Need Help

Posted 10 January 2007 - 02:56 AM

Ok thanks, that worked. But when using it with a while loop it doesn't seem to. It now continuosly loops instead with "Wrong answer etc.."

//Testing Maths

#include <iostream>
#include <limits>
 
using namespace std;

int main()
{
	int iAnswer;

	cout << "What is 15 + 3? ";
	cin >> iAnswer;
	cin.ignore(numeric_limits<int>::max(), '\n');

	if (!cin || cin.gcount() != 1)
	{
		cout << "Not a numeric value. That wasn't ";
	}

	while (iAnswer!=18)
	{
		cout << "\nWrong, please try again... ";
		cin >> iAnswer;
	}
	cout << "The answer! \n";

	return 0;
}



And the compiler isn't getting any errors

This post has been edited by Reehad: 10 January 2007 - 03:15 AM

Was This Post Helpful? 0
  • +
  • -

#5 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 289
  • View blog
  • Posts: 1,898
  • Joined: 25-October 06

Re: Integer Input Validation - Need Help

Posted 10 January 2007 - 03:39 AM

after an error you need to call cin.clear() to clear the error condition before attempting to read again, e.g.
#include <iostream>
#include <limits>
using namespace std;

int main()
{
	int iAnswer;
	cout << "What is 15 + 3? ";   
	cin >> iAnswer;
	while (iAnswer!=18)
	{
		if (!cin || cin.gcount() != 1)
		  {
		  cout << "Not a numeric value. That wasn't ";
		  cin.clear();
		  cin.ignore(numeric_limits<int>::max(), '\n');
		  }
		cout << "\nWrong, please try again... ";
		cin >> iAnswer;
	}
	cout << "The answer! \n";
	return 0;
}


This post has been edited by horace: 10 January 2007 - 03:40 AM

Was This Post Helpful? 1

Page 1 of 1