Receiving the data from file to the function

I need a small help for my c++ function. I am writing a function called "calculate". The Pr[o] array is a data in a file called "data" and is read from the file properly, there is no problem in that part. I have problem in the calculate function because Pr[o] is underlined with red , since it is error. I guess the filename should be the parameter of the calculate function. I do not know how to do that. How can I transfer these data which is read from the file in the function.Can you help me please ? Thank you so much

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
  nbO=15;
void ReadFile()
{
char* filename = "C:..../data.dat";
vector<double>Pr(nbO);
ifstream file(filename);
if (!file) {
     cerr << "ERROR: could not open file '" << filename << "' for reading" << endl;
     cerr << "ERROR: could not open file for reading" << endl;
    throw(-1);
}

char c;
if (file.is_open())
{
for (int i = 0; i < nbO; i++) file >> c >> Pr[i];
     file >> c;}
}

double calculate()
{
   double yy=0;
   for (int k = 0; k < NbO; ++k) {
        yy += 5* Pr[k];
}
return yy;
}
You're saying the data is read from the file correctly, which means that your program compiles and runs, yes? Is the red underline you speak of just a Visual Studio "IntelliSense" error? Hover over it to see what it says is the issue.

Some issues:
- Is it nbO or NbO? C++ is case-sensitive.
- Assignments that aren't initializations cannot happen in the global scope. Your line 1 doesn't make sense. Perhaps something got cut off?
- char* should not point to a string literal. Use const char* (line 4).
- Your indentation is inconsistent and misleading. (e.g. line 17 is not a part of your for loop, is this intentional?)

Edit: Now I see, your primary issue is in fact a compiler error. What you are missing is that the scope of your Pr variable on line 5 is limited to your ReadFile() function. If you wish to use the Pr vector in a different function, you must pass it to that function.

e.g.
1
2
3
4
5
6
7
8
double calculate(const vector<double>& Pr)
{
    double yy = 0;
    for (int k = 0; k < NbO; ++k) { // assumes NbO is a global variable/constant
        yy += 5 * Pr[k];
    }
    return yy;
}
Last edited on
Hello learner999,

As much as I would like to work on and comment on your code with out the missing code and the input file it is hard to say if what you have posted works or not.

What I can do is:
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
nbO = 15;

void ReadFile()
{
    char* filename = "C:..../data.dat";
    //std::string fileName{ "C:..../data.dat" };  // <--- For C++11 and beyond.

    vector<double>Pr(nbO);

    ifstream file(filename);

    if (!file)
    {
        cerr << "ERROR: could not open file '" << filename << "' for reading\n";
        cerr << "ERROR: could not open file for reading" << endl;

        throw(-1);  // <--- But where is it used? "return 1" would work better.
    }

    char c;

    if (file.is_open())
    {
        for (int i = 0; i < nbO; i++)
            file >> c >> Pr[i];

        file >> c;
    }
}

double calculate()
{
    double yy = 0;

    for (int k = 0; k < NbO; ++k)
    {
        yy += 5 * Pr[k];
    }

    return yy;
}

Notice the if statement at line 22.

Your original code made it difficult to understand especially with the indenting that you had.

This makes more sense and begs the question what is "c"?

Andy
> How can I transfer these data which is read from the file in the function.

Pass the data to the function. For example:

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
#include <iostream>
#include <string>
#include <vector>
#include <fstream>

// read the contents of the file into a vector, return the vector
std::vector<double> read_file( const std::string& file_name )
{
    if( std::ifstream file{ file_name } ) // if the file was opened for inpuit
    {
        std::vector<double> numbers ;

        double value ;
        while( file >> value ) numbers.push_back(value) ;

        return numbers ;
    }

    else // failed to open file
    {
        std::cerr << "*** error *** : failed to open input file " << file_name << '\n' ;
        return {} ; // return empty vector
    }
}

// the data on which calculation is performed is the input to the function
double calculate( const std::vector<double>& numbers )
{
    double result = 0.0 ;

    // https://www.stroustrup.com/C++11FAQ.html#for
    for( double num : numbers ) // for each number in the vector
        result += 5.0 * num ;

    return result ;
}

int main()
{
    const std::string file_name = "data.dat" ;

    const std::vector<double> data = read_file(file_name) ; // read the file into a vector

    if( !data.empty() ) // if the vector is nopt empty
    {
        std::cout << "read " << data.size() << " numbers from file\n" ;

        const double calc_result = calculate(data) ; // pass the vector to he function

        std::cout << "calculation result is: " << calc_result << '\n' ;
    }

    else std::cerr << "*** error *** : empty input data\n" ;
}

Hello everyone,
Thank you for your answers. I have a question to JLBorges, in your proposition, there is only one vector which is called "numbers", and your function readfile only reads and turns that. What happens, if we need to read several vectors in the data file but we need to use only one of them in the calculate function in the main block? Imagine that we have three vectors like numbers, numbers2, numbers3 in data file . I need to read everything in data file because in one function called "calculate" I will use the data numbers2 and numbers3 as input, in the another function called calculate2, I will use only the vector called numbers. I really got stuck at this pont.

I will apprecaite alll the helps
Thank you so much
> What happens, if we need to read several vectors in the data file

If all the numbers are in the same data file, there must be a way to identify when the numbers destined for each vector ends and the next group of numbers begins.

As an example, if the the numbers for one vector are in one line in the file (the data is separated by new lines), we could read the file line by line and create one vector with the numbers in one line.
Then, it could be something like this:

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
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <fstream>

// get integers in one line into a vector
std::vector<int> get_integers_from( const std::string& line )
{
    // create an input string stream and read the numbers from that
    std::istringstream stm(line) ;

    std::vector<int> vec ;

    int n ;
    while( stm >> n ) vec.push_back(n) ; // coukld use istream_iterator<int> instead

    return vec ;
}

double calculate_something( std::vector<int>& a, std::vector<int>& b ) ;
double calculate_something_else( std::vector<int>& data ) ;

int main()
{
    const std::string filename = "C:..../data.dat";

    if( std::ifstream file{filename} ) // if the file was opened
    {
       std::string line ;

       std::vector< std::vector<int> > vectors ; // collection of vectors

       // read the file line by line and add vectors made from the line to the collection of vectors
       while( std::getline( file, line ) )
           vectors.push_back( get_integers_from(line) ) ;

       // use the vectors
       // for example, the first two vectors to calculate something
       if( vectors.size() > 1 )
       {
           const double result = calculate_something( vectors[0], vectors[1] ) ;
           // use result
       }

       // the the third vector to calculate something else
       if( vectors.size() > 2 )
       {
           const double result2 = calculate_something_else( vectors[2] ) ;
           // use result2
       }
    }
}
Thank you JLBorges, it worked , thank you so much again
Topic archived. No new replies allowed.