Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * @file pi.cpp
- * @brief Calculates pi for the given number of terms
- * @author syb0rg
- * @date 10/3/14
- */
- #include <iostream>
- #include <limits>
- #include <cctype>
- #include <cmath>
- /**
- * Makes sure data isn't malicious, and signals user to re-enter proper data if invalid
- */
- void getSanitizedDouble(long double &input)
- {
- while (!(input = std::cin.peek()) && input != 'n')
- {
- if (std::isalpha(input) || std::isspace(input)) std::cin.ignore(); // ignore alphabetic and space characters from input
- }
- while(!(std::cin >> input) || input < 0)
- {
- std::cin.clear(); // clear the error flag that was set so that future I/O operations will work correctly
- std::cin.ignore(std::numeric_limits<std::streamsize>::max(), 'n'); // skips to the next newline
- std::cout << "Invalid input. Please enter a positive number: ";
- }
- }
- int main()
- {
- long double num = 0.;
- char again = '