-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGlobalFunctions.cpp
More file actions
74 lines (63 loc) · 2.13 KB
/
GlobalFunctions.cpp
File metadata and controls
74 lines (63 loc) · 2.13 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
//
// Created by ABlack on 2/13/2020.
//
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <windows.h>
#include <cstdlib>
#include <cmath>
#include "Piano.h"
#include "GlobalFunctions.h"
#include <experimental/optional>
using std::experimental::optional;
using std::experimental::nullopt;
using std::experimental::make_optional;
using namespace std;
typedef optional<int> optInt;
bool firstCheck(string &input) {
const string notes = "CDEFGAB";
const string octaves = "12345678";
while(!(getline(cin,input))) {
cout << "Invalid input. Please follow the format." << endl;
cout << "Input (Enter X to exit):";
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
if(input != "X" && input != "Y") {
// Convert string input to a vector of characters
vector<char> inputVec(input.begin(), input.end());
if (inputVec.size() % 2 != 0) {
return false;
}
for (int i = 0; i < inputVec.size()/2; i++) {
if (notes.find(inputVec[2*i]) == string::npos || octaves.find(inputVec[2*i + 1]) == string::npos) {
return false;
}
}
}
return true;
}
void currentMusic(const unique_ptr<Piano> &piano){
cout << "\nYour current music:" << endl;
for (int i = 0; i < piano->getNotes().size(); i++) {
cout << piano->getNotes()[i] << piano->getOctaves()[i] << " ";
}
cout << "\nDelay: " << *piano->getDelay() << " milliseconds" << endl;
}
void playAgain(const unique_ptr<Piano> &piano, string &input, bool &valid){
while (input == "Y" || !valid) {
if (!valid) { // Invalid input. Continue until input is valid.
cout << "Invalid input. Please follow the format." << endl;
cout << "Input (Enter X to exit, or Y to play your music again):";
valid = firstCheck(input);
} else {
piano->play();
currentMusic(piano);
cout << "Go again? Input (Enter X to exit, or Y to play your music again):";
valid = firstCheck(input);
}
}
}