Using error handling, how to distinguish real zeros

Hi all ! It is important to conserve all entries. But with this method I don't know how to distinguish real zeros. Is the method using "int argc, char *argv[]" the only one which allows that ?? . Can you help me ? Here is the code:
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
/*stranz.cpp */
#include <iostream>
#include <vector>
using namespace std;
//using error handling method
enum status { OK = 0, NO_INPUT = 1, BADLY_TERMINATED = 2 };
status failableFunction(int max_i )
{
        int i=0;
        double sum=0;
        vector<double> value(40);

        while (i<max_i &&
                        cout << "enter value "<<i+1<<": " &&
                        cin >> value[i] )
        {
                sum += value[i];
                value.push_back(max_i);
                ++i;
        }
        cin.clear();//clear possible failed state of cin if the input was ended before max_i was reached and if the caracter that ended the input was not $
        if ( i<max_i && cin.get() != '$' )
        {
                cout << "\nFunction call failed: no terminating  $  \n";
                return BADLY_TERMINATED ;
        }
        if (i == 0 )
        {
                cout << "\nFunction call failed : no values were entered\n";
                return NO_INPUT ;
        }
        cout << "\nsum:  " <<sum<< " average: " << sum / i <<endl;
        for ( int i=0; i<max_i; i++ )//OK but all cleared values appear
        {
        cout << value[i] << " ";
        }
        return OK ;
}
int main ()
{
        const int max_numbers=40;
        cout << "addition of many numbers, even with a point (maximum " << max_numbers<<")\n"<< "enter $ for indicating early end of input\n";
        const int result = failableFunction(max_numbers);
        if ( result != OK ) cout << "Function call failed with result " << result<<endl;
}
                                                                                                                                                                  10,14-21      Top
Line 18: Why are you pushing max_i every time through the loop?

Line 35: You're displaying max_i, max_i times. You may not have 40 values in your vector. You should be using value.size() for the number of entries in the vector.

Line 43: result should be of type status, not int.

Line 37: Your vector goes out of scope losing all values stored in it. That's okay if all you want to use it for is calculating the sum. Actually for calculating a sum, you don't even need a vector.

I don't know how to distinguish real zeros. Is the method using "int argc, char *argv[]" the only one which allows that ??

I'm not understanding your question.
I modified lines 35 and 43. Here is what happens when I run.
Press ENTER or type command to continue
addition of many numbers, even with a point (maximum 40)
enter $ for indicating early end of input
enter value 1: 1
enter value 2: 2
enter value 3: 3
enter value 4: 4
enter value 5: 5
enter value 6: $

sum: 15 average: 3
1 2 3 4 5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 40 40 40 40 40
Press ENTER or type command to continue
Just before line 35 i put: int cnt=value.size()-max_i; and line 35->36 is now:
for ( int i=0; i<cnt; i++ ) So it works very well. I should like you explain to me what represents: value.size() . About "int argc etc " I mean you join arguments (values) just after the name of the program. Like: ./stranz 4 8 8.7 9
I should like you explain to me what represents: value.size() .

size() is a member function of a std::vector. It returns the number of entries in the vector.
http://www.cplusplus.com/reference/vector/vector/size/

I mean you join arguments (values) just after the name of the program.

I understand how arguments are passed. I'm having trouble relating that to your question.

If you create a vector and push each valid input onto the vector, vector::size() will contain the number of values, just as argc contains the number of arguments. Is that what you meant?



Yes you are right. I ask myself what method is the best, error handling or joined arguments ? I have a file with joined arguments that works very well. If you wish I send it to you. Many thanks for your collaboration.
Topic archived. No new replies allowed.