HELP!!

Hey, I have to create a menu using functions. I already completed the menu, but using if statements. Can you show me how to fix my program in order to use these functions -
1. Write function fillVector that takes as argument a vector. Ask the user to enter +ve int values
and to enter a -ve value to stop. The function would add the entered int values to the vector.
2. Write function printVector that takes as argument a vector, and prints its elements.
3. Write function reverseVector that takes as argument a vector and reverses the vector.
You may not use the vector member function reverse().
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.
7. Write function menu that prints the following menu:
1. fillVector
2. clearVector
3. printVector
4. reverseVector
5. removeValue
6. quit

My function 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
#include <iostream>
#include <vector>
using namespace std;
void menu();
void fillVector(vector <int>&fill);
void printVector(vector <int>&print);
void reverseVector(vector <int>&reverse);
void removeIndex(vector <int>&removeI);
void removeValue(vector <int>&removeV);

int main(){
    vector <int> fillVector;
    int input=0;
    int theCurrInt=0;
    int index =0;
do{
    menu();
    cin>> input;
    if(input==1){
        cout <<"Enter +ve values separated by spaces and a -ve value to indicate no more " << endl;
        while ( cin>> theCurrInt && theCurrInt>0 )
        {
            fillVector.push_back(theCurrInt) ;
        }
        }
        if(input==2){
            fillVector.clear();
        }
        
        if(input==3){
            for (int i=0; i<fillVector.size(); i++)
                cout<<fillVector.at(i)<<" ";
            cout << endl;
            
            
        }
        if(input==4){
            for (int i=fillVector.size(); i>0; i--)
                cout<<fillVector[i-1]<<" ";
            cout << endl;
            
        }
        if(input==5){
            int value;
            int count=0;
            cout << "Enter value to remove" << endl;
            cin >> value;
            for (int i=0; i<fillVector.size(); i++){
                
                if(fillVector.at(i)==value)
                {
                    for(int j=i; j<fillVector.size()-1; j++)
                    {
                        fillVector[j]=fillVector[j+1];
                    }
                    count ++;
                    break;
                }
                index = i;
            }
            if(count==0)
            {
                cout<<value <<" not found..!!";
            }
            else{
                cout << value << " found at index " << index+1 << " removed" << endl;
            }
        }
        }while(input!=6); cout <<"Good by!" << endl;
    return 0;}

void menu(){
    cout<<"1. fillVector \n2. clearVector \n3. printVector \n4. reverseVector \n5. removeVector \n6. quit " << endl;
}
The main() function should be fairly simple. It needs to control which of the other functions are called. All the small details belong in those other functions.

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
#include <iostream>
#include <vector>

using namespace std;

void menu();
int  get_choice();
void fillVector(vector <int>&fill);
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 == 3) printVector(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;    
}
Last edited on
Topic archived. No new replies allowed.