[Solved] While Loop In C++


Here’s the answer, you should prompt for input, then check the input using a while loop instead of IF-ELSE to make sure the user provided the answer you desired. This is actually a simple logic question tho. You may explore do-while loop too.

#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;
}

solved While Loop In C++