3
我正處於長項目的最後一個障礙。最後一步是將一串noteNames轉換爲MIDI編號,並將這些編號輸出回用戶。我寫了一個合適的代碼將字符(C-B)轉換爲MIDI編號,但是現在我需要將這些編號輸出回給用戶。我不確定在哪裏存儲它們以便能夠回想起它們......任何指針都會很棒!我是一個絕對的編碼愛好者,所以請原諒任何無知。此外,由於某些原因,當我從應用程序複製並粘貼代碼時,它會破壞佈局,因此請提前致歉。向用戶輸出MIDI編號
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int notenumber;
struct noteStorage {
string noteName;
string bassnoteName;
int midiNumber;
int noteLength;
int bassnoteLength;
}
;
///////////////////////////////////////////////////////////////////////////////////////////////
//VALIDATION FOR NOTE NAME
bool ValidateNote(string note) {
// Step 1: If note name length is less than 2 OR more than 3, return false
if (note.length() <2 || note.length() >3) {
cout<<"Note length must be 2 or 3 characters\n";
return false;
}
//Step 2: If true, the note must be/(or be) between A and G
else if(tolower(note[0])<'a' || tolower(note[0]) >'g') {
cout<<"Note must be A-G\n";
return false;
}
//Step 3: If true, the last character must be a digit
else if(isdigit(note[note.length()-1])==false) {
cout<<"Last character must be a digit\n";
return false;
}
//Step 4: If note length is 3 note[1] (character 2) must be '#'.
else if(note.length()==3 && note[1] !='#') {
"Invalid sharp note\n";
return false;
}
return true;
}
/////////////////////////////////////////////////////////////////////////////////////////
//VALIDATION FOR NOTE LENGTH
bool ValidateNoteLength (int length)//Step 1 - If notelength is not a digit, return FALSE
{
if (length==false) {
cout<<"Note length must be a digit/number, please re-enter";
return false;
}
//Step 2 - If notelength is less than or equal to 0 or more than 16, return FALSE
if (length <=0 || length > 16) {
cout<<"Note length value cannot be less than 1 or more than 16, please re-enter";
return false;
}
return true;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
int CalculateNoteNumber(string tempName) {
int Octave;
int notenumber;
tempName[0]=toupper(tempName[0]);
Octave=((tempName[tempName.length()-1]) -48) * 12;
if (tempName.length()==2) {
if(tempName[0]=='C') {
return notenumber=0;
}
else if(tempName[0]=='D') {
return notenumber=2;
}
else if(tempName[0]=='E') {
return notenumber=4;
}
else if(tempName[0]=='F') {
return notenumber=5;
}
else if(tempName[0]=='G') {
return notenumber=7;
}
else if(tempName[0]=='A') {
return notenumber=9;
}
else {
return notenumber=11;
}
}
else if (tempName.length()==3) {
if(tempName[0]=='C') {
return notenumber=1;
}
else if(tempName[0]=='D') {
return notenumber=3;
}
else if(tempName[0]=='F') {
return notenumber=6;
}
else if(tempName[0]=='G') {
return notenumber=8;
}
else {
return notenumber=10;
}
}
}
int main() {
noteStorage noteData[8];
//string note;
for (int i=0;
i < 8;
i++) {
cout<<"Please enter melody note: " << i <<": ";
while (1) {
string tempName;
cin>>tempName;
int noteNumber=CalculateNoteNumber(tempName);
if (ValidateNote(tempName)==true) {
noteData[i].noteName=tempName;
break;
}
else {
cout <<"Please enter correctly: ";
}
}
//end first while
cout<<"Please enter note length: ";
while (1) {
int tempLength;
cin>>tempLength;
if (ValidateNoteLength(tempLength)==true) {
noteData[i].noteLength=tempLength;
break;
}
else {
cout <<"Please enter correctly: ";
}
}
//end while 2
cout<<"Thank you\n";
////////////////////////////////////////////////////////////////////////
}
// Bass Melody Input
{
noteStorage noteData[8];
//string note;
for (int i=0;
i < 8;
i++) {
cout<<"Please enter bass melody note: " << i <<": ";
while (1) {
string basstempName;
cin>>basstempName;
int noteNumber=CalculateNoteNumber(basstempName);
if (ValidateNote(basstempName)==true) {
noteData[i].bassnoteName=basstempName;
break;
}
else {
cout <<"Please enter correctly: ";
}
}
//end first while
cout<<"Please enter note length: ";
while (1) {
int basstempLength;
cin>>basstempLength;
if (ValidateNoteLength(basstempLength)==true) {
noteData[i].bassnoteLength=basstempLength;
break;
}
else {
cout <<"Please enter correctly: ";
}
}
//end while 2
cout<<"Thank you\n";
}
////////////////////////////////////////////////////////////////////////
}
//end for
cout<<"Your melody notes and note lengths are: "<<endl;
for (int i=0;
i < 8;
i++) {
cout<<noteData[i].noteName<<"Length: ";
cout<<noteData[i].noteLength<<endl;
}
cout<<"Your bass notes and note lengths are: "<<endl;
for (int i=0;
i < 8;
i++) {
cout<<noteData[i].bassnoteName<<"Length: ";
cout<<noteData[i].bassnoteLength<<endl;
}
/*system("pause");
return 0;
*/
}
就是這樣,除了我想在項目結束時輸出已存儲的所有音符的midi number equivelants。與輸出音符長度和音符名稱的位置相同 – Hushhh 2015-02-01 16:52:58