Indiana Jones
Indiana Jones

Reputation: 85

Why the input is not cleared by using cin.clear()?

I am adding the values to the program and printing it back to check which values are stored but before printing the values I want the cin to be cleared and print the list is empty. The problem is it is still printing the saved list and not reflecting with cin.clear() How to achieve it? I think I am not doing it on the correct way if I am not wrong.

The Code:

#include <iostream>
#include <vector>
#include <limits>
using namespace std;

int main()
{
    vector <int> number{};
    char selection{};

    do{
        cout << "\nMain Menu" << '\n';
        cout << "P - Print numbers" << '\n';
        cout << "A - Add numbers" << '\n';
        cout << "M - Show the medium number" << '\n';
        cout << "S - Show small number" << '\n';
        cout << "L - Show large number" << '\n';
        cout << "Q - Quite the program" << '\n';
        cout << "------------------------" << '\n';
        cout << "Enter your Choice: ";
        cin >> selection;

        if(selection == 'p' || selection == 'P')
        {
                cin.clear();
            {
                if (number.size() == 0)
                    cout << "[] - The list is Empty" << '\n';
                else{
                    cout << " [ ";
                    for(int num:number)
                        cout << num << " ";
                    cout << " ] " << '\n';
                }
            }
        }else if(selection == 'a' || selection == 'A')
            {
                int num_to_add{};
                cout << "Enter the number to add: ";
                cin >> num_to_add;
                number.push_back(num_to_add);
                cout << num_to_add << " Successfully Added" << '\n';
                cin.clear();
            }else if(selection == 'm' || selection == 'M')
                {
                    if (number.size() == 0)
                    cout << "[] - The list is Empty, unable to calculate" << '\n';
                    else{
                        int total{};
                        for(int num:number)
                            total += num;
                        cout << "The middle number is: " <<static_cast<float>(total)/number.size() << '\n';
                    }
                }else if(selection == 's' || selection == 'S')
                    {
                        if (number.size() == 0)
                        cout << "[] - The list is Empty, unable to retrieve smaller value" << '\n';
                        else{
                            int smallest = number.at(0);
                            for(int num:number)
                                if(num < smallest)
                                smallest = num;
                            cout << "The smallest number is: " << smallest << '\n';
                        }
                    }else if(selection == 'l' || selection == 'L')
                    {
                        if (number.size() == 0)
                        cout << "[] - The list is Empty, unable to retrieve larger value" << '\n';
                        else{
                            int larger = number.at(0);
                            for(int num:number)
                                if(num > larger)
                                larger = num;
                            cout << "The larger number is: " << larger << '\n';
                        }
                    }
    }while (selection != 'q' && selection != 'Q');
    return 0;
}

Upvotes: 0

Views: 71

Answers (1)

Саша
Саша

Reputation: 847

Your data stored in vector < int > number{}; you could clear the number by simply saying number.clear() when anytime you need to empty the stored data.

More explanation: Just run my code, add numbers, press c to clear and then check your list.

#include <iostream>
#include <limits>
#include <vector>

using namespace std;

void mainMenu();
void dataValidation();
void CalcuLation();
void outPuts();


int main()
{
    dataValidation();
    return 0;
}

