-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.cpp
55 lines (44 loc) · 1.19 KB
/
utils.cpp
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
#include <iostream>
#include <string>
#include <limits>
#include <ctime>
#include <cstdlib>
#include "utils.h"
using namespace std;
// desplegar opciones
void displayOptions(string options[], int size) {
int option;
// mostrar opcion con su numero correspondiente
for (option = 0; option < size; option++) {
cout << option + 1 << ") " << options[option] << endl;
}
}
// obtener numero
int getNumber(string label) {
int number;
do {
// si fallo entrada
if (cin.fail()) {
cin.clear(); // manejar fallo de entrada
cin.ignore(numeric_limits<streamsize>::max(), '\n'); // ignorar linea
cout << "Entrada invalida\n" << endl;
}
cout << label;
cin >> number;
} while(cin.fail());
return number;
}
// obtener opcion
int getOption(string label, int min, int max) {
int option;
do {
option = getNumber(label);
} while(option < min || option > max);
return option;
}
// numero aleatorio en rango
int randomInRange(int min, int max) {
// semilla de numeros aleatorios
srand(time(NULL));
return min + (rand() % max);
}