C++ array based list

Hello,

I am writing a menu based program that has the following array based list 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
  #include <iostream>

using namespace std;




int main()
{
int counter = 0;
int anarray[9] = {3, 1, 10, 2, 7, 14, 5, 8, 2};
int number =0;

  do{
       while(counter <= 8){

      cout << anarray[counter] << endl;
      counter++;
      }

    cout << "Please enter a number(enter -1 to quit): ";
    cin >> number;

      if (number >= 0 || number <= 8 && number != -1)
       {
        cout << "\nYour number: " << anarray[number] << endl;
       }

   }while(number != -1);
    return(0);
}


I have made an array already but not sure where to go from there. Not totally asking for the answer.Do I need to make a class to do this?
Thank you for your help.
closed account (28poGNh0)
what is the problem ?

Your code is fine as long as the using number from 0 to 9

what are you still need ?
Implementing the array based list functions

add
insert
delete
show

I do not know how to implement these functions. At first I thought I might have to use a class but I'm not sure.
closed account (28poGNh0)
we can do that easily using templates ,or with only an array like yours if you're a beginer

what do you want to choose
Still a beginner so an array
Last edited on
The condition for your if statement is wrong.

1
2
3
4
5
//What you think it's doing
if ((number >= 0 || number <= 8) && number != -1)

//what it's actually doing (&& has a higher precedence than ||)
if (number >= 0 || (number <= 8 && number != -1))


Even if it did what you thought is was doing, it still wouldn't be doing what you think it is doing. Say I enter 10:

(number >= 0 || number <= 8) is true (10 is >= 0)
number != -1 = true
if we and these results now, we get true and the program spits out some garbage value.

The solution you ask? Change your if statement to this:
if (number >= 0 && number <= 8)
Thank you guys for help so far.

I fixed that part up Danny.
Still a beginner

What all have you covered/ learned so far? An array wrapper wouldn't be too hard to implement, and it is certainly a good beginner OOP exercise. You can even build on it as you learn more about OOP and C++ in general.
I have done arrays and covered up to classes and pointers. However, I don't have a good grasp of these things, and I would be willing to try an array wrapper as you suggested.
closed account (28poGNh0)
I did not test very well ,if some errors occur please dont hesitat
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
# include <iostream>
using namespace std;

void menu(void)
{
    cout << endl << "Choose an option" << endl;
    cout << "1 : add a number" << endl;
    cout << "2 : delete a number" << endl;
    cout << "3 : show a number" << endl;
    cout << "4 : quit program" << endl << endl;
    // What is the difference between adding and inserting ?
}

int main()
{
    const int minNbrs = 1;
    const int maxNbrs = 20;
    int curNbrs = 9;

    int anarray[20] = {3, 1, 10, 2, 7, 14, 5, 8, 2};
    int choice = 0;

    while(choice!=4)
    {
        menu();
        cin >> choice;

        int number;

        switch(choice)
        {
            case 1:
            {
                cout << "Enter your number -> ";cin >> number;
                if(curNbrs<maxNbrs)
                    anarray[curNbrs++] = number;
                else cout << "The array is full" << endl;
            }
            break;
            case 2:
            {
                cout << "Please enter the index of the array : ";cin >> number;
                if(curNbrs==minNbrs)
                    cout << "The array is empty" << endl;
                else if(curNbrs>minNbrs)
                {
                    if(number<curNbrs)
                    {
                        curNbrs--;
                        for(int i=number;i<curNbrs-1;i++)
                        {
                            anarray[i] = anarray[i+1];
                        }
                    }else cout << "This index is invalid" << endl;
                }
            }
            break;
            case 3:
            {
                cout << "Please enter the index of the array : ";
                cin >> number;

                if(number >= 0 && number <= curNbrs-1)
                    cout << "\nYour number: " << anarray[number] << endl;
                else
                    cout << "You are out of rang ,you can index from " << minNbrs-1 << " to "
                    << curNbrs-1 << endl;
            }
            break;
            case 4:
            break;
        }
    }

    return(0);
}
Last edited on
Topic archived. No new replies allowed.