Problem with vectors

Hi I'm trying to create a program that takes a personal and then returns if it's fake or not. In order to do this we have an algorithm(Luhn-Algorithm) that looks as follows:

Let's say our personal is: 811218-9876

First we multiply the first, third, fifth etc. number with 2. The rest of the numbers (not the last one, in this case 6) we multiply by 1. So like this:

8 * 2 = 16, 1 * 1 = 1, 1 * 2 = 2 ... etc and it ends up with the following results:

16 1 2 2 2 8 18 8 14

From these results we separate all the numbers and add them together. So in this case it looks like this:

1 + 6 + 1 + 2 + 2 + 2 + 8 + 1 + 8 + 8 + 1 + 4 = 44

Then we look for the closest tens above the number. In this case, 50.

50 - 44 = 6

The number 6 is the last number in the personal and that means the personal is not fake. If it returns something else than the last number after the calculation it is considered as a fake personal.

I have not written the whole code yet but I've gotten pretty far. However the code is making the prgram crash and I have no idea why. Anyone in here that could enlighten me please ?

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
60
61
62
63
64
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
using namespace std;

void input(vector<int> & v);
void calc(vector<int> & v, vector<int> & amount);
void output(vector<int> & v, vector<int> & amount);

int main()
{
  vector<int> pnumber;
  vector<int> amount;

  input(pnumber);
  calc(pnumber, amount);
  output(pnumber, amount);

  return 0;
}

void input(vector<int> & v)
{
    string str, str2="";

    cout << "Enter your personal: ";
    getline(cin, str);

    for(int i=0; i < str.length(); i++)
    {
        str2=str[i];
        stringstream ss(str2);
        int ret = 0;
        if(ss >> ret)
        {
            v.push_back(ret);
        }
    }
}

void calc(vector<int> & v, vector<int> & amount)
{
    for(int i=0; i < v.size(); i++)
    {
        if(i % 2 == 0)
        {
           amount[i] = i * 1;
        }
        else
        {
            amount[i] = i * 2;
        }
    }
}

void output(vector<int> & v, vector<int> & amount)
{
    for(int i = 0; i < amount.size(); i++)
    {
        cout << amount.at(i) << " ";
    }
}
vector<int> amount; //amount is empty vector, it does not have any elements  
calc(pnumber, amount); //amount is still empty  
amount[i] = i * 1; //Trying to access nonexistent element
Last edited on
Hmm.. Would you mind giving me another hint or an explenation? I'm not really that familiar with vectors yet.
1
2
3
4
5
6
7
8
9
vector<int> x(5); //Vector containing 5 elements
//Valid indices are 0-4
x[0] = 1; //Valid
x[4] = 10;//Valid
x[5] = 0; //Invalid, there is no such element, can lead to crash, incorrect work or something else

vector<int> y; //Vector containing 0 elements
//There is no valid indices
y[0] = 0; //Invalid. There is no such element, Can lead to crash 

Either resize your vector before starting or use push_back()
http://www.mochima.com/tutorials/vectors.html
Thanks, that website was really good and vectors make a whole lot more sense to me now. The program is no longer crashing, it's not giving back the values I want though. I thought I would use push_back() to give the value sum for each time the for loop goes into the amount vector but when I try to print out the values later it gives me strange values.

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
void calc(vector<int> & v, vector<int> & amount)
{
    int sum;

    for(vector<int>::size_type i = 0; i < v.size(); i++)
    {
        if(i % 2 == 0)
        {
           sum = sum + (i * 2);
           amount.push_back(sum);
        }
        else
        {
            sum = sum + (i * 1);
            amount.push_back(sum);
        }
    }
}

void output(vector<int> & v, vector<int> & amount)
{

    for(vector<int>::size_type i = 0; i < amount.size(); i++)
    {
        cout << amount.at(i) << " ";
    }
}


I highlighted the things I changed. The result I get from this code from the 811218-9876 input is the following:

1
2
Enter your personal: 811218-9876
2686600 2686601 2686605 2686608 2686616 2686621 2686633 2686640 2686656 2686665


What am I doing wrong here?
1) Your sum is uninitialised. Theoretically it is Undefined Behavior and anything could happens, in practice usually sum just contains some junk.
2) You never read anything from your v vector. You just operate on indexes.
3) You do not reset your sum between calls, so it continues to accumulate.

More corect version:
1
2
3
4
5
6
7
void calc(const std::vector<int> & v, std::vector<int> & amount)
{
    for(std::vector<int>::size_type i = 0; i < v.size(); i++)  {
        int value = v[i] * (i % 2 == 0? 2 : 1);
         amount.push_back(value);
    }
}
Thank you sir, got confused by all the thinking. The program is working as intended now!
Topic archived. No new replies allowed.