为什么我的一个case常量与值匹配时,switch语句中的默认代码仍会执行?我目前正在使用Microsoft Visual Studio 2015,我似乎无法弄清楚为什么会这样。这是我的代码:
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
void ColaMachine()
{
char randomString;
string cola;
int number;
cout << "Hello, and welcome to ShadowTofu's cola machine!" << endl;
cout << "What is your choice of cola? Enter 1 for Coca Cola, enter 2 for Mountain Dew, enter 3 for Pepsi, enter 4 for Fanta, and enter 5 for Orange Crush." << endl;
cin >> number;
switch (number) {
case 1:
cola = "Coca Cola";
case 2:
cola = "Mountain Dew";
case 3:
cola = "Pepsi";
case 4:
cola = "Fanta";
case 5:
cola = "Orange Crush";
default:
cout << "Error, choice isn't valid! Here's your money back." << endl;
}
cout << "Here's your " << cola << "!" << endl;
cout << "Have a nice day!" << endl;
cin >> randomString;
}
int main()
{
ColaMachine();
return 0;
}