Issues with Input Validation

Hey guys. I'm having some trouble with input validation for one of my assignments in my Advanced C++ class. I'm using g++ on my linux box. Weird thing is, when I use the following block to check an integer value:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int places = 7; //arbitrary number outside valid range
do
{
  std::cout << "Please enter a whole number between -4 and 4 inclusively: " << std::flush;
  std::cin >> places;
  //if the user entered data that cannot be assigned to a int
  if(std::cin.fail())
  {
    std::cin.clear();
    std::cin.ignore((std::numeric_limits<int>::max)(), '\n');
    std::cout << "Invalid Input" << std::endl;
    //if I were to cout the value of 'places' here, before I test any other values, it would still be 7
  }
  //if input is a valid integer, but not within the range
  else if(places < -4 || places > 4)
    std::cout << "You entered " << places << ", which is not between -4 and 4" << std::endl;
}while(places < -4 || places > 4);
std::cout << "You entered " << places << std::endl;

It works like a BOSS. It catches every possible invalid input possibility, even when I do nothing but type garbage on the terminal for ten minutes. HOWEVER, the same cannot be said for when I do the same for a double type variable:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
double base_number = 7; //once again just an arbitrary number
do //prompting user for the base number
{		
  std::cout << "Please enter a non-negative decimal number. Enter 0 to exit: " << std::flush;
  std::cin >> base_number;
  //if the user entered data that cannot be assigned to a double
  if(std::cin.fail())
  {			
    std::cin.clear();
    std::cin.ignore((std::numeric_limits<double>::max)(), '\n');
    std::cout << "Invalid Input" << std::endl;
    //doing a cout here, value of base_number will ALWAYS be 0!
  }
  else if(base_number < 0) //if input is a valid double, but negative			
    std::cout << "You entered " << base_number << ", which is a negative number" << std::endl;
}while(base_number < 0);
std::cout << "You entered " << base_number << std::endl;

For one reason or another, it either A) sets the value of the double to 0, which is a problem because it will end the program (assignment submission requirement, no getting around it) or B) it will go through an infinite loop.

I've tried setting the value of base_number to something else immediately after the cin.ignore, it will go through a loop.

I've tried to make use of flags (i.e. declaring a bool bad_input outside the loop, setting it to false after the cin.ignore statement, and altering the loop condition appropriately) and it STILL goes through an infinite loop.

Seriously! Compile each of these blocks in g++ and you'll see what I mean. It's crazy. Anyway, I'm running out of ideas, time, and patience. Any help at this point would be massively appreciated. Thanks in advance!
The ignore() call should still be using numeric_limits<streamsize>::max not double, or int. Remember you are clearing the input buffer, which is a series of characters. Also you probably should be using numeric_limits<streamsize>max instead of the int in both of your snippets. A streamsize may hold larger numbers than an int and ignore is prototyped as:
istream& ignore ( streamsize n = 1, int delim = EOF );

Jim
Thanks for the reply Jim!

Unfortunately when I swap out double/int for 'streamsize' it says "error: ‘streamsize’ was not declared in this scope" when I compile it again in g++. Is there another library I need to include? I got the following currently in my program:

#include <iostream>
#include <iomanip>
#include <limits>
#include <cmath>
std::streamsize
Did you properly scope std::streamsize?

Fixed it. My cin,ignore statement for both snippets is now as follows:

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

This seems to have fixed the issue. Both blocks catch any invalid input now. Thanks a lot! :-)
Topic archived. No new replies allowed.