Created
September 8, 2022 19:32
-
-
Save stungeye/2cf13203246c81a5f9d19a439055c6d3 to your computer and use it in GitHub Desktop.
A simple console based calculator with data validation.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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