C ++中是否可以通过switch语句使用字符串? [重复]

问题描述 投票:0回答:2

当前,我正在尝试通过将stringswitch一起使用来获取用户输入,但是编译器很生气,它给出了异常,并且由于未知错误而关闭。这是我正在尝试的代码。

#include <iostream>
using namespace std;
int main()
{
   string day;
   cout << "Enter The Number of the Day between 1 to 7 ";
   cin >> day;
  switch (day) {
  case 1:
    cout << "Monday";
    break;
  case 2:
    cout << "Tuesday";
    break;
  case 3:
    cout << "Wednesday";
    break;
  case 4:
    cout << "Thursday";
    break;
  case 5:
    cout << "Friday";
    break;
  case 6:
    cout << "Saturday";
    break;
  case 7:
    cout << "Sunday";
    break;
default:
    cout << "Attention, you have not chosen the Valid number to Identify weekly days from 1 to 7. Try again!" << endl;
 }

}
c++
2个回答
3
投票

string day;替换int day;


2
投票

string day替换int day,或者在进入switch之前,先用daystringint转换为std::stoi()

© www.soinside.com 2019 - 2024. All rights reserved.