I've been building a menu for class, and I can't figure out how to stop calculations and output an error message when more inputs are given than asked for. This is a snippet of what I have(val1 and val2 are unsigned long int and inputError is a boolean):
case '&':
std::cout << "Enter values for first and second operand:" << std::endl;
std::cin >> val1 >> val2;
inputError = 0;
checkData1(val1, val2, inputError);
if (inputError) {
continue;
}
else {
result = val1 & val2;
std::cout << "Result: " << result;
std::cout << " [Hexadecimal: " << std::setw(bitsize) << std::hex << result << std::dec << "]" << std::endl;
}
break;
This is the void function (checkData1):
void checkData1(unsigned long int val1, unsigned long int val2, bool& inputError) {
if (std::cin.fail()) {
inputError = 1;
std::cout << "Input error, enter choice and values again." << std::endl;
std::cin.clear();
while (std::cin.get() != '\n') {
continue;
}
}
else if ((int)val1 < 0 || (int)val2 < 0) {
inputError = 1;
std::cout << "Input error, enter choice and values again." << std::endl;
}
}
And this is what I get:
Enter one of the following choices:
& - AND
| - OR
^ - XOR
~ - NOT
- - Negate
< - Left shift(logical shift only)
> - Right shift(logical shift only)
# - stop execution
&
Enter values for first and second operand:
1 2 3
Result: 0 [Hexadecimal: 0]
Enter one of the following choices:
& - AND
| - OR
^ - XOR
~ - NOT
- - Negate
< - Left shift(logical shift only)
> - Right shift(logical shift only)
# - stop execution
Invalid choice 3, please try again.
Enter one of the following choices:
& - AND
| - OR
^ - XOR
~ - NOT
- - Negate
< - Left shift(logical shift only)
> - Right shift(logical shift only)
# - stop execution
Instead of doing the calculations with the first two integers and giving an error with the third, I want it to ignore all of it, and put out an error message instead.
CodePudding user response:
You can add this to checkData1. This uses the cin.peek function to check if there's anything other than a new line character in the cin buffer, and if so it sets the error variable and clears out the buffer
void checkData1(unsigned long int val1, unsigned long int val2, bool& inputError) {
if (std::cin.fail()) {
inputError = 1;
std::cout << "Input error, enter choice and values again." << std::endl;
std::cin.clear();
while (std::cin.get() != '\n') {
continue;
}
}
else if ((int)val1 < 0 || (int)val2 < 0) {
inputError = 1;
std::cout << "Input error, enter choice and values again." << std::endl;
}
if(std::cin.peek() != '\n'){
inputError = 1;
std::cout << "Make sure to only put in two numbers" << std::endl;
while (std::cin.get() != '\n') {
continue;
}
}
}
CodePudding user response:
You can add this to checkData1. This uses the cin.peek function to check if there's anything other than a new line character in the cin buffer, and if so it sets the error variable and clears out the buffer
void checkData1(unsigned long int val1, unsigned long int val2, bool& inputError) {
if (std::cin.fail()) {
inputError = 1;
std::cout << "Input error, enter choice and values again." << std::endl;
std::cin.clear();
while (std::cin.get() != '\n') {
continue;
}
}
else if ((int)val1 < 0 || (int)val2 < 0) {
inputError = 1;
std::cout << "Input error, enter choice and values again." << std::endl;
}
if(std::cin.peek() != '\n'){
inputError = 1;
std::cout << "Make sure to only put in two numbers" << std::endl;
while (std::cin.get() != '\n') {
continue;
}
}
}