伙計們,我需要有關以下代碼的幫助,我想添加一個 While 回圈,用戶可以在其中不斷獲取 Else 陳述句的 Cout。直到用戶滿足 If 或 Else-If 條件。換句話說,程式應該只在用戶寫入 'g''G''b''B' 時結束,否則它應該繼續顯示其他人的 Cout,并再次要求輸入正確的值。
#include <iostream>
using namespace std;
int main()
{
string item;
float price;
int quantity;
float total;
char experience;
cout << "Write The Name Of Item You Want To Buy:" << endl;
getline(cin, item);
cout << "What Is The Price Of " << item << " In Dollars $ ?" << endl;
cin >> price;
cout << "What Is The Quantity Of " << item << endl;
cin >> quantity;
total = price*quantity;
cout << "Your Total Bill For " << quantity << " " << item << " Is " << total << "$" << endl<< endl;
cout << "How Was Your Shopping Experience Write (G) If Good And (B) For Bad" << endl;
cin >> experience;
if (experience == 'g' || experience == 'G')
{
cout << "We Appreciate Your Feedback THANKS For Shopping :)";
}
else if (experience == 'b' || experience == 'B')
{
cout << "Sorry For Bad Experience, We Will Do Better Next Time THANKS For Shopping :)";
}
else
{
cout << "Write \"G\" If Good And \"B\" If Bad";
}
uj5u.com熱心網友回復:
這是答案,您應該提示輸入,然后使用 while 回圈而不是 IF-ELSE 檢查輸入,以確保用戶提供了您想要的答案。這實際上是一個簡單的邏輯問題。您也可以探索 do-while 回圈。
#include <iostream>
using namespace std;
int main()
{
string item;
float price;
int quantity;
float total;
char experience;
cout << "Write The Name Of Item You Want To Buy:" << endl;
getline(cin, item);
cout << "What Is The Price Of " << item << " In Dollars $ ?" << endl;
cin >> price;
cout << "What Is The Quantity Of " << item << endl;
cin >> quantity;
total = price*quantity;
cout << "Your Total Bill For " << quantity << " " << item << " Is " << total << "$" << endl<< endl;
cout << "How Was Your Shopping Experience Write (G) If Good And (B) For Bad" << endl;
cin >> experience;
while (experience != 'G' && experience != 'g' && experience != 'B' && experience != 'b') {
cout << "Write \"G\" If Good And \"B\" If Bad\n";
cin >> experience;
}
if (experience == 'g' || experience == 'G') {
cout << "We Appreciate Your Feedback THANKS For Shopping :)";
} else if (experience == 'b' || experience == 'B') {
cout << "Sorry For Bad Experience, We Will Do Better Next Time THANKS For Shopping :)";
}
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/388294.html
