Look for experienced man in error handling

Hi all ! With the book of Alex Allain, I try to finish this file. I think I will never be able to solve that . May someone 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
/*somme.cpp */
#include <iostream>
using namespace 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";
        const int result = failableFunction();
        if (result != 0 ) cout << "Function call failed:  " << result ;//this line here or in tha function ?

}
                                                                                                                                                              
 
Look for experienced man personin error handling
Sorry, I didn't intend to offence anyone.
closed account (48T7M4Gy)
It's actually man -> person -> perchild.
Try this:
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
#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 ()
{
    const int max_numbers = 40 ;
    std::cout << "addition of many numbers (maximum " << max_numbers << ")\n"
              << "enter $ for indicating early end of input\n";

    const int 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.
Topic archived. No new replies allowed.