Input validation errors

Mar 23, 2017 at 12:00pm
Hi,

I'm writing a code that will allow a user to enter an integer between 0 and 59, the code needs to have a validation in case a non-integer is entered (incl letters). I've gotten the code below to work correctly, however the minimum is set to 1, and needs to be 0. When it is set to 0 and takes the string entered and uses it as input 0, it needs to come up with an error and prompt for a new input.
I'd appreciate a bit of direction as I'm unable to find anything.

1
2
3
4
5
6
7
8
9
10
cout << "Please enter a number between 0 and 59:";
int num;
cin >> num;
while (!(0 <= num&& num<= 59)){
    cout << "0 <= num<= 59!\n";
    cin.clear();
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
    cout << "Please enter a number between 0 and 59:";    
    cin >> num;
    }


As previously mentioned, the above code (with the min set to 1) provides an error message should the user enter a number outside the range as well as a string, so why does it not work when the minimum is set to 0?



Many thanks!
Mar 23, 2017 at 12:17pm
while (!(0 <= num&& num <= 59))
If you enter 0 the condition (0 <= num&& num <= 59) is true, but the ! makes it false.
I would do it like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
bool valid = false; // set to false to get the loop started

  while (!valid)
  {
    cout << "Please enter a number between 0 and 59: ";
    int num;
    cin >> num;
    if (num >= 0 && num <= 59)
    {
      valid = true;
      cout << "You input was valid\n";
    }
    else
    {
      cout << "Invalid input\n";
    }
    cin.clear();
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
  }


Mar 23, 2017 at 10:30pm
Considering this
code that will allow a user to enter an integer between 0 and 59, the code needs to have a validation in case a non-integer is entered (incl letters)


Then i have this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// small code snippet to take and validate an integer

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

// function declaration
int getInteger(int min, int max);

// START OF MAIN
int main()
{
     cout << "Enter an integer: ";
     
     int input = getInteger(0,59);
     
     return 0;
} // END OF MAIN

//function definition
int getInteger(int min, int max)
{
	int intNum = 0;
	cin >> intNum;

	bool isNotInt = cin.fail() == 1;

       // note use of "short circuit" logical operation here
	while (isNotInt || intNum < min || intNum > max) 
	{
		cout << " Bad entry! Enter a number between " 
                       << min << " to " << max << ": ";
		cin.clear();
		cin.ignore(numeric_limits<streamsize>::max(), '\n'); 
		cin >> intNum;
		isNotInt = cin.fail() == 1;
	}
	cin.get(); // Get the last ENTER
	cin.clear();

	return intNum;
}


Last edited on Mar 24, 2017 at 11:55am
Mar 24, 2017 at 12:47am
Thanks so much!
Thomas1965- I wasn't able to get your code to work, although I may have missed something.

Blongho- I used yours to do what I needed to as it'll be more versatile.
Mar 24, 2017 at 9:41am
If this was to be done using a void parameter instead of a function so it can loop through a couple of options, how would I go about that? I've been googling and searching for an hour but must be missing something :(

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <limits>
#include <cmath>
#include <numeric>


using namespace std;
void get_data(int min, int max, int &input, string type);


int main() {

int min, max, input;
string type;

//Input year & validate
int year;
get_data(1970, 2020, 2, 'year');
year = input;

return 0;
}


void get data(int min, int max, int &input, string type){
    int input;
    cout << "Please enter the " << type;    
    cin >> input;
    bool isNotInt = cin.fail() == 1;


    while (isNotInt || input < min || input > max){
        cout << min <<" <= " << type << " <= " << max <<"!\n";
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
        cout << "Please enter the " << type;    
        cin >> input;
        isNotInt = cin.fail() == 1;
    }
    cin.get();
    cin.clear();


}


I'm getting these errors and I'm having no luck deciphering them to know where on earth I'm going wrong

task_1_3.cpp:25:25: warning: multi-character character constant [-Wmultichar]
task_1_3.cpp: In function 'int main()':
task_1_3.cpp:25:31: error: invalid initialization of non-const reference of type 'int&' from an rvalue of type 'int'
task_1_3.cpp:15:6: error: in passing argument 3 of 'void get_data(int, int, int&, std::string)'
task_1_3.cpp:20:5: warning: unused variable 'min' [-Wunused-variable]
task_1_3.cpp:20:10: warning: unused variable 'max' [-Wunused-variable]
task_1_3.cpp: At global scope:
task_1_3.cpp:137:10: error: expected initializer before 'data'


Appreciate your help!

Mar 24, 2017 at 12:07pm
@NeedsaLife, what do you intend to do with
 
void get_data(int min, int max, int &input, string type);?


What do you expect in line 27?

In a nutshell, you have to tell us what you are trying to do altogether. I understood that you wanted to get an int and verify that it is in range. But now, i don't understand your need anymore.

Please provide more info so you can get the help you need.
Mar 24, 2017 at 12:30pm
My apologies. The data is being stored and then output at the end as below. Basically the code is to request a valid date & time then output it for review.
I'm getting very lost by these functions and voids, unfortunately the more I read the more confused I get.

1
2
3
4
5
6
7
8
cout << fixed << setprecision(2);
cout << "Your inputs are: \n" ;
cout << setw(10) << left << "Year" << "= " << year <<"\n";
cout << setw(10) << left << "Month" << "= " << month <<"\n";
cout << setw(10) << "Day" << "= " << day <<"\n";
cout << setw(10) << "Hour" << "= " << hour <<"\n";
cout << setw(10) << "Minute" << "= " << minutes <<"\n";
cout << setw(10) << "Second" << "= " << sec <<"\n";
Mar 25, 2017 at 2:32pm
@NeedsafeLife
You are trying to do many things at the same time which makes it difficult for you.

Break your program like this
[pseudocode]
1. Start
2. Get year
2.1 validate year
2.2 store valid year
3. Repeat 2 to 2.2 for month, day, hour, minutes and seconds
4. Print results on screen
5. End program

1
2
3
4
5
6
7
8
9
10
int yr = getInteger(1970, 2020); // if 2020 is max limit for year
int mn = getInteger(1,12); // month

/*you can convert months from int to string (Jan to Dec) using 
a series of if---else or 
an enum(if u can) or 
switch statements */

// Hope you got more insight into your program now


Ps: Sorry for late feedback.
Last edited on Mar 25, 2017 at 2:34pm
Topic archived. No new replies allowed.