I am a little confused on how to test user input with the correct answer of a complementary bit. A labby told us just to AND it with 255, but I do not understand why we are doing that.
byte1 is an randomly generated unsigned char from 0 - 255
userInput is an intiger
Code:// Asks for answer of ~byte1 con << "What is the COMPLEMENT result of " << int(byte1) << " ("; toBin(byte1); con << ")?\n"; con << "> "; if(!(cin >> userInput)) { cin.clear(); cin.ignore(20, '\n'); con << "Bad Input! Incorrect Answer.\n"; } else { // Checks to see if the answer is correct if (userInput == (~byte1 & 255)) { numCorrect++; con << "Correct!\n"; } else con << "False!\n"; cin.ignore(20, '\n'); }



LinkBack URL
About LinkBacks


