Home > database >  How to stop user from entering a duplicate integer in the vector in (C )?
How to stop user from entering a duplicate integer in the vector in (C )?

Time:06-09

    // Using switch statement

    // Vectors & Variables required to store selection and list of items
    vector <int> list_items{};
    char selection{};
    int list_adder{};
    

    do {

        // Displaying Menu Options First:
        cout << "\n" << endl;
        cout << "P - Print numbers" << endl;
        cout << "A - Add a number" << endl;
        cout << "M - Display mean of the numbers" << endl;
        cout << "S - Display the smallest number" << endl;
        cout << "L - Display the largest number" << endl;
        cout << "Q - Quit" << endl;

        // Storing their choice in the selection variable:


        cout << "\nEnter your choice: ";
        cin >> selection;


        switch (selection) {
        case 'a':
        case 'A':
                cout << "Enter an integer to add to the list: ";
                cin >> list_adder;
                list_items.push_back(list_adder);
                cout << list_adder << " added" << endl;
            break;

        default: 
            cout << "Unknown selection, please try again";
        }

    } while (selection != 'q' && selection != 'Q');

How do i add a if else or loop or something so if the user already entered for example 5 added , he cant add 5 again, it will say , 5 is already in the list. And they will get the prompt again that "Enter an integer to add to the list. Please help me cant figure this out!

CodePudding user response:

If you really want to use a vector to store the list items, you can use std::find to check if the item already exists in the vector. std::find returns an iterator to the found item or the end() iterator if it was not found.

if(std::cin >> list_adder) {
    if(std::find(list_items.begin(), list_items.end(), list_adder) == list_items.end())
        list_items.push_back(list_adder);
    else
        std::cout << "already in the list\n"; 
}

Another option is to use a std::set to store the items instead. A set can only contain unique values so you don't need to check if the value is already in it before inserting:

std::set<int> list_items;
if(std::cin >> list_adder) list_items.insert(list_adder);

... but you could check if it's in it if you'd like to inform the user:

if(std::cin >> list_adder) {
    if(list_items.contains(list_adder)) std::cout << "already in the list\n";
    else list_items.insert(list_adder);
}

About begin() and end() that was asked in the comments:
Most standard container classes have these member functions. They return iterators. An iterator is an object (like a pointer) that points to an element inside the container. begin() returns an iterator to the first element and end() returns an iterator one step after the last element. To get the actual value from en iterator, you need to dereference it:

auto it = list_items.begin();
std::cout << *it;             // dereferencing the iterator

Note that since end() is returning an iterator one step after the last element you are not allowed to dereference it.

A loop using iterators to print all values in your vector could look like this:

for(auto it = list_items.begin(); it != list_items.end();   it) {
    std::cout << *it << '\n';
}

but nowadays, seeing such loops is rare since you can use range based for-loops instead:

for(auto& value : list_items) {
    std::cout << value << '\n';
}

CodePudding user response:

You can use a do-while loop to iterate the message "5 is already in the list" everything a duplicate integer is given as input. If you can send the whole code, maybe I can do it for you.

  •  Tags:  
  • c
  • Related