好的,所以我正在编写一个控制台应用程序,它使用开关来显示产品和价格。但是,当用户输入产品编号时,我正在弄清楚如何让它跳入开关
这是代码:
using namespace std;
int main()
{
int productNum = 0;
int quantityArray[5];
for (int i = 0; i < 5; ++i)
quantityArray[i] = 0;
char answer = ('y' || 'n' || 'e' || 'Y' || 'N' || 'E');
double priceArray[5] = { 2.98, 4.50, 9.98, 4.99, 6.87 };
double total1 = (quantityArray[0] * priceArray[0]);
double total2 = (quantityArray[1] * priceArray[1]);
double total3 = (quantityArray[2] * priceArray[2]);
double total4 = (quantityArray[3] * priceArray[3]);
double total5 = (quantityArray[4] * priceArray[4]);
double checkout = total1 + total2 + total3 + total4 + total5;
cout << "Please select a product number 1, 2, 3, 4, or 5" << endl;
cin >> productNum;
do
{
switch (productNum)
{
case '1':
cout << "1 inch x 1 inch sticker is $" << priceArray[0] << endl;
cout << "How many of these stickers would you like to purchase?" << endl;
cin >> quantityArray[0];
cout << "You have selected " << quantityArray[0] << "at the price of $" << priceArray[0] << endl;
cout << "Is this correct?" << endl;
cout << "Y for yes, N for no, E to checkout." << endl;
cin >> answer;
case '2':
cout << "3 inch x 2 inch sticker is $" << priceArray[1] << endl;
cout << "How many of these stickers would you like to purchase?" << endl;
cin >> quantityArray[1];
cout << "You have selected " << quantityArray[1] << "at the price of $" << priceArray[1] << endl;
cout << "Is this correct?" << endl;
cout << "Y for yes, N for no, E to checkout." << endl;
cin >> answer;
case '3':
cout << "7 inch x 7 inch sticker is $" << priceArray[2] << endl;
cout << "How many of these stickers would you like to purchase?" << endl;
cin >> quantityArray[2];
cout << "You have selected " << quantityArray[2] << "at the price of $" << priceArray[2] << endl;
cout << "Is this correct?" << endl;
cout << "Y for yes, N for no, E to checkout." << endl;
cin >> answer;
case '4':
cout << "3 inch x 3 inch sticker is our current special at $" << priceArray[3] << endl;
cout << "How many of these stickers would you like to purchase?" << endl;
cin >> quantityArray[3];
cout << "You have selected " << quantityArray[3] << "at the price of $" << priceArray[3] << endl;
cout << "Is this correct?" << endl;
cout << "Y for yes, N for no, E to checkout." << endl;
cin >> answer;
case '5':
cout << "5 inch x 4 inch sticker is $" << priceArray[4] << endl;
cout << "How many of these stickers would you like to purchase?" << endl;
cin >> quantityArray[4];
cout << "You have selected " << quantityArray[4] << "at the price of $" << priceArray[4] << endl;
cout << "Is this correct?" << endl;
cout << "Y for yes, N for no, E to checkout." << endl;
cin >> answer;
if (answer = 'y' || 'Y')
{
break;
}
else if (answer = 'n' || 'N')
{
cout << "Please select a qantity." << endl;
}
else if (answer = 'e' || 'E')
{
cout << checkout << endl;
}
}
}while (answer != 'e' || 'E');
}
我的书使用头文件来展示如何初始化 swtich,但它使用了多种方法。我很确定我可以在不使用标题的情况下将其纳入一种方法。