Hello Everyone! I built a game that's does high low guessing what I'm trying to do now with it is that I want to log the users stats in a log file after the program is finished. also I can seem to get the rogram to clear the consol after the user says yes to another game.
thank you!!
//Includers
#include <iostream>
#include <iomanip>
#include <ctime>
#include <cstdlib>
#include <fstream>
#include <Windows.h>
usingnamespace std;
//Function to print out the welcome banner
void printwelcome()
{
//Display Chart
cout << "******************************************" << endl;
cout << "**** Welcome to the High-Low game! ****" << endl;
cout << "******************************************" << endl;
// Display difficulty
cout << "Choose the difficulty level: " << endl;
cout << setw(16) << "1. Easy" << endl;
cout << setw(18) << "2. Medium" << endl;
cout << setw(16) << "3. Hard" << endl;
}
//This is the main function that contian the levels and user inputs
int main()
{
//game counter
int counter = 0;
//the function that generates random numbers
srand(time(NULL));
//the range of the random numbers
int number = 0;
//The Level storage
int lvl;
//The guess is stored here
int guess;
//The number of tries stored here
int tries = 0;
//Where the answer for yes or no
char answer;
//used to start the loops
char digit;
//used to start the loops
digit = 'm';
outputFile("HighLow.txt");
do {
//left over tries
int rightN = 0;
//generates a random number
number = rand() % 100 + 1;
//calls the welcome banner from the function created
printwelcome();
cout << "\n";
//used to show number generated for programers purpose and purpose for instuctor +++++++
cout << "********** Number Generated: " << number << " **********"<< endl;
//games count
counter++;
//Makes Sure to loop if someone doesnt enter a valid level number
while (true)
{
cout << '\n';
//display level that con be choosen
cout << "Enter level (1,2,3): ";
//user enters level here
cin >> lvl;
//if statement used to make sure a correct level was entered
if (lvl == 1 || lvl == 2 || lvl == 3)
{
break;
}
else
{
//Display a warning that a level number was invalide
cout << '\n';
cout << "***********THIS IS NOT A LEVEL***********" << endl;
cout << "Please choose a number 1-3 for difficulty" << endl;
}
}
//This is level Easy to user get 20 tries
if (lvl == 1)
{
//how many tries
tries = 20;
cout << '\n';
cout << "You choose Easy!" << endl;
//While loop that contains the level easy
while (tries <= 20 && tries >= 1
&& digit == 'm' || digit == 'M')
{
cout << "You have " << tries << " Tries" << endl;
//The user is asked for a guess
cout << "Enter a number between 1 and 100" << endl;
cout << '\n';
//Tell user to enter guess
cout << "Enter guess here: ";
//The guess is stored here
cin >> guess;
cout << '\n';
tries = tries - 1;
rightN++;
//If statement that produces an error message if user enters a number out of the peramiters
if (guess == 0 || guess > 100)
{
//Error message
cout << "This is not an option try again/n" << endl;
}
//If the user guesses the number
if (number == guess)
{
//tell user how many tries are left
cout << "Tries left: " << tries << endl;
//counter for how many tries are used
tries++;
//calculation for how many tries are left
tries = tries - 20;
cout << '\n';
//Message printed out if the user guesses correctly
cout << "Congratualtions!! " << endl;
//Lets the user know how many guess they used
cout << "You got the right number in " << rightN << " tries" << endl;
//log file
file << "Games played: " << counter << endl;
file << "Difficulty level: " << lvl << endl;
file << "Number of tries used: " << tries << endl;
}
//if the guess is to high
if (number < guess)
{
//This message prints if guess it to high
cout << "Too high\n" << endl;
}
//if the guess is to low
if (number > guess)
{
//This message prints if the guess is to low
cout << "Too low\n" << endl;
}
//If the user uses all their guesses
if (tries >= 20)
{
//The message that prints when a user is out of guesses
cout << "You've run out of tries!\n" << endl;
//log file
file << "Games played: " << counter << endl;
file << "Difficulty level: " << lvl << endl;
file << "Number of tries used: " << tries << endl;
}
}
}
//Ask user if theyd like to play the game again if not log game and exit
cout << "Do you want to restart Y/N ?";
cin.ignore(256, '\n');
answer = cin.get();
cout << '\n';
cout << '\n';
//used to replay
} while (answer == 'y' || answer == 'Y')
//display ending message
cout << '\n';
cout << "*+*+*+* To review your game check you HighLow.log file. *+*+*+*" << endl;
cout << "*+*+*+*+*+*+*+* Thank You for playing! *+*+*+*+*+*+*+*" << endl;
//file.close
outputFile.close();
//System pause here
system("pause");
return 0;
}