在应用程序的菜单中显示错误

时间:2012-08-16 15:36:22

标签: 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`

3 个答案:

答案 0 :(得分:4)

一旦case语句的代码长度超过两行或三行,它就应该变成一个单独的函数。特别是这些嵌套菜单。其他答案是正确的,编译器抱怨在case语句中定义变量;代码审查员会抱怨switch语句过于复杂。

答案 1 :(得分:2)

你必须在switch语句中的新块({ })中包含变量定义,即

switch (var) {
case X: {
    bool var = here;
}
}

答案 2 :(得分:1)

除非为它们创建局部范围,否则无法在case块内创建初始化变量:

switch(Foo)
{
    case A:
    int a = 0; <== error
    break;

    case B:
    {
        int b = 0; <== ok
    }
    break;
}