-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdice_simulator.cpp
More file actions
56 lines (49 loc) · 1.22 KB
/
Copy pathdice_simulator.cpp
File metadata and controls
56 lines (49 loc) · 1.22 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
/*
Dice simulator can simulator throwing dice :D
Really simple but Good for get started with vector or simple cpp libs.
I know I can write this code more simply,But,who care...
Created for everyone,By mortzaCFT
Copyright: free for anychanges.
*/
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
std::vector<int> roll_dice(int amount)
{
if (amount <= 0)
{
throw std::invalid_argument("Invalid amount");
}
std::vector<int> rolls;
srand(time(0));
for (int i = 0; i < amount; i++)
{
int random_roll = rand() % 6 + 1;
rolls.push_back(random_roll);
}
return rolls;
}
int main()
{
while (true)
{
try
{
std::string user_input;
std::cout << "How many dice would you like to roll? ";
std::cin >> user_input;
if (user_input == "exit")
{
std::cout << "Thanks for using it." << std::endl;
break;
}
std::cout << roll_dice(std::stoi(user_input)) << std::endl;
}
catch (std::invalid_argument const &e)
{
std::cout << "Please enter a valid number." << std::endl;
}
}
return 0;
}