credit card validation program problem

My program runs with the wrong results

Page 1 of 1

12 Replies - 1764 Views - Last Post: 02 February 2007 - 03:06 PM Rate Topic: -----

#1 cristyjones  Icon User is offline

  • New D.I.C Head

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

credit card validation program problem

Post icon  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  Icon User is offline

  • lost in compilation
  • member icon

Reputation: 205
  • View blog
  • Posts: 4,807
  • 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?
Was This Post Helpful? 0
  • +
  • -

#3 cristyjones  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#4 cristyjones  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#5 Xing  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 19
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#6 cristyjones  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#7 ByteWyse  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 41
  • Joined: 02-January 07

Re: credit card validation program problem

Posted 01 February 2007 - 07:53 AM

View Postcristyjones, 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.
Was This Post Helpful? 0
  • +
  • -

#8 cristyjones  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#9 horacio  Icon User is offline

  • D.I.C Head

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

Re: credit card validation program problem

Posted 01 February 2007 - 01:51 PM

View Postcristyjones, 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
your function definition:
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.
Was This Post Helpful? 0
  • +
  • -

#10 cristyjones  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#11 cristyjones  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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;
		}
	}


Was This Post Helpful? 0
  • +
  • -

#12 horacio  Icon User is offline

  • D.I.C Head

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

Re: credit card validation program problem

Posted 02 February 2007 - 02:59 PM

glad to help. nice work!.
Was This Post Helpful? 0
  • +
  • -

#13 skyhawk133  Icon User is offline

  • Head DIC Head
  • member icon

Reputation: 1875
  • View blog
  • Posts: 20,284
  • Joined: 17-March 01

Re: credit card validation program problem

Posted 02 February 2007 - 03:06 PM

Thanks for helping out guys! Great job!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1