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

C++代码中switch语句失效,无法打印vector内容的问题求助

问题排查与解决

核心问题

你的switch语句无法正常执行的原因是变量selection没有正确获取用户输入的值:

  • ask函数的参数是值传递,函数内部对selection的修改只会作用于局部副本,不会影响main函数中的原变量。
  • 虽然ask函数返回了用户输入的字符,但main函数没有将返回值赋值给selection,导致selection始终是初始化的空字符,switch直接进入default分支输出error。

修复方案

提供两种可行的修复方式:

方式一:接收ask函数的返回值

调整ask函数的声明和调用,直接接收返回的输入值:

#include <iostream>
#include <vector>
using namespace std;

void printing(vector<int> numbers);
char ask(); // 修改函数声明,移除无用参数

int main(){
    vector<int> numbers{1,1,2};
    char selection = ask(); // 接收函数返回的输入值

    switch(selection) { 
      case 'P':
        printing(numbers);
        break;
      case 'M':
        cout << "FF" << endl;
        break;
      default :
        cout << "error" << endl;
    }
    printing(numbers);
    return 0;
}
    
char ask() // 函数内部声明变量,无需参数
{        
    char selection;
    cout << "Enter the selection " << endl;
    cout << "\nP - Print numbers" << endl;
    cout << "A - Add a number" << endl;
    cout << "M - Display mean of the numbers" << endl;
    cout << "S - Display the smallest number" << endl;
    cout << "L - Display the largest number"<< endl;
    cout << "Q - Quit" << endl;
    cout << "\nEnter your selection: ";
    cin  >> selection;
    return selection;
}
    
void printing(vector<int> numbers){
        
    if (numbers.size() == 0){
        cout << "[] - the list is empty" << endl;
    }
    else{
        cout << "[ " ;
        for (auto num : numbers){ cout << num << " " ; }
        cout << "] " << endl;
    }
}

方式二:使用引用传递参数

将ask函数的参数改为引用类型,直接修改main函数中的变量:

#include <iostream>
#include <vector>
using namespace std;

void printing(vector<int> numbers);
void ask(char &selection); // 修改为引用参数

int main(){
    vector<int> numbers{1,1,2};
    char selection{};
    ask(selection); // 直接修改main中的selection变量

    switch(selection) { 
      case 'P':
        printing(numbers);
        break;
      case 'M':
        cout << "FF" << endl;
        break;
      default :
        cout << "error" << endl;
    }
    printing(numbers);
    return 0;
}
    
void ask(char &selection) // 引用参数,无需返回值
{        
    cout << "Enter the selection " << endl;
    cout << "\nP - Print numbers" << endl;
    cout << "A - Add a number" << endl;
    cout << "M - Display mean of the numbers" << endl;
    cout << "S - Display the smallest number" << endl;
    cout << "L - Display the largest number"<< endl;
    cout << "Q - Quit" << endl;
    cout << "\nEnter your selection: ";
    cin  >> selection;
}
    
void printing(vector<int> numbers){
        
    if (numbers.size() == 0){
        cout << "[] - the list is empty" << endl;
    }
    else{
        cout << "[ " ;
        for (auto num : numbers){ cout << num << " " ; }
        cout << "] " << endl;
    }
}

额外优化建议

  • printing函数可以改为传递const vector<int> &numbers,避免vector的拷贝,提升效率:
    void printing(const vector<int> &numbers){
        // 原有逻辑不变
    }
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.19 07:20:37