The instructions:
Write a program that asks the user to enter a series of single digit numbers with nothing separating them. Read the input as a C-string or a string object. The program should display the sum of all the single digit numbers in the string. For example, if the user enters 2514, the program should display 12, which is the sum of 2,5,1, and 4. The program should also display the highest digit in the string.
Question: How would I be able to get the total properly? Number is always around 200 for the total whenever I try it.
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
constint LENGTH = 100;
char digits[LENGTH];
int total = 0;
cout << "Enter a series of digits with no spaces between them: ";
cin >> digits;
for (int j=0; j < strlen(digits); j++)
{
while (!isdigit(digits[j]))
{
cout << "One of the numbers you have inputted is not a digit." << endl;
cout << "Please try again." << endl;
cin >> digits;
}
}
for (int index = 0; index < strlen(digits); index++)
{
total += digits[index];
}
cout << "The sum of those digits is " << total << "." << endl;
char max = digits[0];
for (int i = 0; i < strlen(digits); i++)
{
if (digits[i] > max)
{
max = digits[i];
}
}
cout << "The highest digit is " << max << endl;
return 0;
}