function menu

I got my menu function to work, but I didn't utilize find, removeIndex, and removeValue functions. Im talking about these-
4. Write function find that takes as argument a vector and an int value. It returns the index of
the element or -1 if it is not found.
5. Write function removeIndex that takes as argument a vector and an index value. It removes
the element at that index.
6. Write function removeValue that takes as argument a vector and an int value to remove. It removes that element if present. This function calls functions find and removeIndex.

My current code is

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
 #include <iostream>
#include <vector>

using namespace std;

void menu();
int  get_choice();
void fillVector(vector <int>&fill);
void clearVector(vector <int>& clear);
void printVector(vector <int>&print);
void reverseVector(vector <int>&reverse);
void removeIndex(vector <int>&removeI);
void removeValue(vector <int>&removeV);

int main()
{
    vector <int> vec;
    int input = 0;
    
    do {
        menu();
        input = get_choice();
        if      (input == 1) fillVector(vec);
        else if (input == 2) clearVector(vec);
        else if (input == 3) printVector(vec);
        else if (input == 4) reverseVector(vec);
        else if (input == 5) removeValue(vec);
        
    } while (input != 6);
    
    cout <<"Good by!" << endl;
}

void menu()
{
    cout << " 1. fillVector \n 2. clearVector \n 3. printVector \n"
    " 4. reverseVector \n 5. removeVector \n 6. quit " << endl;
}

int get_choice()
{
    int choice = 0;
    
    while (choice < 1 || choice > 6)
    {
        cout << "Enter your choice: ";
        cin >> choice;
        if (!cin)
        {
            cin.clear();
            cin.ignore(1000, '\n');
        }

    }
    
    return choice;
}

void fillVector(vector <int>&fill)
{
    cout << "Enter +ve values separated by spaces and a -ve value to indicate no more " << endl;
    int theCurrInt;
    while ( cin >> theCurrInt && theCurrInt > 0 )
    {
        fill.push_back(theCurrInt) ;
    }
}

void printVector(vector <int>&print)
{
    for (size_t i=0; i<print.size(); i++)
        cout<<print.at(i)<<" ";
    cout << endl;
}

void reverseVector(vector <int>&reverse){
    for (int i=reverse.size(); i>0; i--)
        cout<<reverse[i-1]<<" ";
    cout << endl;
}
void removeValue(vector <int>&remove){
    int count=0;
    int value =0;
    int index =0;
    cout << "Enter value to remove" << endl;
    cin >> value;
    for (int i=0; i<remove.size(); i++){
        
        if(remove.at(i)==value)
        {
            for(int j=i; j<remove.size()-1; j++)
            {
                remove[j]=remove[j+1];
            }
            count ++;
            break;
        }
        index = i;
    }
    if(count==0)
    {
        cout<<value <<" not found..!!";
    }
    else{
        cout << value << " found at index " << index+1 << " removed" << endl;
    }
}
void clearVector(vector <int>&clear){
    clear.clear();
}
HELP!!!
but I didn't utilize find

Any reason why not?
Find is simpler to implement than some of the code you have already written.
It's not that i choose not to implement it, i'm having difficulty to make it work.
Can anyone help me implement the find and removeindex in my removeValue function.
Well, the first stage would be to write the find() function.
Write function find that takes as argument a vector and an int value. It returns the index of the element or -1 if it is not found.

The specification there tells you what the function header should look like, it returns an int, is named find and takes two parameters:
 
int find(const vector <int>&vec, int value);

Then the code - well - you can probably copy+paste some of your existing code, just a for-loop to iterate through the elements, if it is found, return that index. Otherwise, at the end of the loop, return -1.

Then make sure you are happy with that - test it on its own, without trying to make it part of the implementation of any other function to begin with. Only after coding and testing find(), consider calling it within some of your other functions - it should make those functions much simpler, because some of the complexity is handled elsewhere.
Last edited on
Topic archived. No new replies allowed.