Skip to content

Instantly share code, notes, and snippets.

@stungeye
Created September 8, 2022 19:32
Show Gist options
  • Save stungeye/2cf13203246c81a5f9d19a439055c6d3 to your computer and use it in GitHub Desktop.
Save stungeye/2cf13203246c81a5f9d19a439055c6d3 to your computer and use it in GitHub Desktop.
A simple console based calculator with data validation.
#include <iostream>
#include <limits>
int main()
{
int number1 , number2;
char sign;
while (true) {
std::cout << "Enter 2 numbers seperated by a + or - sign \n";
if (!std::cin.fail() && (sign == '-' || sign == '+')) {
break;
}
else {
std::cin.clear(); // Clears the fail bit.
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
}
if (sign == '-') {
number2 *= -1;
}
std::cout << "Your answer is " << number1 + number2;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment