当前,我正在尝试通过将string与switch一起使用来获取用户输入,但是编译器很生气,它给出了异常,并且由于未知错误而关闭。这是我正在尝试的代码。
#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;
}
}
用string day;
替换int day;
。
用string day
替换int day
,或者在进入switch
之前,先用day
将string
从int
转换为std::stoi()
。