Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/148.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 为什么我的打开菜单在C++;?_C++ - Fatal编程技术网

C++ 为什么我的打开菜单在C++;?

C++ 为什么我的打开菜单在C++;?,c++,C++,目前我正在编译一个程序来做 1.提示用户为旋律和低音输入固定数量的MIDI音符 这就是打开菜单的目的。但是,程序不会继续提示用户输入注释,除非按下选项“3”,如您所见,该选项是退出选项。我需要实现这段代码,以允许用户为1输入一系列注释。旋律,然后是2。低音的如果我按1,为什么不带我去验证呢。或2。但只有当我按3键时 using namespace std; #include <iostream> #include <cstdlib> using namespace st

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

这就是打开菜单的目的。但是,程序不会继续提示用户输入注释,除非按下选项“3”,如您所见,该选项是退出选项。我需要实现这段代码,以允许用户为1输入一系列注释。旋律,然后是2。低音的如果我按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;
}
使用名称空间std;
#包括
#包括
使用名称空间std;
//********拿迷笛********//
字符串GetNote()
{
字符串输入;
int循环=1;
不能输入;
而(1)
{
if(input.length()<2 | | input.length()>3)//步骤1:如果注释名称长度小于2或大于3,则返回false
{
不能输入;
}
else if(((int)input[0]<65)| |((int)input[0]>71))//步骤2:注释必须在A和G之间/(或是)
{
不能输入;
}
else if(input.length()==2&&(isdigit(input[1])==false))
//(isdigit(注意[GetValidNote()-1])==false)//步骤3:如果为true,则最后一个字符必须是数字
{
不能输入;
}
else if(input.length()==3&&(isdigit(input[2])==false))
//(isdigit(注意[GetValidNote()-1])==false)//步骤3:如果为true,则最后一个字符必须是数字
{
不能输入;
}
else if(input.length()==3&&input[1]!='#')//步骤4:如果音符长度为3音符[1](字符2)必须为'#'。
{
不能输入;
}
其他的
{
返回输入;
}
}
}
//********启动菜单*******//
int启动菜单()
{
而(1)
{
智力选择;

cout您没有在代码中的任何地方调用函数GetNote。

只有在选项3中,您从循环中执行中断操作,该操作从循环中返回,这样您的程序才能继续。选项1和2(以及默认值)只显示选项并停留在循环中,因此会询问一个新选项。当给出选项1和2时,您可以中断,但您可能希望返回该选项,以便您的程序可以对其进行操作


顺便说一下,您的代码中有一个输入错误:
否则如果(choice==1;)
不应该包含“;”。

不完全是一个解决方案,
switch case
对您来说是一个更好的选择。您能给我举个例子吗?对于一小部分可能的情况,
switch case
是O(1)TC,更简洁。我想您希望从
StartUpMenu
返回一个有用的值,这样您就知道选择是什么了。您能提供完整的代码吗,除非它分布在多个文件中我已经更改了代码,所以现在当您按2或3时,它会移到GetNote()上函数。我应该怎么做才能允许“3”的命令取消程序?如果返回给定选项,则可以在StartupMenu()时退出返回3I我已经纠正了这一点,但现在当您选择继续使用低音或旋律的选项时,您只允许输入一个值,如果用户想要输入更多值,该怎么办?如何纠正?