CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jan 2017
    Posts
    4

    How can I use my current code to prompt the computer to output back a series of MIDI

    I'm still new to C++ so im still trying to get my head around a few things. All my validation works fine. The user must input a MIDI note that consists of at 2-3 characters, be between A-G, the last character must be a digit, and if the length is 3 the middle character must be a sharp(#).

    What I must do now is allow the user to enter a series of notes for both MELODY and BASS, to then store them, to then output them back to the user in order they entered them. At the moment the user can only enter 2 notes before the program closes itself. What should I do?

    Code:
    using namespace std;
    #include <iostream>
    #include <cstdlib>
    
    using namespace std;
    
    //********START UP MENU*******//
    
    int StartUpMenu()
    {
        while (1)
        {
            int choice;
    
            cout <<"::menu option::\n\n"
                <<"1. Bass\n"
                <<"2. Melody\n"
                <<"3. Exit\n"
                <<"Would you like to work with Bass or Melody first? Please enter 1 for Bass or 2 for Melody or 3 to Exit:";
    
            cin >> choice;
    
            if(choice == 3) break;
    
    
            else if (choice == 1)
            {
                system ("CLS");
                cout<<"1. Bass \n\n";
                system ("PAUSE");
                system ("CLS");
            }
    
            else if (choice == 2)
            {
                 system ("CLS");
                cout<<"2. Melody \n\n";
                system ("PAUSE");
                system ("CLS");
            }
    
            else if (choice > 3 || choice < 1)
            {
                system ("CLS");
                cout<<"2. Melody \n\n";
                system ("PAUSE");
                system ("CLS");
            }
    
        }
    
        return 0;
    }
    
        //********VALIDATION FOR NOTE NAME********//
    
        string GetNote()
        {
            string input;
            int loop = 1;
            cout << "Hello, please type in a note: "<< endl;
            cin >> input;
            while (loop < 5000)
            {
    
                if (input.length() < 2 || input.length() > 3) //Step 1: If note name length is less than 2 OR more than 3, return false
                {
                cout<<"Note must be either 2 or 3 characters long!\n";
                cin >> input;
                }
    
                else if (((int)input[0] < 65)|| ((int)input[0] > 71 )) //Step 2: The note must be/(or be) between A and G
                {
                cout<<"The note must be between A to G!\n";
                cin >> input;
                }
    
                else if (input.length() == 2 && (isdigit(input[1]) == false))
                    //(isdigit(note[GetValidNote()-1]) == false) //Step 3: If true, the last character must be a digit
                {
                cout<<"Last character must be a digit!\n";
                cin >> input;
                }
                else if (input.length() == 3 && (isdigit(input[2]) == false))
                    //(isdigit(note[GetValidNote()-1]) == false) //Step 3: If true, the last character must be a digit
                {
                cout<<"Last character must be a digit!\n";
                cin >> input;
                }
    
                 else if (input.length() == 3 && input[1] !='#') //Step 4: If note length is 3 note[1] (character 2) must be '#'.
                {
                cout<<"Invalid sharp note\n";
                cin >> input;
                }
                else
                {
                    return input;
                }
            }
    
        }
        //********GET MIDI********//
    
        int MidiStorage(string validnote)
        {
            int MidiLetter = validnote[0] - 65;
    
            int Note;
            int Octave = validnote[validnote.length()-1]; //THIS IS A MATHMATICAL EQUATION TO GET THE COMPUTER TO REALISE WHAT AN OCTAVE IS USING THE MIDI NOTE CHART.
    
            if(validnote[0] == 'A')
    
            {
                MidiLetter = 9;
            }
    
            else if (validnote[0] == 'B')
    
            {
                MidiLetter = 11;
            }
    
            else if (validnote[0] == 'C')
    
            {
                MidiLetter = 12;
            }
    
            else if (validnote[0] == 'D')
            {
                MidiLetter = 14;
            }
    
            else if (validnote[0] == 'E')
            {
                MidiLetter = 16;
            }
    
            else if (validnote[0] == 'F')
            {
                MidiLetter = 17;
            }
    
            else if (validnote[0] == 'G')
            {
                MidiLetter = 19;
            }
            /////////////////////
    
            int midivalue = MidiLetter + (Octave * 12);
            if(validnote.length() == 3)
            {
                midivalue += 1;
            }
    
        }
    
        //Validation
    
        int main()
        {
        string note;
        int midivalue;
        StartUpMenu(); //This function brings you to the start up menu
        note = GetNote(); //This function tells the computer what note between A-G and  the user has given
        cout << "The note you chose is: " << note << endl;
        MidiStorage(note);
        GetNote();
        return 0;
        }
    Last edited by 2kaud; January 11th, 2017 at 09:02 AM. Reason: Code tags added

  2. #2
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,923

    Re: How can I use my current code to prompt the computer to output back a series of M

    When posting code, please use code tags so that the code is readable. Go Advanced, select the formatted code and click '#'.

    I'll have look at the code later today.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Jan 2017
    Posts
    4

    Re: How can I use my current code to prompt the computer to output back a series of M

    Thanks for that, I was trying to figure out how to do that myself but you got there before me!

    I am only willing to accept any sort of help from people online! I will be grateful for what ever help I receive. Thanks.

  4. #4
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,923

    Re: How can I use my current code to prompt the computer to output back a series of M

    MidiStorage() doesn't actually return a value. Do you want to return midivalue?

    Do you want to store them as the chars entered - or as the midivalue? Do you need to access the entered notes randomly or only in the order entered?

    To store the entered values, you can use one of the c++ STL containers. Vector is probably the easiest.

    Do you need to also store whether a note is a bass note or a melody note - or do you need to store these separately?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #5
    Join Date
    Jan 2017
    Posts
    4

    Re: How can I use my current code to prompt the computer to output back a series of M

    I would like to store them as midivalue.

    The notes don't have to be in the order entered but I would prefer if they were. How would I go about using a Vector in this instance?

    I also need to store the bass notes and the melody notes separately.

  6. #6
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,923

    Re: How can I use my current code to prompt the computer to output back a series of M

    For info on vector, see http://www.cplusplus.com/reference/vector/vector/

    Sample code to obtain 10 notes and store them in a vector as midivalue and then to display the values in the vector is
    Code:
    int main()
    {
    	StartUpMenu(); //This function brings you to the start up menu
    
    	vector<int> notes;
    
    	for (int i = 0; i < 10; ++i) {
    		string note = GetNote(); //This function tells the computer what note between A-G and  the user has given
    		cout << "The note you chose is: " << note << endl;
    		notes.push_back(MidiStorage(note));
    	}
    
    	for (const auto& n : notes)
    		cout << n << " ";
    
    	cout << endl;
    
    	return 0;
    }
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  7. #7
    Join Date
    Jan 2017
    Posts
    4

    How can I use my code to take the input from the user to then output it back?

    What I am doing here is taking a number of MIDI values from the user and storing it in an array. What I would now like to do is output what the user typed in back to the user.



    Code:
    using namespace std;
    #include <iostream>
    #include <cstdlib>
    #include <string>
    
    using namespace std;
    //int notenumber
    
    int MidiStorage(string validation);
    
    struct Note
    {
    	string name;
    	int midiNumber;
    	int length;
    };
    
    //**************************************************************VALIDATION NOTE LENGTH***********************************************//
    
    /*while ValidateNoteLength(int length)//Step 1 - If notelength is not a digit then it will return false.
    
    {
    	if (length == false)
    	{
    		cout << "Note length must be a digit/number, please re-enter";
    		return false;
    	}
    	if (length <= 0 || length > 16) //Step 2 - If notelength is less than or equal to 0 or more than 16 return false.
    	{
    		cout << "Note length value cannot be less than 1 or more than 16, please re-enter";
    		return false;
    	}
    	return true;
    }*/
    
    //******************************************************VALIDATION FOR NOTE NAME**********************************************//
    string GetNote()
    {
    	string input;
    	int loop = 1;
    	cout << "Hello, please type in a note: " << endl;
    	cin >> input;
    	while (1)
    	{
    
    		if (input.length() < 2 || input.length() > 3) //Step 1: If note name length is less than 2 OR more than 3, return false
    		{
    			cout << "Note must be either 2 or 3 characters long!\n";
    			cin >> input;
    		}
    
    		else if (((int)input[0] < 65) || ((int)input[0] > 71)) //Step 2: The note must be/(or be) between A and G
    		{
    			cout << "The note must be between A to G!\n";
    			cin >> input;
    		}
    
    		else if (input.length() == 2 && (isdigit(input[1]) == false))
    			//Step 3: If true, the last character must be a digit
    		{
    			cout << "Last character must be a digit!\n";
    			cin >> input;
    		}
    		else if (input.length() == 3 && (isdigit(input[2]) == false))
    			//Step 3: If true, the last character must be a digit
    		{
    			cout << "Last character must be a digit!\n";
    			cin >> input;
    		}
    
    		else if (input.length() == 3 && input[1] != '#') //Step 4: If note length is 3 note[1] (character 2) must be '#'.
    		{
    			cout << "Invalid sharp note\n";
    			cin >> input;
    		}
    		else
    		{
    			return input;
    		}
    	}
    
    }
    
    //**************************************************************START UP MENU****************************************************//
    
    
    void StartUpMenu()
    {
    	const int numNotes = 4;
    	Note melody[numNotes];
    	Note bass[numNotes];
    
    	while (1)
    	{
    		int choice;
    
    		cout << "::menu option::\n\n"
    			<< "1. Bass\n"
    			<< "2. Melody\n"
    			<< "3. Exit\n"
    			<< "Would you like to work with Bass or Melody first? Please enter 1 for Bass or 2 for Melody or 3 to Exit:";
    
    		cin >> choice;
    
    		if (choice == 3) break;
    
    
    		if (choice == 1)
    		{
    			system("CLS");
    			cout << "1. Bass \n\n";
    
    			for (int i = 0; i < numNotes; i++)
    			{
    				
    
    				cout << "Bass\n";
    
    				bass[i].name = GetNote();
    				cin >> bass[i].length;
    				bass[i].midiNumber = MidiStorage(bass[i].name);
    			}
    
    			system("PAUSE");
    			system("CLS");
    
    
    		}
    
    		else if (choice == 2)
    		{
    			system("CLS");
    			cout << "2. Melody \n\n";
    
    			for (int i = 0; i < numNotes; i++)
    			{
    				cout << "Melody\n";
    
    				melody[i].name = GetNote();
    				cin >> melody[i].length;
    				melody[i].midiNumber = MidiStorage(melody[i].name);
    
    				//cout << melody[i].name;
    			}
    
    			system("PAUSE");
    			system("CLS");
    		}
    
    	}
    
    	return;
    }
    
    
    //*****************************************************************************VALIDATION FOR NOTE NAME**********************************************************//
    
    
    
    int MidiStorage(string validnote)
    {
    	int MidiLetter = validnote[0] - 65;
    
    	int Note;
    	int Octave = validnote[validnote.length() - 1] - 48; //THIS IS A MATHMATICAL EQUATION TO GET THE COMPUTER TO REALISE WHAT AN OCTAVE IS USING THE MIDI NOTE CHART.
    
    	if (validnote[0] == 'A')
    
    	{
    		MidiLetter = 9;
    	}
    
    	else if (validnote[0] == 'B')
    
    	{
    		MidiLetter = 11;
    	}
    
    	else if (validnote[0] == 'C')
    
    	{
    		MidiLetter = 12;
    	}
    
    	else if (validnote[0] == 'D')
    	{
    		MidiLetter = 14;
    	}
    
    	else if (validnote[0] == 'E')
    	{
    		MidiLetter = 16;
    	}
    
    	else if (validnote[0] == 'F')
    	{
    		MidiLetter = 17;
    	}
    
    	else if (validnote[0] == 'G')
    	{
    		MidiLetter = 19;
    	}
    
    	int midivalue = MidiLetter + (Octave * 12);
    	if (validnote.length() == 3)
    	{
    		midivalue += 1;
    	}
    
    	return midivalue;
    }
    
    
    //*******************************************MAIN FUNCTIONS******************************************//
    
    int main()
    
    
    {	
    	
    
    StartUpMenu();
    	
    		return 0;
    }
    Last edited by DomDienst; January 12th, 2017 at 10:37 AM.

  8. #8
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,923

    Re: How can I use my current code to prompt the computer to output back a series of M

    As an example for Bass, consider
    Code:
    for (int i = 0; i < numNotes; i++)
    {
        cout << "Bass\n";
        bass[i].name = GetNote();
        cin >> bass[i].length;
        bass[i].midiNumber = MidiStorage(bass[i].name);
    }
    
    for (int i = 0; i < numNotes; ++i)
        cout << bass[i].name << " " << bass[i].length << " " << bass[i].midiNumber << endl;
    Note that the arrays melody and bass are local to the function StartUpMenu() and so can't be accessed outside of this function. If they need to be (eg in main()), then they either need to be defined ad global, or defined within main() and passed as parameters to the function.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured