I need to write a program that asks the user to enter a series of single-digit numbers with nothing separating them. Then, 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 and lowest digits in the string.
*I need help getting the total sum of the series of integers*
They would like me to use the function below:
int charToInt(char)
The function accepts a char and returns the char's value as an integer digit. If the char is not a digit, the function returns 0.
#include <iostream>
#include <cstring>
usingnamespace std;
//List function prototypes below
bool isNumber(char[]);
int charToInt(char[]);
int main()
{
char userInput[100];
int characterConverter, total;
cout << "Enter a series of digits with no spaces between them.\n";
cin.getline(userInput,81);
//characterConverter = atoi(userInput);
characterConverter = charToInt(userInput);
//total += characterConverter;
while(!isNumber(userInput))
{
cout << "Incorrect input....?" << endl;
cout << "Enter a series of digits with no spaces between them.";
cin.getline(userInput,81);
characterConverter = charToInt(userInput);
//characterConverter = atoi(userInput);
}
cout << "The numbers are " << characterConverter << "." << endl;
//cout << "The sum is " << total << "." << endl;
return 0;
}
bool isNumber(char str[])
{
for(int index = 0; index < strlen(str); index ++)
{
if (isdigit(str[index]) == false)
returnfalse;
}
returntrue;
}
int charToInt(char convert[])
{
int converted = atoi(convert);
return converted;
}
#include <iostream>
#include <cstring>
usingnamespace std;
//List function prototypes below
bool isNumber(char[]);
int charToInt(char[]);
int main()
{
char userInput[100];
int characterConverter, total, nc;
cout << "Enter a series of digits with no spaces between them.\n";
cin.getline(userInput,81);
characterConverter = atoi(userInput); //To show the raw integers in the cout of the original numbers
//For Loop to total the sum of the integers entered into the c string
for(int index = 0; index < strlen(userInput); index++)
{
total += charToInt(userInput);
}
while(!isNumber(userInput))
{
cout << "Incorrect input....?" << endl;
cout << "Enter a series of digits with no spaces between them.";
cin.getline(userInput,81);
characterConverter = atoi(userInput); //To show the raw integers in the cout of the original numbers
}
cout << "The numbers are " << characterConverter << "." << endl;
cout << "The sum is " << total << "." << endl;
return 0;
}
bool isNumber(char str[])
{
for(int index = 0; index < strlen(str); index ++)
{
if (isdigit(str[index]) == false)
returnfalse;
}
returntrue;
}
int charToInt(char convert)
{
if(isdigit(convert))
return convert - '0';
elsereturn 0;
}
int sumStringCharsToInt(char str[]) {
int total = 0;
for(int index = 0; index < strlen(str); index++)
{
total += charToInt(str[index]);
}
return total;
}