我很困惑如何在while循环正常工作时做到这一点。目标是让代码循环回“主菜单”,并允许您选择另一个项目并将其添加到您的“购物车”中,因为缺乏更好的单词。问题是我的代码现在只会给你所选的第一个项目的小计,然后退出而不返回到主菜单,我希望能够循环进入菜单,直到退出被选中,然后计算总数。任何帮助表示赞赏!Do-while循环的问题
int main()
{
int dice; //Amount of dice
int beads; //Amount of beads
int bobble; //Amount of bobble heads
int selection; //Selection
string code; //User input coupon code
double diceTotal; //Subtotal for dice
double beadsTotal; //Subtotal for beads
double bobbleTotal; //Subtotal for bobble heads
double total; //Total cost of purchase
const double DICE = 6.25; //Dice cost
const double BEADS = 2.25; //Bead cost
const double BEADS_COUPON = 1.50; //Bead cost w/coupon
const double BOBBLE1 = 16.99; //1-5 Bob. Head cost
const double BOBBLE2 = 14.99; //6-10 Bob. Head cost
const double BOBBLE3 = 12.99; //11+ Bob. Head cost
const string COUPON = "beads1"; //Coupon code
cout << fixed << setprecision(2);
//Welcome
do
{
cout << setw(50) << "Welcome to DecoCar!" << endl;
cout << setw(49) << "Nick Wester, Owner" << endl;
cout << "Our inventory: " << endl;
cout << "1. Fuzzy Dice" << endl;
cout << "2. Mardi Gras Beads" << endl;
cout << "3. Bobble Heads" << endl;
cout << "4. Exit" << endl << endl;
cout << "Please make a selection: ";
cin >> selection;
}
while (selection <= 0 || selection >= 5);
{
if (selection == 1)
{
cout << "How many Fuzzy Dice would you like to buy? ";
cin >> dice;
while (dice < 0)
{
cout << "How many Fuzzy Dice would you like to buy? ";
cin >> dice;
}
diceTotal = dice*DICE;
cout << "Your subtotal for the Fuzzy Dice: $" << diceTotal << endl;
}
else if (selection == 2)
{
cout << "How many sets of Mardi Gras beasd would you like to buy? ";
cin >> beads;
cout << "Please type in your coupon code or NONE: ";
cin >> code;
if (code == "beads1")
{
cout << "Valid code entered" << endl;
beadsTotal = beads*BEADS_COUPON;
cout << "Your subtotal for Mardi Gras beads: $" << beadsTotal << endl;
}
else
{
beadsTotal = beads*BEADS;
cout << "Your subtotal for Mardi Gras beads: $" << beadsTotal << endl;
}
}
else if (selection == 3)
{
cout << "How many Bobble Heads would you like to buy? ";
cin >> bobble;
if (bobble >= 1 && bobble <= 5)
{
bobbleTotal = bobble*BOBBLE1;
cout << "Your subtotal for Bobble Heads: $" << bobbleTotal << endl;
}
else if (bobble >= 6 && bobble <= 10)
{
bobbleTotal = bobble*BOBBLE2;
cout << "Your subtotal for Bobble Heads: $" << bobbleTotal << endl;
}
else if (bobble >= 11)
{
bobbleTotal = bobble*BOBBLE3;
cout << "Your subtotal for Bobble Heads: $" << bobbleTotal << endl;
}
}
}
}
你的循环将永远为值1-4的出口,这是你的工作代码或你还没有运行它? –
@RishabhKumar一切都“有效”,它只是不会循环回菜单,一旦我选择一个项目,我想要的数量就会给我一个小计然后退出。 – Nick5227