-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd_player_to_database.cpp
More file actions
56 lines (47 loc) · 1.23 KB
/
Copy pathadd_player_to_database.cpp
File metadata and controls
56 lines (47 loc) · 1.23 KB
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
#include "functions_definitions.h"
#include "conio.h"
#include <iostream>
// Modules for sleep_for function
#include <thread>
#include <chrono>
bool max(PlayersDatabase* database)
{
if (database->number_of_players == MAX_NUMBER_OF_PLAYERS)
{
gotoxy(1, 1);
std::cout << "Sorry, but we can't add another player.";
std::cout << "\nMaximum number of players = " << MAX_NUMBER_OF_PLAYERS << " going back...";
// Sleep for 2 seconds
std::this_thread::sleep_for(std::chrono::seconds(2));
return true;
}
return false;
}
void add_player_to_database(PlayersDatabase* d)
{
clrscr();
// Check if we are able to add the player
if (max(d))
return;
gotoxy(1, 1);
std::cout << "Give the length of the name: ";
int length;
length = getche();
length -= 48;
gotoxy(1, 2);
std::cout << "Give the name for a player ";
// Dynamically initialize the array of chars + 1 for '\0' character
char* name = new char[length + 1];
int zn;
for (size_t i = 0; i < length; ++i)
{
zn = getche();
name[i] = zn;
}
name[length] = '\0';
// Now create a brand new player
Player* new_player = new Player{ 0, 'R', 2, 0, 0, false, {}, 0, name };
// Add player to a database
d->players[d->number_of_players] = *new_player;
d->number_of_players++;
}