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" ;
}

Registered users can post here. Sign in or register to post.