# credit card validation program problem

Page 1 of 1

## 12 Replies - 2090 Views - Last Post: 02 February 2007 - 03:06 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=23428&amp;s=34acb6f8b2e82ff63371eb7c66acd278&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 cristyjones

Reputation: 0
• Posts: 10
• Joined: 31-January 07

# credit card validation program problem

Posted 31 January 2007 - 08:19 PM

Can anyone help me learn where I have gone wrong?

```#include <iostream>
using namespace std;
void main()
{
long x;
cout<<"Please enter a credit card number: ";
cin>>x;
cout<<"You entered the number "<<x<<endl;
long num[17];
int i;
for (i=0; i<x; i++)
{
num [i];
}
cout<<num[i]<<endl;
long valid (num[i]);
{
int total;
int odd;
int even;
even = (num[1] + num[3]+ num[5] + num[7] + num[9] + num[11] + num[13] + num[15]);
odd = (num[0] + num[2] + num[4] + num[6] + num[8] + num[10] + num[12] + num[14]);
odd = odd*2;
if (num[0]||num[2]||num[4]||num[6]||num[8]||num[10]||num[12]||num[14]>4)
{
odd=odd++;
}
total = odd + even;
total = total + num[16];
if (total%10 == 0)
{
cout<<"This is a valid credit card :) "<<endl;
}
else
{
cout<<"This is NOT a valid credit card! "<<endl;
}
}
valid;

}

```

Sorry for the really bad paste job!

Thanks,
Cristy

Is This A Good Question/Topic? 0

## Replies To: credit card validation program problem

### #2 William_Wilson

• lost in compilation

Reputation: 207
• Posts: 4,812
• Joined: 23-December 05

## Re: credit card validation program problem

Posted 31 January 2007 - 08:33 PM

what are the expected results, and what is the problem?

### #3 cristyjones

Reputation: 0
• Posts: 10
• Joined: 31-January 07

## Re: credit card validation program problem

Posted 31 January 2007 - 08:51 PM

a. Add the digits in positions 1, 3, 5, 7, 9, 11, 13, 15 and double the results.

b. count the number of digits in those same positions that are greater than 4 and add this count to the results obtained in a.

c. add the remaining digits (even numbered) except for the check digit (16) and add it to the number.

d. the check digit is then set to be the digit that when added to the result in c, the final result ends in zero.

So, my instructions were as follows:

Write a program that will input a credit card number and display if it is a valid credit card number or not.

Thank you

### #4 cristyjones

Reputation: 0
• Posts: 10
• Joined: 31-January 07

## Re: credit card validation program problem

Posted 31 January 2007 - 08:58 PM

Forgot the problem. Sorry. When I input what I know to be a valid number, I get:

Please enter a credit card number: //I typed in 3125600196430012

After I typed in the number, I got this:

You entered the number -858993460
-858993460
This is NOT a valid credit card!
Press ant key to continue...

Thanks

### #5 Xing

Reputation: 19
• Posts: 725
• Joined: 22-July 06

## Re: credit card validation program problem

Posted 31 January 2007 - 09:15 PM

The number you are entering is much larger than what can be stored in a long integer.

### #6 cristyjones

Reputation: 0
• Posts: 10
• Joined: 31-January 07

## Re: credit card validation program problem

Posted 31 January 2007 - 09:19 PM

I thought int was for shorter numbers, long was for longer numbers. The number entered will not be over 16 digits.

### #7 ByteWyse

Reputation: 8
• Posts: 41
• Joined: 02-January 07

## Re: credit card validation program problem

Posted 01 February 2007 - 07:53 AM

cristyjones, on 31 Jan, 2007 - 09:19 PM, said:

I thought int was for shorter numbers, long was for longer numbers. The number entered will not be over 16 digits.

Assuming that you're using a 32-bit processor, integers and long integers are probably also 32-bits. In that case, signed integers range from - -2147483648 to 2147483647. Unsigned integers range from 0 to 4,294,967,295.

If your system supports 64-bit integers, you can then range from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.

You can verify this for your specific system by looking in the header file limits.h and stdint.h.

Were I to implement such a system myself, I would probably read the credit card number in as a string. I would then process each character of the string sequentially to fill a 16-element array of either type "unsigned int" or perhaps event "unsigned char". I could then easily extract specific elements for additional processing as necessary.

### #8 cristyjones

Reputation: 0
• Posts: 10
• Joined: 31-January 07

## Re: credit card validation program problem

