任务:
编写一个程序,让用户选择你最喜欢的5种饮料(可乐,水,雪碧,……等等)。然后允许用户通过输入数字1-5来选择饮料.输出他们选择的饮料。★如果编程使用If语句而不是开关语句,则修改它以使用switch语句。如果您的程序使用开关语句,则修改它以使用If / modify语句。★★修改程序,以便如果用户输入的选择不是1-5,那么它将输出“Error.select无效,这是您的钱回来”。
#include <iostream>
using namespace std;
int main() {
int drink = 0;
string ifswitch;
string again = "y";
while( again == "y" || again == "Y")
{
cout << "Would you like to use if or switch statements?: ";
cin >> ifswitch;
cout << endl;
if(ifswitch == "switch" || ifswitch == "Switch")
{
cout << "Now using switch statements..." << endl << endl;
cout << "Your choices of drinks are: " << endl;
cout << "1 - Coke" << endl;
cout << "2 - Sprite" << endl;
cout << "3 - Water" << endl;
cout << "4 - Orange Juice" << endl;
cout << "5 - Apple Juice" << endl;
cout << "What drink would you like (1/5): ";
cin >> drink;
cout << endl;
switch(drink)
{
case 1 :
cout << "You chose Coke." << endl;
break;
case 2 :
cout << "You chose Sprite." << endl;
break;
case 3 :
cout << "You chose Water." << endl;
break;
case 4 :
cout << "You chose Orange Juice." << endl;
break;
case 5 :
cout << "You chose Apple Juice." << endl;
break;
default :
cout << "Error. Choice was not valid, Here is your money back.";
}
}
else if(ifswitch == "if" || ifswitch == "If")
{
cout << "Now using if statements..." << endl << endl;
cout << "Your choices of drinks are: " << endl;
cout << "1 - Coke" << endl;
cout << "2 - Sprite" << endl;
cout << "3 - Water" << endl;
cout << "4 - Orange Juice" << endl;
cout << "5 - Apple Juice" << endl;
cout << "What drink would you like (1/5): ";
cin >> drink;
cout << endl;
if(drink == 1)
cout << "You chose Coke." << endl;
else if(drink == 2)
cout << "You chose Sprite." << endl;
else if(drink == 3)
cout << "You chose Water." << endl;
else if(drink == 4)
cout << "You chose Orange Juice." << endl;
else if(drink == 5)
cout << "You chose Apple juice." << endl;
else
cout << "Error. Choice was not valid, Here is your money back.";
}
cout << "Would you like to go again? (Y/N)" << endl;
cin >> again;
cout << endl;
}
cout << "Ending...";
}发布于 2016-10-07 14:07:38
这里有一种方法可以避免编写两个版本的指令,既不使用switch也不使用级联if:
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
static const std::vector<std::string> drinks = {
"Coke",
"Sprite",
"Water",
"Orange Juice",
"Apple Juice"
};
void print_drinks(auto& o, std::vector<auto> drinks)
{
int i = 0;
for (auto drink: drinks)
o << ++i << " - " << drink << "\n";
o.flush();
}
int main()
{
print_drinks(std::cout, drinks);
std::cout << "What drink would you like? (1-" << drinks.size() << ")" << std::endl;
size_t n;
if (!(std::cin >> n) || !n || n > drinks.size()) {
std::cerr << "Error. Choice was not valid, Here is your money back." << std::endl;
return EXIT_FAILURE;
}
// We now have a valid selection
const std::string drink = drinks[n-1];
std::cout << "You chose " << drink << "." << std::endl;
// Implicit EXIT_SUCCESS
}我不建议你把这个作为你的家庭作业,但是你可以从学习中学到一些东西。
Apple Juice和Apple juice)。它还可以添加或删除饮料,而无需更改代码的任何“逻辑”部分。https://codereview.stackexchange.com/questions/143528
复制相似问题