Continue entering in array where previously left off

Hi.
i am working on a code in where the add_values selection will allow the user to enter numbers into an array.
My question is : how can i change my code so that this function must ensure that if the user only enters a few numbers, leaves to access a different
menu choice, then returns to enter more numbers, that previously stored values are not overwritten

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
#include <iostream>
using namespace std;
#define array_max_size = 10
void Display_menu();
void Add_values();
void Edit_values();
void Print_values();
void Display_Stats();
void Quit_program();
float stored_values[10];
int element_to_change;
int max();
int min();

int main()
{
cout << " Hello please choose a option! " << endl;

Display_menu();

cout << " thanks for using my program" << endl;

return 0;
}

void Display_menu()
{   int n;
    //float (num_array)[array_max_size] = {10};
{
        cout << " 1 - Add Values " << endl;
        cout << " 2 - Edit Values " << endl;
        cout << " 3 - Print Values " << endl;
        cout << " 4 - Display Statistics " << endl;
        cout << " 5 - Quit Program " << endl;
        cin>>n;
{   if (n<0 || n>5)
        cout << "not a menu item" << endl;
        else
{
    if (n==1){Add_values();}
    if (n==2){Edit_values();}
    if (n==3){Print_values();}
    if (n==4){Display_Stats();}
    if (n==5){Quit_program();}
}
}
}
return Display_menu();
}
void Add_values()
{
    cout<<"You selected option 1 to Add values"<<endl;
    cout<<"Please enter 10 numbers between -999 and 999 and number outside this range will cause the program to terminate" << endl;
    cout<<"Enter -1000 to exit this function" << endl;
for(int i=0; i<10; ++i)
{
if(stored_values[i]<-999||stored_values[i]>999)
{
    cout<< " Please try again " << endl;
    cout<< " Enter a number between -999 and 999" << endl;
}

else
{
cout<< "What number would you like to enter?" << endl;
cin>> stored_values[i];
}

if(stored_values[i]==-1000){ break;}
cout<< "You have entered value of ["<< i << "]"<<" is: "<<stored_values[i]<<endl;
}
}
closed account (48T7M4Gy)
You could have a counter variable that updates as data is input. Make sure the counter has scope outside the menu. It doesn't need to be global though, more than likely.
Hey Kemort! Thanks for your help last time!

I do not know how to input a counter variable nor how they work? could you make an example?
closed account (48T7M4Gy)
Well it depends exactly where you want to stop and start but the idea I had in mind from what you wrote is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
main(){

int counter = 0;

go to some_function(&counter) //counter is zero
do other_stuff //counter is updated a couple of times say
...
go to some_function(&counter) // counter is now 3 say so element 4 goes in
...

}

In short the counter keeps track of the last position in the list/array


some_function(&counter)
{
  add new element
  element [counter] = xyz
  increment counter
}
Topic archived. No new replies allowed.