Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/136.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.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++_While Loop_Switch Statement - Fatal编程技术网

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

C++ 应用程序菜单中显示的错误,c++,while-loop,switch-statement,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>> selecti

当我使用
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.";

        }
    }

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

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

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

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

不能在
case
块内创建初始化变量,除非为它们创建本地作用域:

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

    case B:
    {
        int b = 0; <== ok
    }
    break;
}
开关(Foo)
{
案例A:

int a=0;您不能在
case
块内创建初始化变量,除非您为它们创建一个本地作用域:

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

    case B:
    {
        int b = 0; <== ok
    }
    break;
}
开关(Foo)
{
案例A:

int a=0;一旦case语句的代码超过两行或三行,就应该将其转换为一个单独的函数。尤其是对于这些嵌套菜单。其他答案是正确的,编译器正在抱怨在case语句中定义变量;代码审阅者会抱怨switch语句是错误的太复杂了。

一旦case语句的代码超过两行或三行,就应该将其转换为一个单独的函数。特别是对于这些嵌套菜单。其他答案是正确的,编译器抱怨在case语句中定义变量;代码审阅者会抱怨开关语句t太复杂了。

只是为了混淆问题,
int a;a=0;
就可以了。问题不是创建变量,而是初始化它们。我并不推荐这种方法…@PeteBecker:谢谢,我知道这一点。+1对于你的回答只是为了混淆问题,
int a;a=0;
就可以了。问题不是创建变量,但是初始化它们。我并不推荐这种方法…@PeteBecker:谢谢,我知道。
数组的+1看起来像一个类,而不是一个对象。
数组的可能副本看起来像一个类,而不是一个对象。数组的可能副本