You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++中如何跨switch case访问数组?菜单驱动程序问题求助

如何在switch case间共享数组变量

核心问题:变量作用域

你在case 1代码块里声明的myarray是局部变量,仅在当前{}包裹的范围内有效,执行完case 1的代码块后,这个数组的内存会被自动释放,其他case根本无法访问它,强行访问会触发未定义行为。另外,C++标准不支持int myarray[s]这种变长数组(这是C语言特性,部分编译器兼容但不推荐使用)。

解决方案:提升变量作用域 + 动态内存/容器

要让所有case都能访问数组,需要把数组的声明放到switch之外(比如main函数开头),同时因为数组大小由用户输入决定,得用动态内存分配或者更安全的std::vector容器。

方案1:使用动态数组(指针)

把数组指针声明在switch外,在case 1中分配内存,退出程序前记得释放内存,还要处理重复创建数组的情况(避免内存泄漏):

#include <iostream>
using namespace std;
int main()
{
   int c,a=0;
   int* myarray = nullptr; // 声明指针并初始化为空
   do
   {
       cout << "-----MENU------" << endl;
       cout << "1.Create" << endl;
       cout << "2.Display" << endl;
       cout << "3.Insert" << endl;
       cout << "4.Delete" << endl;
       cout << "5.Search" << endl;
       cout << "6.Exit" << endl;
       cin >> c;

       switch(c)
       {
           case 1:
           {
               // 若已有数组,先释放旧内存
               if(myarray != nullptr)
               {
                   delete[] myarray;
                   myarray = nullptr;
               }
               cout << "Enter the size of array :";
               int s;
               cin >> s;
               a=s;
               myarray = new int[s]; // 动态分配内存
               for(int i=0;i<s;i++)
               {
                   cout<<"Enter "<<i+1<<" element :";
                   cin>>myarray[i];
               }
               break; // 防止case穿透
           }
           case 2:
           {
               if(a==0 || myarray == nullptr)
               {
                   cout<<"Array is empty or not created"<<endl;
               }
               else
               {
                   cout<<"Array elements:"<<endl;
                   for(int i=0;i<a;i++)
                   {
                       cout<<myarray[i]<<" ";
                   }
                   cout<<endl;
               }
               break; // 防止case穿透
           }
           case 6:
           {
               // 退出前释放内存
               if(myarray != nullptr)
               {
                   delete[] myarray;
               }
               cout<<"Exiting program..."<<endl;
               return 0; // 结束程序
           }
           default:
           {
               cout<<"Invalid choice, try again"<<endl;
               break;
           }
       }
   }while(1);
}

方案2:使用std::vector(更推荐)

std::vector是C++标准库容器,自动管理内存,比动态数组更安全,无需手动释放:

#include <iostream>
#include <vector> // 引入vector头文件
using namespace std;
int main()
{
   int c;
   vector<int> myarray; // 声明空vector
   do
   {
       cout << "-----MENU------" << endl;
       cout << "1.Create" << endl;
       cout << "2.Display" << endl;
       cout << "3.Insert" << endl;
       cout << "4.Delete" << endl;
       cout << "5.Search" << endl;
       cout << "6.Exit" << endl;
       cin >> c;

       switch(c)
       {
           case 1:
           {
               cout << "Enter the size of array :";
               int s;
               cin >> s;
               myarray.clear(); // 清空原有内容
               myarray.resize(s); // 调整容器大小
               for(int i=0;i<s;i++)
               {
                   cout<<"Enter "<<i+1<<" element :";
                   cin>>myarray[i];
               }
               break;
           }
           case 2:
           {
               if(myarray.empty())
               {
                   cout<<"Array is empty or not created"<<endl;
               }
               else
               {
                   cout<<"Array elements:"<<endl;
                   for(int num : myarray) // 范围for循环遍历
                   {
                       cout<<num<<" ";
                   }
                   cout<<endl;
               }
               break;
           }
           case 6:
           {
               cout<<"Exiting program..."<<endl;
               return 0;
           }
           default:
           {
               cout<<"Invalid choice, try again"<<endl;
               break;
           }
       }
   }while(1);
}

额外修复点

  • 原代码case 2中的else()是语法错误,应改为else。
  • 所有case必须添加break,否则会触发case穿透(执行完当前case后继续执行下一个case)。
  • 原代码未处理case 6的退出逻辑,导致程序无限循环,需在case 6中用return 0结束程序。

内容的提问来源于stack exchange,提问作者NoobMaster_69

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.24 15:18:14