This is an exercise for my Intro to CS class (not homework) that I am doing on my own. It asks to write a function "SumDigits", that takes a string as input and sums the values for each digit characters (0 through 9) and then returns the integer value for the sum.
Example: if the function is called with ("123") in main(), SumDigits should return 6(1+2+3).
All of my searches on the internet deal with getting a letter from a string and printing that out.
I only have to write the user-defined function and I keep getting 0 as my return value.
1 2 3 4 5 6 7 8 9 10 11 12
int SumDigits (string strInput) { //get the string from main
int index = 0; //initialize index position
int sum = 0; //declare sum variable
int strLen = (int)strInput.length(); //declare and initialize variable to hold the value of string length
while (strInput[index] <= strLen){
int str = strInput[index]; //declare int variable to hold value of string position
sum = sum + 1;
index ++; //goes to the next position in the string
}
return sum;
}
I changed things up but now I'm getting 49 for SumDigis("123") instead of 0. The ASCII table reference makes sense but I'm still not seeing what I'm doing wrong?
1 2 3 4 5 6 7 8 9 10 11 12 13
int SumDigits (string strInput) {
int index = 0;
int sum = 0;
int strLen = (int)strInput.length();
while (index < strLen) {
char character = strInput[index];
int number = character;
sum = number + sum;
index ++;
}
return sum;
}
I'm still a little perplexed at what EXACTLY is going on and if someone would maybe give me a step-by-step of each action, that would be a really helpful learning tool!
Thanks for all the help either way.
Here is the correct code (according to the machine my school uses):
1 2 3 4 5 6 7 8 9 10 11 12 13 14
int SumDigits (string strInput) {
int index = 0;
int sum = 0;
int strLen = (int)strInput.length();
while (index < strLen){
sum = (strInput[index] - '0') + sum;
index ++;
}
return sum;
}
For example, the latest code differs from the earlier version in only one significant respect:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
int SumDigits (string strInput) {
int index = 0;
int sum = 0;
int strLen = (int)strInput.length();
while (index < strLen) {
char character = strInput[index];
int number = character;
number = number - '0'; // convert character code (ASCII) to integer
sum = number + sum;
index ++;
}
return sum;
}
that is to say, it is the conversion of the character which is encoded usually in ASCII (though other encoding systems exist), to a plain integer. It is dependent on the fact that the digits '0' to '9' are arranged in consecutive positions in the ASCII table.
The rest of the code is the mechanics of iterating through each character in the string - though it is a bit overly complex here. Taking the suggestion made earlier by Thomas1965, it could be reduced to this
1 2 3 4 5 6 7 8 9 10 11
int SumDigits (string strInput)
{
int sum = 0;
for (char ch : strInput)
{
sum += ch - '0';
}
return sum;
}
Or indeed using a more traditional for loop in place of the while loop:
1 2 3 4 5 6 7 8 9 10 11
int SumDigits (string strInput)
{
int sum = 0;
for (unsignedint index = 0; index < strInput.length(); ++index)
{
sum += strInput[index] - '0';
}
return sum;
}
#include <iostream>
#include <string>
usingnamespace std;
int SumDigits (string strInput);
int main()
{
string strInput;
int sum;
cout << "Please a a line of numbers.. "<< endl;
getline(cin, strInput); // Using getline, in case spaces are inputted as well
sum = SumDigits(strInput);
cout << "The total of the numbers entered, was " << sum << endl << endl;
return 0;
}
int SumDigits (string strInput)
{ //get the string from main
int x;
int sum = 0; //declare sum variable
int strLen = strInput.length(); //Variable to hold the value of string length
for(x=0;x<strLen;x++)
{
if(strInput[x] <'0' || strInput[x]>'9') // Checking if the value is NOT a digit
cout << strInput[x] << " is NOT a digit..." << endl;
else // Otherwise
sum+= strInput[x]-'0'; // Increase value of sum
}
return sum;
}