//Dione Torrevillas CS1A Chapter 10, P. 589, #8
/******************************************************************************
* SUM OF DIGITS IN A STRING
* This program accepts a series of digits and displays their
* sum, highest digit, and lowest digit.
*
* INPUT:
* numbers : String of digits entered by the user
*
* OUTPUT:
* sum : Sum of all digits
* highest : Highest digit
* lowest : Lowest digit
******************************************************************************/
#include <iostream>
#include <string>
using namespace std;
int main()
{
string numbers;
int sum = 0, highest = 0, lowest = 9;
int digit;
// Get a string of digits from the user.
cout << "Enter a series of digits: ";
getline(cin, numbers);
// Check for an empty string.
if (numbers.empty())
{
cout << "No digits were entered." << endl;
return 0;
}
// Process each digit in the string.
for (int count = 0; count < static_cast<int>(numbers.length()); count++)
{
// Make sure the character is a digit.
if (numbers[count] < '0' || numbers[count] > '9')
{
cout << "Please enter digits only." << endl;
return 0;
}
// Convert the character to an integer.
digit = numbers[count] - '0';
sum += digit;
// Find the highest and lowest digits.
if (digit > highest)
highest = digit;
if (digit < lowest)
lowest = digit;
}
// Display the results.
cout << "Sum of digits: " << sum << endl;
cout << "Highest digit: " << highest << endl;
cout << "Lowest digit: " << lowest << endl;
return 0;
}