为什么我的开始菜单在C ++中不起作用?

时间:2017-01-11 14:38:41

标签: c++

目前我正在编制一个程序来做 1.提示用户为旋律和低音输入固定数量的MIDI音符

这是开场菜单的用途。但是,除非选项' 3' 3,否则程序将不会提示用户输入注释。按下,你可以看到是EXIT选项。我需要实现这段代码,以允许用户输入1. Melody然后2. Bass的一系列音符。如果我按下1.或2,为什么它不会让我进行验证。但只有当我按下3时才会进行验证。?

using namespace std;
#include <iostream>
#include <cstdlib>

using namespace std;
//********GET MIDI********//
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))
            //(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;
        }
    }

}

//********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";

            GetNote();

            system ("PAUSE");
            system ("CLS");


        }

        else if (choice == 2)
        {
             system ("CLS");
            cout<<"2. Melody \n\n";

            GetNote();

            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********//



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;
}

2 个答案:

答案 0 :(得分:2)

仅在选项3中,您从循环中断,从循环返回,以便您的程序继续。选项1和2(和默认值)只显示选择并保持在循环中,因此需要一个新的选择。您可以在给出选项1和2时中断,但您可能希望返回该选项,以便您的程序可以对其执行操作。

顺便说一句,您的代码中存在拼写错误:else if (choice == 1;)不应包含“;”。

答案 1 :(得分:0)

您没有在代码中的任何位置调用函数GetNote。