11 Replies - 6489 Views - Last Post: 27 October 2011 - 03:33 AM Rate Topic: -----

#1 RandomlyKnighted  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 118
  • View blog
  • Posts: 1,370
  • Joined: 14-January 10

Input Validation of Integers?

Posted 26 October 2011 - 06:37 PM

Hello everyone, I was wondering if someone could please something for me. In my program, I'm receiving a number and a string from the user. The problem I'm trying to correct is when it prompts for the integer and I type a string it throws an error. I want to correct this by validating the user's input.

I found this code in one of the snippets and I've seen it recommended several times here on DIC.

#include <iostream>
#include <limits>

using namespace std;

int main() {
  int number = 0;
  cout << "Enter an integer: ";
  cin >> number;
  cin.ignore(numeric_limits<int>::max(), '\n');

  if (!cin || cin.gcount() != 1)
    cout << "Not a numeric value.";
  else
    cout << "Your entered number: " << number;
  return 0;
}





I don't see where you're comparing it to see if it's really an integer or not. Can someone explain this for me please? Is creating a limit the easiest way to validate the input? Thanks in advance for any help given.

Is This A Good Question/Topic? 0
  • +

Replies To: Input Validation of Integers?

#2 sk1v3r  Icon User is offline

  • D.I.C Addict

Reputation: 231
  • View blog
  • Posts: 668
  • Joined: 06-December 10

Re: Input Validation of Integers?

Posted 26 October 2011 - 06:51 PM

You can't see it because strictly speaking it doesn't happen.
When you use cin to input to your integer, it expects an integer value to be inputted. If you type a string in instead, it sends cin into a fail state (because you have temporarily broken it), which can be checked using !cin. I don't believe that line 10 is strictly needed.
Was This Post Helpful? 0
  • +
  • -

#3 RandomlyKnighted  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 118
  • View blog
  • Posts: 1,370
  • Joined: 14-January 10

Re: Input Validation of Integers?

Posted 26 October 2011 - 07:00 PM

That's actually not my code. That's something I was trying to learn from.

Here's my code:

#include <iostream>
#include <string>

using namespace std;

void calculations();

int main()
{
	calculations();
	return 0;
}

void calculations()
{
	string planet;
	int weight;
	int correctWeight;
	double factor;
	int index;

	cout << "What's your weight in pounds? ";
	cin >> weight;
	cout << endl << endl;

	if (cin.fail())
	{
		cin.ignore(weight);
		cout << "Invalid data. Please type an integer: ";
		cin >> correctWeight;
		cout << endl << endl;
	}

	cout << "Type a planet: ";
	cin >> planet;
	cout << endl << endl;

	index = 0;
	planet[index] = toupper(planet[index]);
	
	if (planet == "Mercury")
	{
		factor = 0.4155;
	}

	else if (planet == "Venus")
	{
		factor = 0.8975;
	}

	else if (planet == "Earth")
	{
		factor = 1.0;
	}

	else if (planet == "Moon")
	{
		factor = 0.166;
	}

	else if (planet == "Mars")
	{
		factor = 0.3507;
	}

	else if (planet == "Jupiter")
	{
		factor = 2.5374;
	}

	else if (planet == "Saturn")
	{
		factor = 1.0677;
	}

	else if (planet == "Uranus")
	{
		factor = 0.8947;
	}

	else if (planet == "Neptune")
	{
		factor = 1.1794;
	}

	else if (planet == "Pluto")
	{
		factor = 0.0899;
	}

	weight = weight * factor;

	cout << "Your weight on the planet " << planet << " is: " << weight << " pounds." << endl << endl;
}


Was This Post Helpful? 0
  • +
  • -

#4 sk1v3r  Icon User is offline

  • D.I.C Addict

Reputation: 231
  • View blog
  • Posts: 668
  • Joined: 06-December 10

Re: Input Validation of Integers?

Posted 26 October 2011 - 07:07 PM

if cin is sent into the fail state then you will need to clear it using cin.clear()
Also, in your code why do you have two variables for weight? you might as well use weight instead of correctWeight after cin has failed ( and cleaar cin before that ! :) )
Was This Post Helpful? 0
  • +
  • -

#5 RandomlyKnighted  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 118
  • View blog
  • Posts: 1,370
  • Joined: 14-January 10

Re: Input Validation of Integers?

Posted 26 October 2011 - 07:18 PM

Ok, here is what I have so far:

void calculations()
{
	string planet;
	int weight;
	double factor;
	int index;

	cout << "What's your weight in pounds? ";
	cin >> weight;
	cout << endl << endl;

	if (cin.fail())
	{
		cin.clear();
		cout << "Invalid data. Please type an integer: ";
		cin >> weight;
		cout << endl << endl;
	}

	cout << "Type a planet: ";
	cin >> planet;
	cout << endl << endl;



It shows that I entered k for the input then it displays the prompt for a new weight and then instead of waiting for the new weight it goes on and displays the prompt for the planet and Visual Studio throws an error message that says string subscript out of range. Any idea why it still thinks its invalid and not waiting to take in the new weight?
Was This Post Helpful? 0
  • +
  • -

#6 sk1v3r  Icon User is offline

  • D.I.C Addict

Reputation: 231
  • View blog
  • Posts: 668
  • Joined: 06-December 10

Re: Input Validation of Integers?

Posted 26 October 2011 - 07:22 PM

you also need to cin.ignore
Was This Post Helpful? 0
  • +
  • -

#7 RandomlyKnighted  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 118
  • View blog
  • Posts: 1,370
  • Joined: 14-January 10

Re: Input Validation of Integers?

Posted 26 October 2011 - 07:58 PM

I think I've almost got this fixed.

void calculations();

int main()
{
	calculations();
	return 0;
}

void calculations()
{
	string planet;
	double weight;
	double factor = 0;
	int index = 0;

	cout << "What's your weight in pounds? ";
	cin >> weight;
	cout << endl;

	while (cin.fail())
	{
		cin.clear();
		cin.ignore();
		cout << "Invalid data. Please type an integer: ";
		cin >> weight;
		cout << endl;
	}

	cout << "Type a planet: ";
	cin >> planet;
	cout << endl << endl;

	while (cin.fail())
	{
		cin.clear();
		cin.ignore();
		cout << "Invalid data. Please type the planet's name correctly: ";
		cin >> planet;
		cout << endl << endl;
	}

	planet[index] = toupper(planet[index]);
	
	if (planet == "Mercury")
	{
		factor = 0.4155;
	}

	else if (planet == "Venus")
	{
		factor = 0.8975;
	}

	else if (planet == "Earth")
	{
		factor = 1.0;
	}

	else if (planet == "Moon")
	{
		factor = 0.166;
	}

	else if (planet == "Mars")
	{
		factor = 0.3507;
	}

	else if (planet == "Jupiter")
	{
		factor = 2.5374;
	}

	else if (planet == "Saturn")
	{
		factor = 1.0677;
	}

	else if (planet == "Uranus")
	{
		factor = 0.8947;
	}

	else if (planet == "Neptune")
	{
		factor = 1.1794;
	}

	else if (planet == "Pluto")
	{
		factor = 0.0899;
	}

	else
	{
		cout << "Invalid data. Please enter the name of a planet: ";
		cin >> planet;
		cout << endl << endl;
	}

	weight = weight * factor;

	cout << "Your weight on the planet " << planet << " is: " << weight << " pounds." << endl << endl;
}



I changed the if statement to a while loop that way in case the user decides to enter the value more than once it will keep asking for the correct number. I then continued to do the same thing for the planet. I'm testing it by typing in the word hello for the planet name. For some reason it doesn't want to clear or ignore that variable. Is there a rule of thumb against clearing cin back to back like this?
Was This Post Helpful? 0
  • +
  • -

#8 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Input Validation of Integers?

Posted 26 October 2011 - 08:08 PM

> cin.ignore();

This only ignores one character. See how cin.ignore was originally used in the first post, they very code snippet you found.
Was This Post Helpful? 0
  • +
  • -

#9 RandomlyKnighted  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 118
  • View blog
  • Posts: 1,370
  • Joined: 14-January 10

Re: Input Validation of Integers?

Posted 26 October 2011 - 08:10 PM

Yeah, one this line:


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



I don't really understand what they are doing here though.
Was This Post Helpful? 0
  • +
  • -

#10 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Input Validation of Integers?

Posted 26 October 2011 - 08:10 PM

So Google.
Was This Post Helpful? 0
  • +
  • -

#11 RandomlyKnighted  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 118
  • View blog
  • Posts: 1,370
  • Joined: 14-January 10

Re: Input Validation of Integers?

Posted 26 October 2011 - 08:24 PM

Ok, so I found something about how you can use numeric_limits<string>::is_specialized but I haven't found a good definition yet of what it does. I'm still looking though.
Was This Post Helpful? 0
  • +
  • -

#12 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Input Validation of Integers?

Posted 27 October 2011 - 03:33 AM

So look at how you can effectively break down the line of code into something you can learn about. Assuming you understand C++ syntax, you should recognize the following, even without knowing any details:

1. cin is an istream object. You should know this from using cin from day 1. You might not know any details about istream, but you should know cin is an object.
2. ignore is a member function of the istream class. Even if you don't know this, you should recognize the syntax to mean ignore is a member function.
3. numeric_limits is a class, because it's being instantiated and then a static member function is being called.
4. There's a static member function called max.

If you didn't recognize all four points above, you need to learn about classes, member functions, static member functions, and templates. That or Google significantly more.

Anyway, here's what I get with appropriate Googling (first result linked here), and I noted the search terms I used.

1. C++ cin ignore.
2. C++ numeric_limits.
3. Looking at number 2, you can then answer this question
4. Looking at number 2, you can then answer this question

it's a bit hard to understand the documentation if you don't understand some level of C++ syntax. In particular, numeric_limits is impossible to understand if you don't know about templates.

But, what were you Googling for??

This post has been edited by Oler1s: 27 October 2011 - 03:35 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1