SHOW:
|
|
- or go back to the newest paste.
1 | #include <iostream> | |
2 | ||
3 | int main(int argc, const char*[]) | |
4 | { | |
5 | ||
6 | double iChoice; | |
7 | ||
8 | // Prompt for user to input numbers. | |
9 | std::cout << "Enter a number with decimal points, or press 99 to exit." << std::endl; | |
10 | std::cout << "Your number: "; | |
11 | std::cin >> iChoice; | |
12 | ||
13 | int iNumberOfNumbersEntered = 0; | |
14 | double sum = 0; | |
15 | try | |
16 | { | |
17 | // Loop for executing input. | |
18 | - | while (iChoice != 99) // <-- CHECK ICHOICE INITIALLY FIRST BEOFRE YOU EXECUTE THE LOOP |
18 | + | while (iChoice != 99 && iChoice >= -1000000000 || iChoice <= 1000000000) // <-- CHECK ICHOICE INITIALLY FIRST BEOFRE YOU EXECUTE THE LOOP |
19 | { | |
20 | sum = sum + iChoice; | |
21 | std::cout << sum << std::endl; | |
22 | std::cout << "Enter another number with decimal points." << std::endl; | |
23 | std::cout << "Your number: "; | |
24 | std::cin >> iChoice; | |
25 | iNumberOfNumbersEntered++; | |
26 | } | |
27 | ||
28 | //Throw an exception if the number of numbers entered is 0 to avoid a divide by 0 error. | |
29 | if (iNumberOfNumbersEntered == 0) | |
30 | throw 0; | |
31 | // Throw an exception if the number is more than 10 digits long. | |
32 | if (iChoice <= -1000000000 || iChoice >= 1000000000) | |
33 | throw 1; | |
34 | ||
35 | // Average of numbers added. | |
36 | sum = sum / iNumberOfNumbersEntered; | |
37 | std::cout << "The average for the numbers that you entered is " << sum << "." << std::endl; | |
38 | } | |
39 | ||
40 | catch (int x) | |
41 | { | |
42 | if (x == 0) | |
43 | std::cout << "You must enter more than one number to calculate an average." << std::endl; | |
44 | else if (x == 1) | |
45 | std::cout << "Enter a number in the range of 10 digits." << std::endl; | |
46 | } | |
47 | system("pause"); | |
48 | return 0; | |
49 | } |