int to string conversion issues

I seem to be making mistakes with how conversions from int into strings and then evaluating the string characters and then pushing certain results into a vector
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
59
  //
//  main.cpp
//  last-digit-sum
//
//  Created by Zack Fanning on 9/23/15.
//  Copyright © 2015 Zackluckyf. All rights reserved.
//

/*
 For a non-negative integer N, define S(N) as the sum of the odd digits of N
 plus twice the sum of the even digits of N. For example, S(5)=5, 
S(456)=2*4+5+2*6=25, and S(314159)=3+1+2*4+1+5+9=27. Define D(N) as the last
 digit of S(N). So D(5)=5, D(456)=5, and D(314159)=7. Given 2 non-negative 
integers A and B, compute the sum of D(N) over all N between A and B, 
inclusive.
 */

#include <iostream>
#include <string>
#include <vector>

int main(int argc, const char * argv[]) {
    int start, end, temp, length, tempNum, productLength, sum = 0, count = 0;
    int total = 1;
    std::vector<int> list;
    std::cout << "Enter the starting digit: ";
    std::cin >> start;
    std::cout << "Enter the ending digit: ";
    std::cin >> end;
    for (int i = start; i <= end ; i ++)
    {
        temp = i;
        std::string number = std::to_string(temp);
        length = number.length();
        for(int j = 0; j < length; j++)
        {
            if (number.at(j) % 2 == 0)
            {
                tempNum = (number.at(j) -'0') * 2;
            }
            else
            {
                tempNum = number.at(j) - '0';
            }
        }
        // I think most of the issues are here
        std::string product = std::to_string(tempNum);
        productLength = product.length();
        list.push_back(product.at(productLength -1));
    }
    for (int i = 0; i < list.size(); i++)
    {
        sum = sum + list[i] - '0';
    }

    std::cout << "\n" << sum << "\n";
    
    return 0;
}


Example answers
1 and 8 gives 36 which I get
28 and 138 gives 495 I get 501
314159 and 314159 gives 7 and I get 9




Last edited on
I can't see why you're doing anything with strings to begin with. You're asked to evaluate a function that takes a number, does number stuff and returns a number.

The requirement is: For a non-negative integer N, define S(N) as the sum of the odd digits of N plus twice the sum of the even digits of N.

For a non-negative integer N
You're going to be working with unsigned int rather than int.

You need to work out the odd and even digits in N. You need to work out a way to determine that without resorting to strings. Then your code looks like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
unsigned S(unsigned N)
{
    unsigned ret = 0;
    // ... do it!
    return ret;
}

int main()
{
    unsigned n;
    std::cout << "Enter a non-negative number: ";
    std::cin >> n;

    std::cout << S(n) << std::endl;
}
I don't think using strings is that bad of an idea -- it breaks a number into easy-to-access digits.

I'm not sure OP understands the depth of his HW, though.

You are being asked to do a number of things:


First, you must implement S. This is where you would use a string, or, as kbw suggests, no strings (use division / and remainder % to get individual digits).

1
2
3
4
5
6
7
8
9
10
unsigned S( unsigned n )
{
  unsigned result = 0;
  for each digit in n
  {
    if (n is odd) result += the digit;
    else (n is even) result += 2*(the digit);
  }
  return result;
}

Again, I don't find any reason why you cannot just convert n to a string and iterate over that to get the individual digits (remembering, as in your OP, to subtract '0' from the character representation of each digit in the string).


Next, you need to implement D.

1
2
3
4
unsigned D( unsigned n )
{
  return the last digit in S(n);
}

Again, I don't care how you get it. A simple remainder operation will do. (Hint hint).


Finally, you need to implement the algorithm to sum all the D(n) for n in [A,B].

1
2
3
4
5
6
7
8
9
10
int main()
{
  unsigned A;  cout << "A? ";  cin >> A;
  unsigned B;  cout << "B? ";  cin >> B;

  unsigned sum = 0;
  for every N in [A,B]: sum += D(N);

  cout << N << "\n";
}

Hope this helps.
I was a little confused, after some tinkering I realized that all I needed was to make sure I was summing and that total = 0 before the start of the second loop every time

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
  for (int i = start; i <= end ; i ++)
    {
        std::string number = std::to_string(i);
        length = number.length();
        total = 0;
        for(int j = 0; j < length; j++)
        {
            if (number.at(j) % 2 == 0)
            {
                tempNum = (number.at(j) -'0') * 2;
                
            }
            else
            {
                tempNum = number.at(j) - '0';
            }
            total = total + tempNum;
        }
        std::string product = std::to_string(total);
        productLength = product.length();
        list.push_back(product.at(productLength -1));
    }
    for (int i = 0; i < list.size(); i++)
    {
        sum = sum + list[i] - '0';
    }


This seems to work in all cases, thank you for the help
Topic archived. No new replies allowed.