我很困惑如何在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