How to get a prefix for a credit card number

What I am trying to do is get a credit card number from the user, reverse it, and use the first and second digit to get a prefix depending on how much numbers I want for the prefix. I currently have the no_of_digits_in_prefix as 2. So for this case, what I need is the first two digits of the credit card number not the reverse one. For example, if the user puts in 12345, I wantit to return 12. I am stuck. Here is what I have. I have three files. I have a creditcard.cpp, creditcards.cpp, and creditcards.h.
creditcard.cpp has:

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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <iostream>
#include <string>
#include "CreditCards.h"

using namespace std;
using namespace credit_cards;

int main()
{

 cout << "Please enter credit card #: ";
		long long credit_card_no = GetCreditCardNumber();

long long reverse_credit_card_no = 0;
		while (credit_card_no > 0) {
			reverse_credit_card_no = reverse_credit_card_no * 10 + credit_card_no % 10;
			credit_card_no = credit_card_no / 10;
		}
                int no_of_digits_in_prefix = 0;
		cout << "Prefix is: " << GetPrefix(reverse_credit_card_no, no_of_digits_in_prefix) << endl;
return 0;
}


Creditcards.h (header file):

#ifndef CREDITCARDS_H
#define CREDITCARDS_H

namespace credit_cards {

//After user is prompted, this will
	//allow user to enter in credit card number
	//and whatever user enters will be
	//returned.
	long long GetCreditCardNumber();

int GetPrefix(long long, int&);
}

creditcards.cpp (implementation file):
#include <iostream>
#include <string>

using namespace std;
namespace credit_cards {

const int kToGetRemainder = 10;
const int kToGetNewCreditCardNo = 10;
int GetPrefix(long long reverse_credit_card_no, int &no_of_digits_in_prefix) 
		{
		
	
		
		no_of_digits_in_prefix = 2;
		switch (no_of_digits_in_prefix) {
			case 1:
			int digit_1 = 0;
			digit_1 = reverse_credit_card_no % kToGetRemainder;
			reverse_credit_card_no = reverse_credit_card_no / kToGetNewCreditCardNo;
			return digit_1;
			break;
			case 2:
			int digit_1 = 0;
			digit_1 = reverse_credit_card_no % kToGetRemainder;
			reverse_credit_card_no = reverse_credit_card_no / kToGetNewCreditCardNo;
			int digit_2 = 0;
			digit_2 = reverse_credit_card_no % kToGetRemainder;
			reverse_credit_card_no = reverse_credit_card_no / kToGetNewCreditCardNo;
		
			return digit_1, digit_2;
			break;
			default:
			cout << "Invalid sorry pal." << endl;
			
		} 
}
Last edited on
just fetch the first two digits before reversing the code by dividing with an appropriate power of 10.....
Topic archived. No new replies allowed.