我正在为一家商店制作一个程序,并且需要询问用户是否想要多次购买商品。我环顾四周但找不到任何地方或任何有助于我回到起点的代码。
这是我到目前为止所拥有的:
cout << "Welcome to Techi Toys!" << endl;
cout << "" << endl;
cout << "Please select what product you would like to purchase: \n\n\n 1) iPhone \n 2) iPad Air \n 3) Sony Laptop \n 4) Kindle Fire \n 5) Xbox One \n 6) PS4 \n 7) Macbook Air \n 8) iPod Touch \n 9) DSLR Camera \n 10) Polaroid Camera \n" << endl;
cin >> choice;
switch(choice)
{
case 1:
cout << "\nYou have chosen iPhone 5S\n" << endl;
price = 549;
break;
case 2:
cout << "\nYou have chosen iPad Air\n" << endl;
price = 399;
break;
case 3:
cout << "\nYou have chosen Sony Laptop\n" << endl;
price = 798;
break;
case 4:
cout << "\nYou have chosen Kindle Fire\n" << endl;
price = 120;
break;
case 5:
cout << "\nYou have chosen Xbox One\n" << endl;
price = 430;
break;
case 6:
cout << "\nYou have chosen PS4\n" << endl;
price = 350;
break;
case 7:
cout << "\nYou have chosen Macbook Air\n" << endl;
price = 849;
break;
case 8:
cout << "\nYou have chosen iPod Touch\n" << endl;
price = 219;
break;
case 9:
cout << "\nYou have chosen DSLR Camera\n" << endl;
price = 519;
break;
case 10:
cout << "\nYou have chosen Polaroid Camera\n" << endl;
price = 100;
break;
}
//purchasing
cout << "How many would you like to purchase?\n" << endl;
cin >> quantity;
cout << "\nThe total is " << pound << ""<< price*quantity << endl;
非常感谢任何帮助
答案 0 :(得分:4)
如果您想询问用户是否想要做出更多选择,可以在交换机周围放置一个循环,变量wantMore = true。循环将是
while (wantsMore)
{
switch...
// ask if user wants more, if no set wantsMore = false
}
答案 1 :(得分:1)
某事
do
{
cout << "Please select what product you would like to purchase: \n\n\n 1) iPhone \n 2) iPad Air \n 3) Sony Laptop \n 4) Kindle Fire \n 5) Xbox One \n 6) PS4 \n 7) Macbook Air \n 8) iPod Touch \n 9) DSLR Camera \n 10) Polaroid Camera \n" << endl;
cin >> choice;
switch(choice)
{
case 1:
cout << "\nYou have chosen iPhone 5S\n" << endl;
price = 549;
break;
case 2:
cout << "\nYou have chosen iPad Air\n" << endl;
price = 399;
break;
case 3:
cout << "\nYou have chosen Sony Laptop\n" << endl;
price = 798;
break;
case 4:
cout << "\nYou have chosen Kindle Fire\n" << endl;
price = 120;
break;
case 5:
cout << "\nYou have chosen Xbox One\n" << endl;
price = 430;
break;
case 6:
cout << "\nYou have chosen PS4\n" << endl;
price = 350;
break;
case 7:
cout << "\nYou have chosen Macbook Air\n" << endl;
price = 849;
break;
case 8:
cout << "\nYou have chosen iPod Touch\n" << endl;
price = 219;
break;
case 9:
cout << "\nYou have chosen DSLR Camera\n" << endl;
price = 519;
break;
case 10:
cout << "\nYou have chosen Polaroid Camera\n" << endl;
price = 100;
break;
}
//purchasing
cout << "How many would you like to purchase?\n" << endl;
cin >> quantity;
cout << "\nThe total is " << pound << ""<< price*quantity << endl;
std::cout << "One more (y/n)? ";
} while ( std::cin >> yes_no && yes_no == 'y' );
答案 2 :(得分:1)
实际上你需要一些方法让用户指定他现在想要结束他的购买......
所以把你的switch语句放到这样的循环中:
bool fTerminate = false;
do {
// ... other stuff
switch(choice) {
case 0:
fTerminate = true;
break;
// ...
} // end of switch statement
// purchasing / selecting the number of items ...
// ...
} while( (!fTerminate) );
// ...