One of my assignments for class was to create a Guess my Number game using functions, passing parameters, validation, and writing to a file. I really do not use many comments in my code but I should start doing so.
Guess my Number game
- Asks user for their full name
- Asks user to set difficulty
- Asks user if they want to play
- User has five tries to guess
- Once user is finish, write to file with games played and games won with a score percentage.
Code:#include <iostream> #include <fstream> #include <string> #include <iomanip> using namespace std; //GUESS MY NUMBER W/FUNCTIONS BY CALL //Prototypes int getReply(); int getLevel(); int validateNumber(int, int); void printResults(string, int, int); int main() { //Housekeeping int number, guess, tries=0, level=0, upper=0, gamesPlayed=0, gamesWon=0; char reply; string playerName; srand((unsigned)time(0)); cout << "State your full name please: "; getline(cin, playerName); cout << "Would you like to play the guessing game? (Y)es or (N)o? "; reply = getReply(); cout << "Choose your level of difficulty (select a number from 1 to 10): "; level = getLevel(); upper = level * 10; cout << "\nLet's play!"; do { number=rand()%upper+1; gamesPlayed++; cout << "\n\nI am thinking of a number between 1 and " << upper << " \nCan you guess what it is? "; for(tries=5;tries>0;tries--) { cin >> guess; guess = validateNumber(guess, upper); if (guess < number) cout << "Your guess is low. Try again: "; else if (guess > number) cout << "Your guess is high. Try again: "; if (guess==number) { cout << "Congratulations " << playerName << "! You did it.!"; gamesWon++; break; } } cout << endl; if(guess!=number) cout << "Sorry " << playerName << ", The number was " << number << "."; cout << "\nWould you like to play again? "; reply = getReply(); }while(reply == 'Y'); //Loop/Play again printResults(playerName, gamesPlayed, gamesWon); system("type results.txt"); system("pause"); } //Functions int getReply() { char answer; cin >> answer; answer = toupper(answer); while(answer != 'Y' && answer != 'N') { cin.clear(); cin.ignore(80, '\n'); cout << "Error: must be Y or N: "; cin >> answer; answer = toupper(answer); } cin.clear(); cin.ignore(80, '\n'); return answer; } int getLevel() { int level; cin >> level; while(level < 1 || level > 10 || cin.fail()) { cin.clear(); cin.ignore(80,'\n'); cout << "Error: number must be from 1-10: "; cin >>level; } return level; } int validateNumber(int number, int upper) { while(number < 1 || number > upper || cin.fail()) { cin.clear(); cin.ignore(80,'\n'); cout << "Error: number must be from 1-" << upper << ": "; cin >> number; } return number; } void printResults(string playerName, int gamesPlayed, int gamesWon) { int percentage; percentage = (gamesWon/gamesPlayed)*100; ofstream fout("results.txt"); if(!fout.is_open()) { cout << "Error: could not find file."; system("pause"); exit(666); } fout << setw(30) << left; fout << "Name: " << playerName; fout << setw(30) << right << "Score: " << percentage << "%"; fout << endl << endl; fout << setw(30) << left << "Number of games played: " << gamesPlayed << endl; fout << setw(30) << "Number of games won: " << gamesWon << endl; cout << "\nAttention: saving to results.txt\n"; }



Reply With Quote![[showcase] guess my number with difficulty level](http://ragezone.com/hyper728.png)

