log game stats and clear consol

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!!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
//Includers
#include <iostream>
#include <iomanip> 
#include <ctime>
#include <cstdlib>
#include <fstream>
#include <Windows.h>
using namespace 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;
}
Last edited on
Topic archived. No new replies allowed.