How to use new array outside its function ?

Jan 23, 2016 at 8:02pm
Hello, how do I use variables "Number" and "Price" in another function after setting them from normal variable to array with "new" ? And why cant I display new array outside "ReadFile" function ? P.S Sorry about using quotes instead of format, but it doesnt seem to work.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <fstream>
using namespace std;
void ReadFile(int &DifferentBooks, int *Number, double *Price)
{
ifstream ifile("Read.txt");
ifile >> DifferentBooks;
Number=new int[DifferentBooks];
Price=new double[DifferentBooks];
for(int x=0;x<DifferentBooks;x++)
{
    ifile >> Price[x] >> Number[x];
}
}
int main()
{
    int DifferentBooks, Number;
    double Price;
    ReadFile(DifferentBooks, &Number, &Price);
    cout << Number[0] << endl; // why doesnt this work ? and how to use these arrays in another function ?
    return 0;
}
Jan 23, 2016 at 8:22pm
You need to pass a pointer to the function ReadFile.
1
2
3
4
5
6
7
8
int main()
{
    int DifferentBooks, *Number;
    double *Price;
    ReadFile(DifferentBooks, Number, Price);

    return 0;
}

You can pass Number and Price to other functions as well.
Don't forget to delete your arrays when you are finished.
If arrays are not required by an assignment then consider using vectors or lists instead.
Jan 23, 2016 at 8:32pm
Also, the pointers should be passed by reference, since the actual value of the pointer will be changed. Other functions wouldn't need the reference.
1
2
3
void ReadFile(int &DifferentBooks, int *&Number, double *&Price)
{
// etc. 
Jan 23, 2016 at 8:43pm
Ive made all of the suggested changes and now program runs, but it throws some random number. Any idea why ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <fstream>
using namespace std;
void ReadFile(int &DifferentBooks, int *&Number, double *&Price)
{
ifstream ifile("Read.txt");
ifile >> DifferentBooks;
Number=new int[DifferentBooks];
Price=new double[DifferentBooks];
for(int x=0;x<DifferentBooks;x++)
{
    ifile >> Price[x] >> Number[x];
}
}
int main()
{
    int DifferentBooks, *Number;
    double *Price;
    ReadFile(DifferentBooks, Number, Price);
    cout << Number[0];
    return 0;
}
Last edited on Jan 23, 2016 at 9:03pm
Jan 23, 2016 at 8:54pm
Did you check that the file was opened successfully?
1
2
3
4
5
ifstream ifile("Duomenys3.txt");
if (!ifile)
{
 // error message
}


Also, try displaying the value of DifferentBooks
1
2
ifile >> DifferentBooks;
cout << "DifferentBooks: " << DifferentBooks  << '\n'; 


And remember to release the memory when you are finished, using delete[].
1
2
3
4
5
6
7
8
9
10
11
int main()
{
    int * Number = nullptr;
    double * Price = nullptr;

// rest of processing here
    
    delete [] Number; 
    delete [] Price;
    return 0;
}


Jan 23, 2016 at 9:00pm
I feel so stupid now, I accidently renamed my text file :| Everything seems to be working now, and as I understood, I need to use "delete" only at the end of the program ?
Jan 23, 2016 at 9:25pm
I need to use "delete" only at the end of the program ?
In this case yes. But there may be other circumstances where you want to allocate some memory just for the duration of a certain part of the process, in that case use delete [] when you no longer need the allocated memory.

Though if possible, the use of raw pointers should be avoided. C++ has smart pointers which take care of their own resources. Or in this case as Thomas1965 suggested, you might better use a vector or a list instead.


Jan 23, 2016 at 9:35pm
Ok, thank you Thomas and Chervil for your help !
Topic archived. No new replies allowed.