// ********************************************************************* // Description: Guess The Number // ********************************************************************* #include #include #include #include using namespace std; int main() { int number; int guess; int sum = 0; int game = 0; char choice; fstream outFile; do { int count = 0; game++; srand(time(NULL)); number = rand() % 100 + 1; do { cout << "\nGuess an integer between 1-100: "; cin >> guess; cout << endl; count++; if (guess > number) { cout << guess << " is higher than the integer." << endl; } else if (guess < number) { cout << guess << " is lower than the integer." << endl; } else { cout << guess << " is the integer!" << endl; outFile.open("GuessTheNumberOutput.txt", ios::out | ios::app); outFile << number << " was the number, it took " << count << " guesses to get it right!\n"; outFile.close(); } } while (guess != number); cout << "\nDo you want to play another game (Y or N)? "; cin >> choice; sum += count; } while (choice == 'Y' || choice == 'y'); outFile.open("GuessTheNumberOutput.txt", ios::out | ios::app); outFile << "In " << game << " games it took an average of " << sum/float(game) << " guesses to get the correct number." << endl; return 0; }