Posted 01 February 2007 - 12:24 PM

Thank you. I think I'll try using the char or string type. Then convert to int to be able to do the math portion.

### #9 horacio

Reputation: 0
• Posts: 67
• Joined: 29-January 07

## Re: credit card validation program problem

Posted 01 February 2007 - 01:51 PM

cristyjones, on 1 Feb, 2007 - 12:24 PM, said:

Thank you. I think I'll try using the char or string type. Then convert to int to be able to do the math portion.

hi, i have a cuestion regarding your problem:

i see this code segment:
```long num[17];
int i;
for (i=0; i<x; i++)
{
num [i];
}
cout<<num[i]<<endl;

```

you havent asing anything to num[i], thus, it is still uninitialized, it contains trash, ie, anything that lies in that part of the heap, it could be anything.
I am not really shure what you where trying to do there, you did not really modified the value of num[i].
#2
main always returns an integer to the os.
#3
```long valid (num[i]);
{
int total;
int odd;
int even;
even = (num[1] + num[3]+ num[5] + num[7] + num[9] + num[11] + num[13] + num[15]);
odd = (num[0] + num[2] + num[4] + num[6] + num[8] + num[10] + num[12] + num[14]);
odd = odd*2;
if (num[0]||num[2]||num[4]||num[6]||num[8]||num[10]||num[12]||num[14]>4)
{
odd=odd++;
}
total = odd + even;
total = total + num[16];
if (total%10 == 0)
{
cout<<"This is a valid credit card :) "<<endl;
}
else
{
cout<<"This is NOT a valid credit card! "<<endl;
}
}

```

functions inside functions are allowed in c++, theu arent caught by the compiler as a mistake,however the do not work as you would expect them(main() simply goes right in to them as he would with any other block. if your idea was to get a function that could only be called from main, then you should have declared in main.
like this
```int main()
{
void f(void);
.
.
}

void f(void)
{

}

```

t#4
num[i], as you declared it, is only a long, it is not really necesary to write num[i] as a parameter.
#5
function calls without right parameteres will work, they might not call the desired function but they will work.

### #10 cristyjones

Reputation: 0
• Posts: 10
• Joined: 31-January 07

## Re: credit card validation program problem

Posted 01 February 2007 - 05:33 PM

Thank you very much. I'll adjust accordingly.

### #11 cristyjones

Reputation: 0
• Posts: 10
• Joined: 31-January 07

## Re: credit card validation program problem

Posted 01 February 2007 - 09:36 PM

I figured it out finally. Thanks for everyone's help. Here is my finished code.

```#include<string>
#include <iostream>
#include <sstream>
using namespace std;
void main()
{

string x;
cout<<"Please enter a credit card number: ";
cin>>x;
cout<<"The credit card number you entered is: "<<x<<endl;

int i;
istringstream ss( x );
ss >> i;

int y=5;
int total;
int odd = (x[0]-48)+ (x[2]-48)+ (x[4]-48)+ (x[6]-48)+ (x[8]-48)+ (x[10]-48)+ (x[12]-48)+ (x[14]-48);
odd=odd*2;
cout<<odd<<endl;
if(x[0]>=y)	{odd = odd+1;}
if((x[2]-48)>=y)	{odd = odd+1;}
if((x[4]-48)>=y)	{odd = odd+1;}
if((x[6]-48)>=y)	{odd = odd+1;}
if((x[8]-48)>=y)	{odd = odd+1;}
if((x[10]-48)>=y)	{odd = odd+1;}
if((x[12]-48)>=y)	{odd = odd+1;}
if((x[14]-48)>=y)	{odd = odd+1;}
cout<<odd<<endl;
int even = (x[1]-48)+(x[3]-48)+(x[5]-48)+(x[7]-48)+(x[9]-48)+(x[11]-48)+(x[13]-48)+(x[15]-48);
cout<<even<<endl;
total = (odd + even)-1;
cout<<total<<endl;
if (total%10 == 0)
{
cout<<"This is a valid credit card :) "<<endl;
}
else
{
cout<<"This is NOT a valid credit card! "<<endl;
}
}

```

### #12 horacio

Reputation: 0
• Posts: 67
• Joined: 29-January 07

## Re: credit card validation program problem

Posted 02 February 2007 - 02:59 PM

### #13 skyhawk133

Reputation: 1953
• Posts: 20,377
• Joined: 17-March 01

## Re: credit card validation program problem

Posted 02 February 2007 - 03:06 PM

Thanks for helping out guys! Great job!