80 percent done.. Stuck on the last 20%


Menu:
1.Insert "Insert" key
2.Delete "Delete" key
3.Sort "F2" key
4.Select "Down Arrow" key
5.Move Right "Right Arrow" key
6.Move Left "Left Arrow" key
7.Exit "F1" key



This is my code so far.. I'm stuck on the insert key and the delete key. and the sort key
suppose to keep the brackets around [11].
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <conio.h>
using namespace std;
void readFile(ifstream& read, int& numUsed, int& activeNum, int a[]);
void displayList(int a[], int numUsed, int activeNum);
void menus();
void sortItem(int a[], int& numUsed, int& activeNum);
void shiftLeft(int a[], int& numUsed, int& activeNum);
void shiftRight(int a[], int& numUsed, int& activeNum);
void deleteFunction(int a[], int& numUsed, int& activeNum);
void insertFunction();
void exitFunction();
void selectFunction(int a[], int& numUsed, int& activeNum);
const int ARRAYS = 10;
int main()
{
    ifstream read;
    int a[ARRAYS];
    int numUsed, activeNum, value, integer;
    readFile(read, numUsed, activeNum, a);
    read.close();
    // getting_info();//gets all the info
    do
    {
        menus(); //displays the menu for the user.
        cout << "\n";//skips a few line to make the menu more neat.
        displayList(a, numUsed, activeNum);//display_stuff(a[], active, current_used,);//displays the array.
        value = getch();
        if(value == 224 || value == 0)
        {
            value = getch();
        }
        switch(value)
        {
            case 49:
            case 82:
                    cout << "\nPlease enter an integer:\n";
                    cin >> integer;
                    break;
            case 50:
            case 83:
                    if(numUsed >= 0)
                        deleteFunction(a, numUsed, activeNum);
                    break;
            case 51:
            case 60:
                    sortItem(a, numUsed, activeNum);
                    break;
            case 52:
            case 80:
                    selectFunction(a, numUsed, activeNum);
                    break;
            case 53:
            case 77:
                    shiftRight(a, numUsed, activeNum);
                    break;
            case 54:
            case 75:
                   shiftLeft(a, numUsed, activeNum);
                    break;
            case 55:
            case 59:
                    cout << "F1 key, Quit\n";
                    break;
            default:
                    cout << "\nPlease enter a correct selection.\n";
                    break;
        }
        system("CLS");
        //Insert 82; 49, Delete 83; , F2 60, down 80, right 77, left 75, F1 59
    }while(value != 55 && value != 59);
}
void readFile(ifstream& read, int& numUsed, int& activeNum, int a[])
{
    read.open("test.txt");
    if(read.fail())
    {
        cout << "The file has failed to open.\n";
        exit(1);
    }
    read >> numUsed;
    read >> activeNum;
    for(int i(0); i < numUsed; i++)
    {
        read >> a[i];
    }
}
void displayList(int a[], int numUsed, int activeNum)
{

    for(int i = 0; i < numUsed; i++)
    {
        if (i == activeNum)
            cout << "[" << a[i] << "]";
        else
            cout << a[i] << " ";
    }
}
void menus()
{
    cout << "Menus:\n"
         << "1.)Insert       ""Insert"" key\n"
         << "2.)Delete       ""Delete"" key\n"
         << "3.)Sort         ""F2"" key\n"
         << "4.)Select       ""Down Arrow"" key\n"
         << "5.)Move Right   ""Right Arrow"" key\n"
         << "6.)Move Left    ""Left Arrow""key\n"
         << "7.)Exit         ""F1 key"" key\n";
}
void sortItem(int a[], int& numUsed, int& activeNum)
{
    for(int i = numUsed -1; i > 0; i--)
        for(int j = 0; j < i; j++)
            if(a[j] > a [j+1])
            {
                int temp = a[j + 1];
                a[j + 1] = a[j];
                a[j] = temp;
            }
}
void shiftLeft(int a[], int& numUsed, int& activeNum)
{
    int temp = a[activeNum - 1];
    if(activeNum > a[0-1])
    {
        a[activeNum - 1] = a[activeNum];
        a[activeNum] = temp;
        activeNum--;
    }
}
void shiftRight(int a[], int& numUsed, int& activeNum)
{
    int temp = a[activeNum +1];
    if(activeNum < numUsed - 1)
    {
        a[activeNum + 1] = a[activeNum];
        a[activeNum] = temp;
        activeNum++;
    }
}
void selectFunction(int a[], int& numUsed, int& activeNum)
{
    int active;
    if(activeNum == numUsed -1)
    {
        activeNum = 0;
        active = a[0];
    }
    else
    {
        activeNum ++;
        active = a[activeNum];
    }
}
void deleteFunction(int a[], int& numUsed, int& activeNum)
{
    for(int i = 0; i < numUsed; i++)
    {
        if(i == activeNum)
        {
            for(int j = i; j < numUsed; j++)
            {
                a[j] = a[j + 1];
                    if (activeNum == numUsed - 1)
                         a[j] = --activeNum;

            }
        }
    }
    numUsed--;
    a[activeNum--];
    activeNum++;
}

Last edited on
I fixed it. Sorry didn't know I was suppose to do that. Hopefully that makes it better.
Also once all the keys been deleted there should be a bracket and nothing in the bracket.
Help please!!
This is my code so far.. I'm stuck on the insert key and the delete key.

Insert and delete are related. Each of them has to move some of the array contents one place to the left or to the right. Well, You can't actually move it, you need a loop to copy each element into the adjacent element - but be careful, or you might end up propagating a duplicate value to all the elements.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void insertFunction(int a[], int& numUsed, int& activeNum)
{
    if (numUsed >= ARRAYS)
        return;
    
    int integer; 
    cout << "\nPlease enter an integer:\n";
    cin  >> integer;
    
    for (int i = numUsed; i>activeNum; --i)
        a[i] = a[i-1];
        
    a[activeNum] = integer;
    
    ++numUsed;
}

I think that works - but it needs testing.
Delete is similar, but a little simpler perhaps.


and the sort key suppose to keep the brackets around [11].

One way - a bit clunky but seems ok. Save the current active integer before the sort.
 
    int active = a[activeNum];
then after sorting, search for it in the array:
1
2
3
4
5
6
    for (int i=0; i<numUsed; ++i)
        if (a[i] == active)
        {
            activeNum = i;
            break;
        }


Also once all the keys been deleted there should be a bracket and nothing in the bracket.

A small adjustment in the display function
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void displayList(int a[], int numUsed, int activeNum)
{
    if (numUsed == 0)
    {
        cout << "[ ]";
        return;
    }
   
    for(int i = 0; i < numUsed; i++)
    {
        if (i == activeNum)
            cout << "[" << a[i] << "]";
        else
            cout << " " << a[i] << " ";
    }
}

Thank you. I just completed the program.
Topic archived. No new replies allowed.