Functions for getting prefix

Mar 28, 2015 at 9:29pm

I have this right now but I am not sure what to put for return for case 2. I want it to return the first two digits of the number not reversed. For example if the creditcard number is 1234567. the reverse would be 7654321, and I want 12 to be returned. I want it to work with any amount of digits as well. So if it was 65643 it will return 65 and 747 to return 74. I tried using % but it wouldnt work with every number.

int GetPrefix(long long reverse_credit_card_no, int &no_of_digits_in_prefix, long long credit_card_no_1)
{
cout << credit_card_no_1 << endl;
cout << reverse_credit_card_no << endl;
switch (no_of_digits_in_prefix) {
case 1:
return reverse_credit_card_no % 10;
break;
case 2:
return
break;
default:
cout << "Invalid sorry pal." << endl;
return -1;
}



}
Mar 28, 2015 at 9:38pm
Okay this is kinda weird to me. What you send into that function, is the reversed number of whatever the user typed in. And then you want to return the first 2 numbers, which are now the last 2 numbers backwards. Is that correct?
Mar 28, 2015 at 10:13pm
I reversed the number so that when I % it by 10, I will get the first number. If I used the normal number, it will give me the last digit. But for case 2, I want the first two digits of the normal number not reversed, and I am not sure how to do that.
Mar 28, 2015 at 10:14pm
If reverse_credit_card_no is the reversed number, and you want the normal number in case 2. Then you will have to reverse it back inside of case 2 first.
Mar 28, 2015 at 10:51pm
It's still not working. I just put the cout << reverse_credit_card_no_back to see if it worked, but it didnt. Any ideas how to make it work for any amount of digits?

int GetPrefix(long long reverse_credit_card_no, int &no_of_digits_in_prefix, long long reverse_credit_card_no_back)
{
cout << reverse_credit_card_no << endl;
switch (no_of_digits_in_prefix) {
case 1:
return reverse_credit_card_no % 10;
break;
case 2:
while (reverse_credit_card_no > 0) {
reverse_credit_card_no_back = reverse_credit_card_no_back * 10 + reverse_credit_card_no % 10;
reverse_credit_card_no = reverse_credit_card_no / 10;

}
cout << reverse_credit_card_no_back << endl;
return 1;
break;
default:
cout << "Invalid sorry pal." << endl;
return -1;
}
Mar 28, 2015 at 10:55pm
Wouldnt it be easier if instead of using integer for the credit card, to use string instead? Ive done a project before with this and I used string, Is there any reason you cant? Its much easier to reverse a string, its much easier to get whatever prefix you want aswell.

Edit: Yep. If you're not doing any actual calculations with the credit card number, using string is the right thing to do.
Last edited on Mar 28, 2015 at 10:58pm
Mar 28, 2015 at 11:02pm
I wishhh, but my professor gave us specific functions we have to use, and for this one we need to use: Int GetPrefix(long long, int). I already messed it up a bit by adding another long long, but I was really stuck and didnt know what to do.
These are the specific directions:

Write a program that prompts the user to enter a credit card number as a long
long integer. Display whether the number is valid or invalid and the type of
card that was entered (Visa, Discover, etc…). Design your program to use the
following methods. In order to receive credit for the assignment you must
implement and use all the methods. All methods should be declared in a header
file with implementations in a separate cpp file.

Int GetPrefix(long long, int) // Return the first (n) number of digits
from the given long. If the number of digits in the number is less than
the int, then return the number. Ex – long long 4567 int 2 returns 45.
Given long long 4567 int 7 returns 4567.

bool IsPrefixMatched(long long, int) // Returns true if the given int
value is the prefix for the given long long ex – long long 4567 int 4
then return true.
Mar 28, 2015 at 11:03pm
I agree with TarikNeaj. Why not use string?

If you insist on using numbers, why not just do as programmer007 suggested in your other thread and divide by the appropriate power of 10?

1
2
numDigitsOfCC = floor(log10(CCNumber));
prefix = CCNumber / (pow(10, numDigitsOfCC - numDigitsOfPrefix + 1);


EDIT: even simpler, not requiring cmath library.

1
2
3
4
5
6
long long prefix = CCNumber;
long long limit = 1;
for (int i = 0; i < numDigitsOfPrefix; ++i)
    limit *= 10;
while (prefix >= limit)
    prefix /= 10;
Last edited on Mar 28, 2015 at 11:17pm
Mar 28, 2015 at 11:05pm
Your professor is dumb. But hey, thats just my opinion.

Try what @fg109 suggested I guess...

Edit: My professor is also dumb so, it might be a common thing in universities.
Last edited on Mar 28, 2015 at 11:09pm
Mar 29, 2015 at 12:01am
I dont think my professor is dumb but maybe just loves to challenge us. Haha i hate being challenged so maybe programming is just not my thing.
But thank you!
Topic archived. No new replies allowed.