edit the array , but also changing old array


hi guys , i tried to do the option that the user can edit the array(all element) , but when im running the program again , it uses the old array in the beginning. i want to use the new array to use with the different functions in menu. please show me how to do this correctly, im not that good in c++. i hope for a reply and thank you.



#include <iostream>
using namespace std;

const int NUMBERS = 5;

double b(int[]);
int main()
{
char opt;
int num[NUMBERS];
int d;
cout << "please enter " << NUMBERS << " numbers " << endl;
cin >> num[0];
cin >> num[1];
cin >> num[2];
cin >> num[3];
cin >> num[4];
do
{
cout << "Menu of option" << endl;
cout << endl;
cout << "1. Sum" << endl;
cout << "2. Mean" << endl;
cout << "3. Display" << endl;
cout << "4. Edit" << endl;
cout << "5. Exit" << endl;
cout << endl;
cout << "Please insert your choice ( only menu letter):" << endl;
cin >> opt;
cout << endl;
switch (opt)

{ case 4:
// user's choice to edit

cout << "please insert 5 new numbers" << endl;
b(num);
break;
case 5:
break;
}
cout << endl;
cout << " run the program again(1) or quit (2) ?" << endl;

cin >> d;
// user's choice
cout << endl;
} while (d == 1);
system("pause");
}

double b(int[])
// function to change the array
{
int num[NUMBERS];
cin >> num[0];
cin >> num[1];
cin >> num[2];
cin >> num[3];
cin >> num[4];

return NUMBERS;
}
Last edited on
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
#include <iostream>
#include <fstream>

// ok at namespace scope: all these have internal linkage
const char* const file_name = "numbers.txt" ;
constexpr std::size_t N = 5 ;
static int numbers[N] {} ;

void save() // write the numbers into the file
{
    std::ofstream file(file_name) ; // open the file for output
    // range based loop: for each number v in the array numbers
    for( int v : numbers ) file << v << '\n' ; // write each number into the file
}

void load() // read in the numbers from the file
{
    std::ifstream file(file_name) ; // open the file for output
    for( int& v : numbers ) file >> v  ; // read into (note: int&) each number from the file
}

int main()
{
    load() ; // right at the start, load the numbers from the file

    // ...

    save() ; // at the end, write the numbers into the file
}
Topic archived. No new replies allowed.