简体   繁体   中英

Read from text file and count the number of integers using sizeof results to nan

I'm writing a code to count the number of integers read from a text file, sizeof function is displaying "nan".

double getSamplesize(vector<double> data)
{
    int samplesize;
    samplesize=sizeof(data);
    cout<<samplesize<< endl;

}

on the int(main):

cout<<"sample size: " << getsamplesize(arr) << endl;

Expected output should be: sample size: (number) But im getting

(number) sample size: nan

 double getSamplesize(vector<double> data) ^^^^^^ 

You've declared that the function returns double . But your function does not end in a return statement. As a result, the behaviour of the program is undefined, and that undefined behaviour is what you observe.

 sizeof(data) 

This returns the size of the type of the variable. It is completely unrelated to the number of elements, or the size of the array owned by the vector.

Expected output should be: sample size: (number) But im getting

The first piece of code in your program that streams into standard output is this line:

 cout<<samplesize<< endl; 

Thus the expected output cannot possibly begin with "sample size:".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM