Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- int main(int argc, const char*[])
- {
- double iChoice;
- // Prompt for user to input numbers.
- std::cout << "Enter a number with decimal points, or press 99 to exit." << std::endl;
- std::cout << "Your number: ";
- std::cin >> iChoice;
- int iNumberOfNumbersEntered = 0;
- double sum = 0;
- try
- {
- // Loop for executing input.
- while (iChoice != 99)
- {
- sum = sum + iChoice;
- std::cout << sum << std::endl;
- std::cout << "Enter another number with decimal points." << std::endl;
- std::cout << "Your number: ";
- std::cin >> iChoice;
- iNumberOfNumbersEntered++;
- }
- //Throw an exception if the number of numbers entered is 0 to avoid a divide by 0 error.
- if (iNumberOfNumbersEntered == 0)
- throw 0;
- // Throw an exception if the number is more than 10 digits long.
- if (iChoice <= -1000000000 || iChoice >= 1000000000)
- throw 1;
- // Average of numbers added.
- sum = sum / iNumberOfNumbersEntered;
- std::cout << "The average for the numbers that you entered is " << sum << "." << std::endl;
- }
- catch (...)
- {
- std::cout << "You must enter more than one number to calculate an average." << std::endl;
- }
- catch (...)
- {
- std::cout << "Enter a number in the range of 10 digits." << std::endl;
- }
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement