0
#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.

Levi
  • 19
  • 6
  • Whats `A`? Please show the declaration of `A`; – doug Sep 17 '22 at 18:19
  • class List{ private: int A[10]; int i; public: List(){ i = 0; } void insert(){ for(int j = 0 ; j < 10 ; j++){ cout << "\nElement you want to insert: (" << i+1 << "). "; if(cin >> A[j]){ i++; } else{ cout << "\nWrong Input" << endl; break; } } if(i > 9){ cout << "\nYour List Capacity is Full.\n" << endl; } } – Levi Sep 17 '22 at 18:21
  • When input from `cin` fails, you need to clear the stream (like this `cin.clear();`) before you can do any more input. That said, it not clear what this code is supposed to be doing, so maybe there are other problems as well. – john Sep 17 '22 at 18:21
  • A is my array list – Levi Sep 17 '22 at 18:21
  • @Levi If you have more code to share, edit the question. Code is impossible to read in the comments. – john Sep 17 '22 at 18:21
  • The Program is taking array input from user in int values but if he puts a character it just goes into infinite loop thats the issue – Levi Sep 17 '22 at 18:23
  • 1
    Now's a good time to learn how to use a debugger. It will save you tons of time. – doug Sep 17 '22 at 18:23
  • 1
    @Levi Probably that is caused by you failing to clear the input stream as I explained in my comment. – john Sep 17 '22 at 18:23
  • im trying with ' cin.clear(); ' too but it doesn't work – Levi Sep 17 '22 at 18:30
  • Does this answer your question? [C++, getting a infinite loop](https://stackoverflow.com/questions/20002137/c-getting-a-infinite-loop) – JaMiT Sep 17 '22 at 18:35
  • You might be missing some code to make the issue reproducible. The only loop looks like it should have at most 10 iterations regardless of the input. Does the code that call this call it repeatedly until `i` is at least some number? Even if I guessed wrong, you should have a (stub) `main` function in your question, so that others can copy the code, then compile and run it to reproduce your result. – JaMiT Sep 17 '22 at 18:40
  • i added my whole code now so you can execute it. – Levi Sep 17 '22 at 18:50
  • @Levi "whole code" is usually too much. Still that whole code does bring up a thought... Have you tested "when the user inputs a char or string" for the option selection (in your `main` function)? If that also puts you in an infinite loop, then you can drop your `List` class from the example, and drop your supported main menu choices down to just "6. Exit". – JaMiT Sep 17 '22 at 19:08
  • @JaMiT i dont really get what you're trying to say i have tried cin boolean check and cin clear and everything nothing really works and just crashes so idk what to do in my main function it doesnt really contain anything. – Levi Sep 17 '22 at 19:22
  • @Levi It's hard to help when you keep changing the code. Please update the code to the code you want to ask about (if necessary), say what you are entering as input, what happens when you do that, and what you want to happen instead. – john Sep 17 '22 at 19:32
  • @Levi You said *I've tried cin boolean check and cin.clear*. Do you realise that you need **both** of those things? The boolean check tells you that the input has failed, and the cin.clear recovers from that failure. You need to do both. – john Sep 17 '22 at 19:33
  • @john sorry about that im new to cpp jumped from java so its a little new to me im trying to get my grip on it. and thank you for your help. – Levi Sep 17 '22 at 19:48

1 Answers1

0

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

john
  • 85,011
  • 4
  • 57
  • 81
  • What do you mean by the numeric limit and streamsize in the ignore function can you explain that please. – Levi Sep 17 '22 at 19:49
  • 1
    @Levi Suppose the user types *"abc"*, the program tries to read that as an integer and that obviously fails. But the important point is that the input **remains unread** and if you don't do anything the next time you try to read an integer the *"abc"* will still be there waiting to be read. That's why your program goes into an infinite loop. So you need to discard the erroneous input. That's what that line of code does, it discards everything up to the next newline character. So you will start reading again on a fresh line. – john Sep 17 '22 at 19:53