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) << " ";
}
}
|