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:
/*stranz.cpp */
#include <iostream>
#include <vector>
usingnamespace 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 ()
{
constint max_numbers=40;
cout << "addition of many numbers, even with a point (maximum " << max_numbers<<")\n"<< "enter $ for indicating early end of input\n";
constint 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 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: $
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 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.