Help summing a series of digits with a c-string in c++

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
#include <cstring>

using namespace 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)
        return false;
	}
	return true;
}

int charToInt(char convert[])
{
    int converted = atoi(convert);

    return converted;
}
Last edited on
> int charToInt(char)
> The function accepts a char and returns the char's value as an integer digit.

So you wrote
> int charToInt(char[]);

So your first step is to implement something which accepts a single char and not a string.

For a single char, it's dead easy. It's just ch - '0';

Then you would call that function from main within a loop iterating over the length of the string.
so something like this

1
2
3
4
5
6
7
8
int charToInt(char convert)
{
    if(isdigit(convert))
        return convert - '0';
    else
        return 0;
}
Last edited on
Looks good so far :)
The for loop I am looking at making would be

1
2
3
4
5
6

for(int index = 0; index < strlen(userInput); index++)
{
     total += charToInt(userInput);
}


how would I go about making the function work with the c string userInput[100];
This is what I have so far, how would I go about making the charToInt compatible with my c string with a storage size?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>
#include <cstring>

using namespace 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)
        return false;
	}
	return true;
}

int charToInt(char convert)
{
    if(isdigit(convert))
        return convert - '0';
    else
        return 0;
}
So make it a function.
1
2
3
4
5
6
7
8
int sumStringCharsToInt(char str[]) {
    int total = 0;
    for(int index = 0; index < strlen(str); index++)
    {
        total += charToInt(str[index]);
    }
    return total;
}
Thank you it works perfectly! I was so just confused because I never worked with C strings and instead have worked with string objects from c++ more
Topic archived. No new replies allowed.