I'm trying to check my array list input if its an int or string but this seems to crash and just go into infinite loop

Viewed 45
#include <iostream>
#include <stdio.h>
#include <ctype.h>
using namespace std;

class List{
    private:
        int A[10];
        int i;
    public:
        List(){
            i = 0;
        }
        void insert(){
            int v;
            for(int j = 0 ; j < 10 ; j++){
                cout << "\nElement you want to insert: (" << i+1 << "). ";
                cin >> v;
                if(i <= 9){
                    A[i] = v;
                    i++;
                }
                else{
                    cout << "\nWrong Input" << endl;
                    break;
                }
            }
            if(i > 9){
                    cout << "\nYour List Capacity is Full.\n" << endl;
            }
        }
        void display(){
            cout << "\n{ ";
            for(int j = 0 ; j < i ; j++)
                cout << A[j] << "  ";
            
            cout << "}\n" << endl;
        }
        void remove(){
            int p;
            cout << "\nElement you want to remove (0 - 9): ";
            cin >> p;
            if (i == 0){
                cout << "\nList is empty!\n" << endl;
                return;
            }
            if (p >= 0 && p < i){
                for(int j = p ; j < i-1 ; j++)
                    A[j] = A[j + 1];
                    i--;
        }
    }
        void size(){
            cout << "\nYour list size is: " << i << endl;
        }
        void checkcapacity(){
            int arrSize = sizeof(A)/sizeof(A[0]);
            cout << "\nThe Capacity of the array is: " << arrSize << endl;
        }
};

int main(){
    List l;
    int a;
    cout << "\t\t\t\t\t\tWelcome to List Program!";
    while(a != 4){
        int choose;
        cout << "\nThe Program have following options:\n1. Insert\n2. Display\n3. Remove\n4. Check Size\n5. Check Capacity\n6. Exit\n\nNote: Your list capacity is 10!";
        cout << "\n\nChoose (1 - 5): ";
        cin >> choose;
            if (choose == 1){
                l.insert();
        }
            else if (choose == 2){
                l.display();
        }
            else if (choose == 3){
                l.remove();
        }
            else if (choose == 4){
                l.size();
        }
            else if (choose == 5){
                l.checkcapacity();
        }
            else if (choose == 6){
                a = 4;
        }
    }
    cout << "Thank you for using this program!";
}

I'm using this class in my main function in which I call them but when the user inputs a char or string in the insert function it goes into infinte loop. Int i is my counter and it just contains the size of my array list im just trying to put a check of character that if user input a character is should show an error.

1 Answers

Here's one way to write your insert function

void insert()
{
    int v;
    for(int j = 0 ; j < 10 ; j++)
    {
        cout << "\nElement you want to insert: (" << i+1 << "). ";
        if (cin >> v)
        {
            if (i < 10)
            {
                A[i] = v;
                i++;
            }
        }
        else
        {
            cin.clear(); // clear error
            cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // discard any pending input
        }
    }
    if (i >= 10)
        cout << "\nYour List Capacity is Full.\n" << endl;
}

The important part is the recovery from bad input. First cin.clear() is called to clear the stream error state, secondly cin.ignore(...) is called to discard any pending input.

More details here

Related