//----------------------------------------------------- // ---hw2.cpp--- // ID#: 10164771 // Edward Cory epcr2f@umr.edu // CS53 2-12-2003 //----------------------------------------------------- // This program tries to guess a number that a user // enters that is between 1 & 100. It continues untill // the correct number is guessed then prompts the user // if they want to play again. //----------------------------------------------------- // Revisions: // // v1.0 Initial build //----------------------------------------------------- #include using namespace std; int main() { //initialize variables int i_High, i_Low, i_NumGuess; char c_Again, c_GuessYHL; do { i_High=100; i_Low=1; cout << "Pick an integer from 1 to 100 and I will try to guess it. Respond with Y if my guess"; cout << endl << "is right, H if it is too high, or L if my guess is too low." << endl << endl; // Following while loop contains all too high/low comments and math while (c_GuessYHL!='Y') { i_NumGuess = (i_High+i_Low)/2; cout << "Is your number " << i_NumGuess << "?" << endl; cin >> c_GuessYHL; // Following requires the user to enter capital H/L/Y's while ((c_GuessYHL!='H') && (c_GuessYHL!='L') && (c_GuessYHL!='Y')) { cout << "I don't understand; please type Y, H, or L." << endl; c_GuessYHL='K'; cin >> c_GuessYHL; }; // Checks if computer guess was too high or low if (c_GuessYHL == 'H') { i_High = i_NumGuess--; cout << "Too high, huh. "; } if (c_GuessYHL == 'L') { i_Low = i_NumGuess++; cout << "Too Low, huh. "; } }; // Checks if the user wants to play again cout << "I knew I could do it!" << endl << endl << "Do you want to play again? Type Y or N." <> c_Again; while ((c_Again != 'N') && (c_Again != 'Y')) { cout << "I don't understand; please type Y or N." << endl; cin >> c_Again; }; } while (c_Again == 'Y'); return 0; }