-3

I want to implement vectors so that they can be used as a dynamic array. Also I want to implement the same so that it automatically checks the data type of the vector and carry out the operations. There are various runtime errors which I'm unable to resolve.

Here is my implementation:

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

void arrayInsert(vector<char>, char a);
char arraySearch(vector<char>, char a);
void arrayDelete(vector<char>, char a);
void arrayInsert(vector<int>, int a);
int arraySearch(vector<int>, int a);
void arrayDelete(vector<int>, int a);

int main()
{
    int ch = 0;
    char a;
    int a;
    vector<int> arr;
    vector<char> arr;
    do
    {   
        do
        {
            cout<<"Arrays Program!!!\n\n";
            cout<<"1. Insert in the array\n";
            cout<<"2. Search a element in the array\n";
            cout<<"3. Delete from the array\n";
            cout<<"4. Exit\n";
            cout<<"\nEnter your choice:";
            cin>>ch;
            if(ch < 1 || ch > 4)
                cout<<"Invalid Choice!! Try again";
        }while(ch < 1 || ch > 4);

        switch(ch)
        {
            case 1: cout<<"Enter the element in the array:\n";
                    cin>>a;
                    arrayInsert(arr,a);
                    break;
            case 2: cout<<"Enter the element to be searched:";
                    cin>>a;
                    int flag = arraySearch(arr,a);
                    if(flag == NULL || flag == -1)
                        cout<<"\nElement Not Found!";
                    else
                    {
                        cout<<"\nElement found at index "<<flag;
                    }
                    break;
            case 3: cout<<"\nEnter element to be deleted:";
                    cin>>a;
                    arrayDelete(arr,a);
                    break;
            case 4: cout<<"Exiting Program!!!";
                    exit(0);
        }
    }while(ch >= 1 || ch <= 4);

    return 0;
}

void arrayInsert(vector<char> arr, char a)
{
    arr.push_back(a);
}

void arrayInsert(vector<int> arr, int a)
{
    arr.push_back(a);
}

char arraySearch(vector<char> arr, char a)
{
    for(int i = 0; i<arr.size(); ++i)
    {
        if(arr[i] == a)
        {
            return i;
        }
        else
        {
            return NULL;
        }

    }
}

int arraySearch(vector<int> arr, int a)
{
    for(int i = 0; i<arr.size(); ++i)
    {
        if(arr[i] == a)
        {
            return i;
        }
        else
        {
            return -1;
        }

    }
}

void arrayDelete(vector<char> arr, char a)
{
    for(vector<char>::iterator i = arr.begin(); i<arr.end(); ++i)
    {
        if(*i == a)
        {
            arr.erase(i);
        }
        else
        {
            cout<<"\nElement does not exist in the array!";
        }

    }
}

void arrayDelete(vector<int> arr, int a)
{
    for(vector<int>::iterator i = arr.begin(); i<arr.end(); ++i)
    {
        if(*i == a)
        {
            arr.erase(i);
        }
        else
        {
            cout<<"\nElement does not exist in the array!";
        }



    }
}

These are the errors: Error Screen Screenshot

  • Post your code to reproduce the problem here, links aren't acceptaple. – πάντα ῥεῖ Aug 04 '19 at 16:43
  • Please copy the code into the question itself. Not everyone can access external sites, and the links may break over time – Lalit Mehra Aug 04 '19 at 16:46
  • @πάνταῥεῖ It's a long code and this is my first time on stack overflow. I actually am not used to the interface. Any help will be appreciated. – Rishabh Jain Aug 04 '19 at 16:48
  • @RishabhJain Narrow it to a [mcve] and then post here. – πάντα ῥεῖ Aug 04 '19 at 16:49
  • @πάνταῥεῖ Sir will this help? – Rishabh Jain Aug 04 '19 at 16:57
  • @RishabhJain Don't call me _sir_ please! And no you edit doesn't really help, you have to be more specific about the errors. – πάντα ῥεῖ Aug 04 '19 at 17:00
  • @πάνταῥεῖ Actually there are a lot of errors coming up. I have included a screenshot of the errors now. – Rishabh Jain Aug 04 '19 at 17:07
  • @RishabhJain Putting pictures about errors isn't useful either. Post error messages as verbatim texz please. – πάντα ῥεῖ Aug 04 '19 at 17:16
  • `arrayDelete` [invalidates](https://stackoverflow.com/questions/6438086/iterator-invalidation-rules) `i` in the middle of a loop using `i`. You may find the [Erase-Remove Idiom](https://en.wikipedia.org/wiki/Erase%E2%80%93remove_idiom) to be a better alternative. – user4581301 Aug 04 '19 at 17:28
  • Before you go further, you need to decide how you will tell an `int` from a `char` when taking input from the user. When you are done figuring that out, you'll probably find that you do not need both `int` and `char`. – user4581301 Aug 04 '19 at 17:31
  • Terminology note: You have Compiler errors, not Runtime errors. Compiler errors are bad syntax detected while compiling the program. No program is produced. Compiler warnings are the result of code that compiles, but the compiler suspects the logic is questionable. A program may be produced, but the program is probably wrong, leading to runtime errors if executed. Linker errors are typically absences or ambiguities found while the program is being linked. Runtime errors are when the program fails to execute correctly. – user4581301 Aug 04 '19 at 17:34
  • @user4581301 Thanks for the Terminology Note. And I think that I should store the element in a char and then see if it's a digit or not using isdigit() and then pass it accordingly. Please correct me if I am wrong. – Rishabh Jain Aug 04 '19 at 17:40

1 Answers1

1

You cannot re-declare a variable in the same scope:

char a;
int a;

just rename them. Live

Oblivion
  • 6,320
  • 2
  • 9
  • 30
  • I want to take an input which is either a char or int and can be either of them so how will I implement it and pass the correct variable to the functions if I rename them i.e. how will I know which variable to use at which place. – Rishabh Jain Aug 04 '19 at 17:20
  • 1
    @RishabhJain It's is extremely rare that you want a variable with multiple simultaneous types. I recommend checking your program requirements and design before continuing. – user4581301 Aug 04 '19 at 17:23
  • @RishabhJain as user4581301 suggested you might consider redesigning your application – Oblivion Aug 04 '19 at 17:34
  • 1
    @RishabhJain if you insist on this design you probably can read a string and check if it has digits inside or not then decide what to do: https://stackoverflow.com/q/2346599/10933809 and https://stackoverflow.com/q/9642292/10933809 may help – Oblivion Aug 04 '19 at 17:36