void dataValidation()
{
    int bad_index = 0;
    string Uinput;
    cout << "Enter the number only to check if You are not a bot: ";
    cin >> Uinput;
    bool isvalid_ = true;
    if(Uinput[0] == '0')
    {
        isvalid_ = false;
        cout << "Oops! Entering Zero is not allowed. Try again with correct number." << '\n';
        return ;
    }
    for (int countingPoint = 0; countingPoint < Uinput.length() && isvalid_; countingPoint++)
    {
        if (!(Uinput [countingPoint] >= 48 && Uinput[countingPoint] <= 57))
        {
            isvalid_ = false;
            cout << "Not Success" << '\n';
            bad_index = countingPoint;
            break;
        }
    }
    if(isvalid_)
    {
    int userChoice_;
    char restart_;
    do{
    system("cls");
    cout << "Thank you for Entering the correct number to solve bot captcha..." << '\n';
    cout << "-----------------------------------------------------------------" << '\n';
    mainMenu();
    cout<< " Enter the Choice: ";
    while (!(cin>>userChoice_))
    {
        system("cls");
        cin.clear();
        cin.ignore(numeric_limits<streamsize>:: max(), '\n');
        system("cls");
        cout<< " Wrong choice detected! Illegal stance stopped! Try again." << '\n';
        cout<< " *********************************************************" << '\n';
        mainMenu();
        cout<< " Enter the Choice: ";
    }
    do{
        cin.clear();
        cout << " Do you want to Restart the App? [Y/n] " ;
        cin >> restart_;
        system("cls");
        mainMenu();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');

        }while (restart_!='Y'&&restart_!='y'&&restart_!='N'&&restart_!='n');
    }while (restart_ == 'Y' || restart_ == 'y');
    return;
}
    else
        cout << "Value Entered: ["<< Uinput[bad_index] << "] is illegal which is not a number. Try again with a correct number." << '\n';
}

void mainMenu()
{
    cout << "Main Menu" << '\n';
    cout << "A for Adding Numbers" << '\n';
    cout << "P for Printing Saved numbers" << '\n';
    cout << "S for Printing Saved numbers" << '\n';
    cout << "L for Printing Saved numbers" << '\n';
    cout << "C to to clear the saved list" << '\n';
    cout << "Q to Quite the Program" << '\n';
    cout << "Enter your Choice: ";
    CalcuLation();
}

void CalcuLation()
{
    vector <int> number{};
    vector <int> Emptynumber{0};
    char selection{};

    do{
        cin >> selection;
        if(selection == 'p' || selection == 'P')
            {
                if (number.size() == 0)
                    cout << "[] - The list is Empty" << '\n';
                else{
                    cout << " [ ";
                    for(int num:number)
                        cout << num << " ";
                    cout << " ] " << '\n';
                }
        }else if(selection == 'a' || selection == 'A')
            {
                int num_to_add{};
                cout << "Enter the number to add: ";
                cin >> num_to_add;
                number.push_back(num_to_add);
                cout << num_to_add << " Successfully Added" << '\n';
            }else if(selection == 'm' || selection == 'M')
                {
                    if (number.size() == 0)
                    cout << "[] - The list is Empty, unable to calculate" << '\n';
                    else{
                        double total{};
                        for(double num:number)
                            total += num;
                        cout << "The middle number is: " <<static_cast<int>(total)/number.size() << '\n';
                    }
                }else if(selection == 's' || selection == 'S')
                    {
                        if (number.size() == 0)
                        cout << "[] - The list is Empty, unable to retrieve smaller value" << '\n';
                        else{
                            int smallest = number.at(0);
                            for(int num:number)
                                if(num < smallest)
                                smallest = num;
                            cout << "The smallest number is: " << smallest << '\n';
                        }
                    }else if(selection == 'l' || selection == 'L')
                    {
                        if (number.size() == 0)
                        cout << "[] - The list is Empty, unable to retrieve larger value" << '\n';
                        else{
                            int larger = number.at(0);
                            for(int num:number)
                                if(num > larger)
                                larger = num;
                            cout << "The larger number is: " << larger << '\n';
                        }
                    }else if(selection == 'C' || selection == 'c')
                    {
                        if (number.size() == 0)
                        cout << "[] - The list is Already Empty, Can't be Cleared" << '\n';
                        else{
                            number.push_back(0);
                            number.clear();
                            cout << " Successfully Cleared" << '\n';
                            cout << " The list is Empty " << '\n';
                        }
                    }
    }while (selection != 'q' && selection != 'Q');
    return;
}

Upvotes: 3

Related Questions