c++ - 应用程序菜单中显示的错误

标签 c++ while-loop switch-statement

当我使用 while 循环和 switch 语句时,我的 main 中出现了一些错误。我检查了花括号,但没有发现拼写错误。谁能看出问题所在?

bool menu = true;
    while(menu)// Loop to revert back to menu when choice is not compatable with options.
    {
    int selection;
    cout<< "Choice: ";
    cin>> selection;

        switch(selection)
        {
        case 1:
            cout<< "View Elements.";
            Array.print();
            break;
        case 2:
            cout<< "Delete Element. ";
            break;
        case 3:
            cout<< "Delete All Elements. ";
            //Array.clear(OrderedArray);
            break;
        case 4:
            cout<< "Insert Element. ";
            //Array.Insert();
            break;
        case 5:
            cout<< "Search for Element. ";
            bool searchMenu = true;
            while(searchMenu)
            {
            int searchSelection;
            cout<< "Choice: ";
            cin>> searchSelection;

            switch(searchSelection)
            {
                case 1:
                cout<< "Linear Search. ";
                //Array.linearSearch();
                    break;

                case 2:
                cout<< "Binary Search ";
                //Array.binarySearch();
                    break;
            }

            }
            break;
        case 6:
            cout<< "Store. ";
            bool storeMenu = true;
            while(storeMenu)
            {
            int storeSelection;
            cout<< "Choice: ";
            cin>> storeSelection;

            switch(storeSelection)
            {
                case 1:
                cout<< "Write File ";
                //Array.writeFile();
                    break;

                case 2:
                cout<< "Read File ";
                //Array.readFile();
                    break;
            }
            }
            break;
        case 7:
            cout<< "Sort. ";
            Array.sort();
            break;
        case 8:
            cout << "Exit.";
            cout << "Please press Enter to exit.";

        }
    }

这些是错误:

error C2360: initialization of 'searchMenu' is skipped by 'case' label  c:\users\conor\documents\college\c++\projects\repeat - ordered array\repeat - ordered array\orderedarray.cpp    71  1   Repeat - Ordered Array

error C2360: initialization of 'storeMenu' is skipped by 'case' label   c:\users\conor\documents\college\c++\projects\repeat - ordered array\repeat - ordered array\orderedarray.cpp    94  1   Repeat - Ordered Array

error C2360: initialization of 'searchMenu' is skipped by 'case' label  c:\users\conor\documents\college\c++\projects\repeat - ordered array\repeat - ordered array\orderedarray.cpp    94  1   Repeat - Ordered Array

error C2360: initialization of 'storeMenu' is skipped by 'case' label   c:\users\conor\documents\college\c++\projects\repeat - ordered array\repeat - ordered array\orderedarray.cpp    98  1   Repeat - Ordered Array

error C2360: initialization of 'searchMenu' is skipped by 'case' label  c:\users\conor\documents\college\c++\projects\repeat - ordered array\repeat - ordered array\orderedarray.cpp    98  1   Repeat - Ordered Array`

最佳答案

一旦 case 语句的代码超过两三行,它就应该变成一个单独的函数。尤其是这些嵌套菜单。其他答案是正确的,编译器提示在 case 语句中定义变量;代码审查者会提示 switch 语句太复杂。

关于c++ - 应用程序菜单中显示的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11990749/

相关文章:

c++ - 为什么使用 'int' 数据类型而不是 'float' 数据类型?

c++ - 分区解决太慢

python - opencv中的while循环导致错误

c# - 有没有一种方法可以在 Switch 语句中执行某些代码,该语句仅在通过任何案例时才执行?

ios - 基于字符串的 Switch 语句?

c++ - 默认参数值错误 [Visual C++ 2008 中的错误?]

c++ - 错误 : multiple definiton of class function c++

Javascript while 循环返回值

php - 比较变量 foreach

php变量改变结果