diff options
Diffstat (limited to 'bitwise-operator-tests.cpp')
-rw-r--r-- | bitwise-operator-tests.cpp | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/bitwise-operator-tests.cpp b/bitwise-operator-tests.cpp new file mode 100644 index 0000000..f3f8a8f --- /dev/null +++ b/bitwise-operator-tests.cpp @@ -0,0 +1,74 @@ +#include <iostream> +#include <limits> + +/* + * + * + * + * + */ + +void cin_clear() { + + std::cin.clear(); + std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); +} + +int main () { + + unsigned long Quiz1 = 0; + unsigned long Student = 0; + unsigned long Pass = 0; + const int maxval = 31; + std::string choice; + while(1) { + + if(std::cin.eof()) { + std::cout << "Exiting..." << std::endl; + return -1; + } + + std::cout << "Would you like to 'add' a new student's result or 'print' out the total results?" << std::endl; + if(std::cin >> choice) {} else { + cin_clear(); + continue; + } + + if(choice == "add") { + std::cout << "Which student out of 30?" << std::endl; + if(std::cin >> Student) {} else { + cin_clear(); + continue; + } + + if(Student >= maxval || Student < 1) { + continue; + } + + std::cout << "Did this student pass? (0 == fail) (1 == pass)" << std::endl; + if(std::cin >> Pass) {} else { + cin_clear(); + continue; + } + + if(Pass) { + Quiz1 |= 1UL << Student; + } + + if(!Pass) { + Quiz1 &= ~(1UL << Student); + } + + std::cout << "Added result for student #" << Student + << ((Quiz1 & (1UL << Student)) ? ": passed" : ": failed") << std::endl; + } + + if(choice == "print") { + for(int i = 1 ; i != maxval ; ++i) { + std::cout << "Student #" << i << " " << ((Quiz1 & (1UL << i)) ? "passed" : "failed") << std::endl; + } + } + } + + return 0; +} |