为什么我的打开菜单在C++不起作用

Why will my opening menu not work in C++?

本文关键字:C++ 不起作用 打开菜单 我的 为什么      更新时间:2023-10-16

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

这就是打开菜单的用途。但是,程序不会继续提示用户输入注释,除非按下选项"3",如您所见,这是EXIT选项。我需要实现这段代码以允许用户输入 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 noten";
        cin >> input;
        }
        else
        {
            return input;
        }
    }
}
//********START UP MENU*******//

int StartUpMenu()
{
    while (1)
    {
        int choice;
        cout <<"::menu option::nn"
            <<"1. Bassn"
            <<"2. Melodyn"
            <<"3. Exitn"
            <<"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 nn";
            GetNote();
            system ("PAUSE");
            system ("CLS");

        }
        else if (choice == 2)
        {
             system ("CLS");
            cout<<"2. Melody nn";
            GetNote();
            system ("PAUSE");
            system ("CLS");
        }
        else if (choice > 3 || choice < 1)
        {
            system ("CLS");
            cout<<"2. Melody nn";
            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;
}

仅在选项 3 中,您从循环中中断,该循环从循环返回,以便程序继续。 选项 1 和 2(以及默认值)仅显示选项并保持在循环中,因此会询问新选项。 当选项 1 和 2 给定时,您可以中断,但您可能希望返回该选项,以便您的程序可以对其进行操作。

顺便说一下,你的代码中有一个拼写错误:else if (choice == 1;)不应该包含';"。

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