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>
#include <fstream>
// ok at namespace scope: all these have internal linkage
constchar* const file_name = "numbers.txt" ;
constexpr std::size_t N = 5 ;
staticint 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
}