为什么我的打开菜单在C++不起作用
Why will my opening menu not work in C++?
目前我正在编译一个程序来做
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。
相关文章:
- 我的神经网络不起作用 [XOR 问题]
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- C++为什么尽管我调用了void函数,它却不起作用
- 为什么在保护模式下继承升级不起作用
- 循环在计数器中不起作用
- 在其他文件中创建类时在 c++ 项目中不起作用
- Visual studio代码重构似乎不起作用(例如,重命名符号-f2)
- 为什么二进制搜索在我的测试中不起作用
- 我的代码中有错误吗?使用BGI图形的C++代码对我不起作用
- 为什么 const std::p air<K,V>& 在 std::map 上基于范围的 for 循环不起作用?
- 带有指定长度字符* 参数的 std::regex_search 在 VS2017 中不起作用?
- Bjarne Stroustrup Book - std_lib_facilities.h - 不起作用(未知类型名称)
- 为什么简单的算术减法在"if"条件下不起作用?
- 为什么Stroustup书中的has_f不起作用
- 你能检查一下为什么在这个代码中从链接列表中删除项目不起作用吗
- 嵌套While循环不起作用(C++问题)
- C++Matching Brackets 2解决方案不起作用
- 为什么这段代码不起作用,我该如何解决?
- 我正在开发服务器,ip作为参数传递不起作用