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?
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');
}
// small code snippet to take and validate an integer
#include <iostream>
#include <numeric>
#include <limits>
usingnamespace 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;
}
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 :(
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'
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.
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.
@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