/*somme.cpp */
#include <iostream>
usingnamespace std;
int failableFunction()
{
double sum =0;
double value[40];
int result=0;
for (int i = 0; i < 40; ++i )
{
cout << "Enter value: " << i << ": ";
cin >> value[ i ];
sum += value[ i ];
}
if (result != 0 )//this line here or in the main ?
{
cout << "Function call failed: "<< result;
if (static_cast<int>(value[ i ])== 36 )//36 is ASCII of $//: error: name lookup of ‘i’ changed for ISO ‘for’ scoping [-fpermissive]
{
cout << "Here is the sum : " <<sum<<endl;
double nomb=i;
cout << "Here is the average value: " << sum / nomb << endl;
return 0;
}
}
}
int main ()
{
cout << "Here is the addition of many numbers (even with a .).MAXIMUM OF NUMBERS=39 : \n";
cout << "Once you have finished your list of numbers, hit $ for indicating the end \n";
constint result = failableFunction();
if (result != 0 ) cout << "Function call failed: " << result ;//this line here or in tha function ?
}
#include <iostream>
enum status { OK = 0, NO_INPUT = 1, BADLY_TERMINATED = 2 };
status failableFunction( int max_cnt )
{
int cnt = 0 ;
double sum = 0 ;
double value ;
while( cnt < max_cnt &&
std::cout << "enter value # " << cnt+1 << ": " &&
std::cin >> value )
{
sum += value ;
++cnt ;
}
std::cin.clear() ; // clear possible failed state of std::cin
// if the input was ended before max_cnt was reached
// and if the character that ended the input was not $
if( cnt < max_cnt && std::cin.get() != '$' )
{
std::cout << "\nFunction call failed: no terminating '$'\n" ;
return BADLY_TERMINATED ;
}
if( cnt == 0 )
{
std::cout << "\nFunction call failed: no values were entered\n" ;
return NO_INPUT ;
}
std::cout << "\nsum: " << sum << " average: " << sum / cnt << '\n' ;
return OK ;
}
int main ()
{
constint max_numbers = 40 ;
std::cout << "addition of many numbers (maximum " << max_numbers << ")\n"
<< "enter $ for indicating early end of input\n";
constint result = failableFunction(max_numbers);
if( result != OK ) std::cout << "Function call failed with result " << result << '\n' ;
}
Your file works very well ! Thank you very much. Allain's proposition is very simple but unfortunately it doesn't work, or perhaps I don't understand it.