-1

When the user enters choice a afterward the program will ask to enter your input to add the vector but If the user enters a string instead of a double or int the compiler gives error and it runs forever. What is the problem and How can I fix it. If the user didn't entered a valid number I want compiler show "Pleas just enter a valid number" and ask the user again. How can I find the input type that user enters and control it?

#include <iostream>
#include <vector>
#include <iomanip>
#include <bits/stdc++.h>
using namespace std;
int main()
{
    char answer;
    vector<double> vec{1, 2, 3, 7}; // 1, 2, 3, 7
            //     cout << "Enter an number to add to the list: ";
            //     double add_num;
            // cin >> add_num;
            // // if (add_num > 0 || add_num < 0)
            // // {
            // vec.push_back(add_num);
            // cout << add_num << " is added.\n";
            
            // cout << "The added thing is: " << vec.at(4);
    do
    {
        cout << "\nP - Print numbers\n";
        cout << "A - Add a number\n";
        cout << "M - Display mean of the numbers\n";
        cout << "S - Display the smallest number\n";
        cout << "L - Display the largest number\n";
        cout << "Q - Quit\n\n";
        cout << "Enter your choice: ";
        cin >> answer;
        switch (answer)
        {
        case 'P':
        case 'p':
            if (vec.size() == 0)
            {
                cout << "[] - the list is empty\n";
            }
            else
            {
                cout << "[ ";
                for (auto vector : vec)
                {
                    cout << vector << ' ';
                }
                cout << "]";
            }
            break;
        case 'A':
        case 'a':
        {
            double add_num{0.0};
            cout << "Enter an number to add to the list: ";
            cin >> add_num;
            if (add_num > 0 || add_num < 0)
            {
            vec.push_back(add_num);
            cout << add_num << " is added.";
            }
            else
            break;
        }
            break;
        case 'M':
        case 'm':
        {
            cout << fixed << setprecision(1);
            double mean{0}, sum{0};
            for (auto vector : vec)
            {
                sum += vector;
            }
            mean = sum / vec.size();
            cout << "The mean is: " << mean;
        }
        break;
        case 'S':
        case 's':
        {
            double min{vec.at(0)};
            if (vec.size() < 1)
            {
                cout << "Unable to determine the smallest number - list is empty\n";
            }
            else
            {
                //     min = *min_element(vec.begin(), vec.end());
                for (auto vector : vec)
                {
                    if (vector < min)
                    {
                        min = vector;
                    }
                }
                cout << "The smallest number is: " << min;
            }
        }
        break;
        case 'L':
        case 'l':
        {
            double max{vec.at(0)};
            if (vec.size() < 1)
            {
                cout << "Unable to determine the smallest number - list is empty\n";
            }
            else
            {
                //    max = *max_element(vec.begin(), vec.end());
                for (auto vector : vec)
                {
                    if (vector > max)
                    {
                        max = vector;
                    }
                }
                cout << "The largest number is: " << max;
            }
        }
        break;
        case 'Q':
        case 'q':
            cout << "Goodbye";
            break;
        default:
            cout << "Unknown selection, please try again\n";
            break;
        }
        cout << "\n";
    } while (answer != 'Q' && answer != 'q');

    cout << endl;
    return 0;
}


Shayan
  • 1
  • 1
  • 1
    If you enter `123a` when a number is expeted, the runtime will read `123` and leave the `a` for later. If you enter only an `a`, the input will fail (no number found), but the `a` will still remain in the input buffer. [How do I flush the cin buffer?](https://stackoverflow.com/questions/257091/how-do-i-flush-the-cin-buffer) – BoP May 14 '22 at 13:42
  • Would you mind to show how it should be done here? – Shayan May 14 '22 at 18:39

0 Answers0