0

I have declared the following:

const int NUMBER_OF_DIGITS = 16;
std::vector<int> digits(NUMBER_OF_DIGITS);

However when I open the MSVC debugger it shows the following:

enter image description here

This is how I added values to the vector:

for (int i = 0; i < NUMBER_OF_DIGITS; ++i) {
        scanf("%d", &digits[i]);
    }

Is this normal? Can I just ignore this? Or is this a problem?

Full code(the program is still not ready yet):

#include <iostream>
#include <vector>
#include "stdio.h"

const int NUMBER_OF_DIGITS = 16;

int checksum, tmp1, tmp2, tmp3, sum = 0;

std::vector<int> digits(NUMBER_OF_DIGITS);
std::vector<int> new_digits(NUMBER_OF_DIGITS);

int luhn_checksum(std::vector<int> cardnumber[NUMBER_OF_DIGITS]) {
    //step 1: duouble every second number
    for (int i = 1; i < NUMBER_OF_DIGITS; i += 2) {
        new_digits[i] = digits[i] * 2;
        if (new_digits[i] > 9) {
            //if the product is larger than 9 we will add the two numbers together
            //example: 9 * 2 = 18 so we will add 1 + 8 to get 9
            tmp1 += new_digits[i] % 10;
            new_digits[i] /= 10;
            tmp1 = 0;
        }
    }

    //step 2: sum all the values
    for (int i = 0; i < NUMBER_OF_DIGITS; ++i) {
        checksum += new_digits[i];
    }

    return checksum;
}

void get_card_numbers(void) {
    for (int i = 0; i < NUMBER_OF_DIGITS; ++i) {
        scanf("%d", &digits[i]);
    }
}

void initialize(void) {
    for (int i = 0; i < NUMBER_OF_DIGITS; ++i) {
        digits.push_back(0);
        new_digits.push_back(0);
    }
}


int main() {
    initialize();

    get_card_numbers();

    printf("checksum is: %d\n", luhn_checksum(&digits));


    std::cout << digits.size() << std::endl;


    int x; scanf("%d", &x);
    return 0;
}
dominique120
  • 1,170
  • 4
  • 18
  • 31
  • Tell me what `cout << digits.size() << endl` prints – Mustafa Ozturk Apr 02 '15 at 20:54
  • @MustafaOzturk ```std::cout << digits.size() << std::endl;``` prints out 32 – dominique120 Apr 02 '15 at 20:58
  • Can you post full example? You're doing something to your vector (and you should most definitely be worried about the size being equal to 32) – Mustafa Ozturk Apr 02 '15 at 20:59
  • I added all the code as you asked. – dominique120 Apr 02 '15 at 21:05
  • Cleaned up your code a bit – Mustafa Ozturk Apr 02 '15 at 21:19
  • 1
    When you create your vectors with a size, so before initialize() is called your vectors have a size of 16. Then you push back 16 0's to the end of the vector, so you end up with 32 items on your vectors. – Mustafa Ozturk Apr 02 '15 at 21:28
  • I see now. When I create the vector it automatically gets initialized to 0s so if I push_back a bunch of zeroes I'm adding them instead of changing anything . Thanks! – dominique120 Apr 02 '15 at 21:33
  • It gets initialized with garbage, if you include a second argument, it will initialize it to the second argument you provide. In this case you should do the following `digits(NUMBER_OF_DIGITS, 0)` The 0 at the end indicates you want to create a vector with NUMBER_OF_DIGITS items with the value of 0. – Mustafa Ozturk Apr 02 '15 at 21:36
  • @MustafaOzturk: No, it gets initialized with `T()` which is defined as `0` for `T=int`. Not garbage. Specifying `0` is therefore redundant. – MSalters Apr 02 '15 at 21:49
  • @MSalters, you're right, however, I think explicitly initializing it to 0 better anyway. – Mustafa Ozturk Apr 03 '15 at 01:23

2 Answers2

1

The constructor you're using for digits is setting the size by specifying the count. So after calling push_back you've just added another 16 to the vector. Use a different constructor that doesn't set the count.

int _tmain(int argc, _TCHAR* argv[])
{
    const int NUMBER_OF_DIGITS = 16;

    std::vector<int> digits(NUMBER_OF_DIGITS);
    //std::vector<int> digits;

    int digitsLen = digits.size();
    // Here is 16

    for (int i = 0; i < NUMBER_OF_DIGITS; ++i) {
        digits.push_back(0);
    }

    int digitsLen2 = digits.size();
    // Here is 32

    return 0;
}
Chris O
  • 5,017
  • 3
  • 35
  • 42
0

Cleaned up your code a bit:

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

static const size_t NUMBER_OF_DIGITS = 16;

class cards {
public:
   cards();
   void read();
   int luhnChecksum() const;
private:
   vector<int> digits;
};

cards::cards() : digits(NUMBER_OF_DIGITS, 0) 
{
}

void cards::read() {
   for_each(digits.begin(), digits.end(), [](int& i) { cin >> i; });
}

int cards::luhnChecksum() const {
   vector<int> newdigits(digits.begin(), digits.end());
   for (size_t i=1; i<NUMBER_OF_DIGITS; i += 2) {
      newdigits[i] = digits[i] * 2;
      if (newdigits[i] > 9) {
     int tmp1 = newdigits[i] % 10;
     newdigits[i] /= 10;
     newdigits[i] += tmp1;
      }
   }

   return accumulate(newdigits.begin(), newdigits.end(), 0);
}

int main() {
   cards c;
   c.read();
   cout << "checksum = " << c.luhnChecksum() << endl;
   return 0;
}
Mustafa Ozturk
  • 812
  • 4
  • 19