C++中的While循环
While Loop In C++
伙计们,我需要有关以下代码的帮助,我想添加一个 While 循环,用户在其中不断获取 Else 语句的 Cout。直到用户满足 If 或 Else-If 条件。换句话说,程序应该只在用户写入 'g''G''b''B' 时结束,否则它应该继续显示 Else 的 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";
}
这是答案,您应该提示输入,然后使用 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;
}
伙计们,我需要有关以下代码的帮助,我想添加一个 While 循环,用户在其中不断获取 Else 语句的 Cout。直到用户满足 If 或 Else-If 条件。换句话说,程序应该只在用户写入 'g''G''b''B' 时结束,否则它应该继续显示 Else 的 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";
}
这是答案,您应该提示输入,然后使用 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;
}