#include <conio.h>
#include <iostream>
#include <cstring>
#include <ctype.h>

using namespace System;
using namespace System::String;

enum CardType{
	MasterCard,
        BankCard,
	Visa,
	AmericanExpress,
	Discover,
	DinersClub,
	JCB
};

bool Validate(CardType cardType, String cardNumber){
	int number[16], len=0;
	for(int i=0; i<strlen(cardNumber); ++i){
		cardNumber[i] = atoi(cardNumber[i]);
		if(isdigit(cardNumber[i])){
			if(len == 16) return false;
			number[len++] = cardNumber[i];
		}
	}

	switch(cardType){

		case CardType.MasterCard:
			if(len!=16) return false;
			if(number[0] != 5 || number[1] == 0 || number[1] > 5)
               return false;
            break;

         case CardType.BankCard:
            if(len != 16)
               return false;
            if(number[0] != 5 || number[1] != 6 || number[2] > 1)
               return false;
            break;

         case CardType.Visa:
            if(len != 16 && len != 13)
               return false;
            if(number[0] != 4)
               return false;
            break;

         case CardType.AmericanExpress:
            if(len != 15)
               return false;
            if(number[0] != 3 || (number[1] != 4 && number[1] != 7))
               return false;
            break;

         case CardType.Discover:
            if(len != 16)
               return false;
            if(number[0] != 6 || number[1] != 0 || number[2] != 1 || number[3] != 1)
               return false;
            break;

         case CardType.DinersClub:
            if(len != 14)
               return false;
            if(number[0] != 3 || (number[1] != 0 && number[1] != 6 && number[1] != 8)
               || number[1] == 0 && number[2] > 5)
               return false;
            break;

         case CardType.JCB:
            if(len != 16 && len != 15)
               return false;
            if(number[0] != 3 || number[1] != 5)
               return false;
            break;
	}

	//Using the Luhn's Algorithm
	int sum = 0;
	for(i=len-1; i>=0; ++i){
		if(i % 2 == len % 2){
			int n = number[i] * 2;
            sum += (n / 10) + (n % 10);
         }
         else
            sum += number[i];
      }
    return (sum % 10 == 0);
}

void valid(){
	cout << "The credit card number is VALID !";
}

void invalid(){
	cout << "The credit card number is INVALID !";
}

int main(){
	system("cls");
	string ccn, ct;
	cout << "Enter the Card Type :";
	cin >> ct;
	cout << "Enter the Credit card Number :";
        cin >> ccn;
	CardType card = ct;
        bool val = Validate(ct, ccn);
	if (val == true) valid();
        else invalid()
	getch;
	return 0;
}

There are a lot of errors ..... !
Please help me correct them ....

How do declare a string in Visual C++ ?
Is it not same as in C# ... like "string s;" !!!????

:confused:

Edited 5 Years Ago by Nick Evan: Chenged ICODE tags to CODE tags. ICODE is only meant for a single line of code,. For blocks, use CODE

The namespace you're looking for is called std
The header file you're looking for is called string. The header file cstring does not contain the C++ std::string type.

This stuff

using namespace System;
using namespace System::String;

looks like it came from C#. C++ is different.

Edited 5 Years Ago by Moschops: n/a

I changed it to

#include <conio.h>
#include <iostream>
#include <cstring>
#include <ctype.h>
#include <string>

using namespace System;
using namespace std::string;

But i am still getting the same error!

Edited 5 Years Ago by Nick Evan: Added CODE-tags

The namespace you're looking for is called std. It is not called System. Do not try to use the namespace System. Use the namespace std

You can use the namespace std like this:

using namespace std;

This

using namespace std::string;

is not C++ code. It's just plain wrong. There is no namespace called std::string
I suspect you meant
using std::string;

Edited 5 Years Ago by Moschops: n/a

I did as you told, now that problem is solved !
I got other issues now, where I need help !

#include <conio.h>
#include <iostream>
#include <cstring>
#include <ctype.h>
#include <string>

using namespace std;
using std::string;

enum CardType{
	MasterCard,
    BankCard,
	Visa,
	AmericanExpress,
	Discover,
	DinersClub,
	JCB
};

bool Validate(CardType cardType, string cardNumber){
	int number[16], len=0;
	[U][B]for(int i=0; i<strlen(cardNumber); ++i){[/B][/U]
		[U][B]cardNumber[i] = atoi(cardNumber[i]);[/B][/U]
		if(isdigit(cardNumber[i])){
			if(len == 16) return false;
			number[len++] = cardNumber[i];
		}
	}

	switch(cardType){

		case CardType.MasterCard:
			if(len!=16) return false;
			if(number[0] != 5 || number[1] == 0 || number[1] > 5)
               return false;
            break;

         case CardType.BankCard:
            if(len != 16)
               return false;
            if(number[0] != 5 || number[1] != 6 || number[2] > 1)
               return false;
            break;

         case CardType.Visa:
            if(len != 16 && len != 13)
               return false;
            if(number[0] != 4)
               return false;
            break;

         case CardType.AmericanExpress:
            if(len != 15)
               return false;
            if(number[0] != 3 || (number[1] != 4 && number[1] != 7))
               return false;
            break;

         case CardType.Discover:
            if(len != 16)
               return false;
            if(number[0] != 6 || number[1] != 0 || number[2] != 1 || number[3] != 1)
               return false;
            break;

         case CardType.DinersClub:
            if(len != 14)
               return false;
            if(number[0] != 3 || (number[1] != 0 && number[1] != 6 && number[1] != 8)
               || number[1] == 0 && number[2] > 5)
               return false;
            break;

         case CardType.JCB:
            if(len != 16 && len != 15)
               return false;
            if(number[0] != 3 || number[1] != 5)
               return false;
            break;
	}

	//Using the Luhn's Algorithm
	int sum = 0;
	for(i=len-1; i>=0; ++i){
		if(i % 2 == len % 2){
			int n = number[i] * 2;
            sum += (n / 10) + (n % 10);
         }
         else
            sum += number[i];
      }
    return (sum % 10 == 0);
}

void valid(){
	cout << "The credit card number is VALID !";
}

void invalid(){
	cout << "The credit card number is INVALID !";
}

int main(){
	system("cls");
	string ccn, ct;
	cout << "Enter the Card Type :";
	cin >> ct;
	cout << "Enter the Credit card Number :";
        cin >> ccn;
	CardType card = ct;
        bool val = Validate(ct, ccn);
	if (val == true) valid();
        else invalid()
	getch;
	return 0;
}

Error list :

Error	1	error C2664: 'strlen' : cannot convert parameter 1 from 'std::string' to 'const char *'	
Error	2	error C2664: 'atoi' : cannot convert parameter 1 from 'char' to 'const char *'	
Error	3	error C2226: syntax error : unexpected type 'CardType'	
Error	4	error C2226: syntax error : unexpected type 'CardType'	
Error	5	error C2226: syntax error : unexpected type 'CardType'	
Error	6	error C2226: syntax error : unexpected type 'CardType'	
Error	7	error C2226: syntax error : unexpected type 'CardType'	
Error	8	error C2226: syntax error : unexpected type 'CardType'	
Error	9	error C2226: syntax error : unexpected type 'CardType'	
Warning	10	warning C4060: switch statement contains no 'case' or 'default' labels	
Error	11	error C2065: 'i' : undeclared identifier	
Error	12	error C2065: 'i' : undeclared identifier	
Error	13	error C2065: 'i' : undeclared identifier	
Error	14	error C2065: 'i' : undeclared identifier	
Error	15	error C2065: 'i' : undeclared identifier	
Error	16	error C2065: 'i' : undeclared identifier	
Error	17	error C2440: 'initializing' : cannot convert from 'std::string' to 'CardType'	
Error	18	error C2664: 'Validate' : cannot convert parameter 1 from 'std::string' to 'CardType'

What is with the first two errors ???

Edited 5 Years Ago by Nick Evan: Changed the tags again

strlen is a function that takes a single parameter. That parameter must be a char*. You have tried to feed the function a parameter that is not a char*. You cannot simple pass whatever kinds of variables you like into a function; you can only pass in variables of the correct type.

Your second error is exactly the same; you're trying to feed a parameter of type char to the atoi function, but the atoi function does not take a char. It takes a char*.

This is C++, and you're using a proper C++ string. Look up the string class functions http://www.cplusplus.com/reference/string/string/ - there are two in there that do what you want for finding the length of a std::string.

cardNumber - this gives you a char. atoi wants a pointer to that char. You can get a pointer to any object using the reference operator & - http://www.cplusplus.com/doc/tutorial/pointers/

strlen is a function that takes a single parameter. That parameter must be a char*. You have tried to feed the function a parameter that is not a char*. You cannot simple pass whatever kinds of variables you like into a function; you can only pass in variables of the correct type.

Can't we pass a string into it ..... ???
or I should change the type of 'cardNumber' to char* ??

Can't we pass a string into it ..... ???

No. This is a really important aspect of programming in C++ (and many other languages). A function will have a set of parameters it accepts, and it will accept no others. You cannot simply decide to feed it something else. If the function expects an int, feeding it a void pointer is just not going to work. If it expects some custom class, passing it a pointer to a float just will not work. In this case, the function expects a pointer to a character, so passing it a std::string just will not work. A pointer to a char is not the same thing as a std::string.

Sometimes when you make this mistake, the compiler will be able to work out how to convert what you're passing the function into what the function expects. Sometimes not.

or I should change the type of 'cardNumber' to char* ??

No. You should use the std::string length function - http://www.cplusplus.com/reference/string/string/length/

Edited 5 Years Ago by Moschops: n/a

I did that ...
I used "cardNumber.length();"

What should I do if I wish to convert a string to an integer array ???
Is there a function for it ..... or do I have to convert it using my own function ??

How is it possible to make the program ask the user the Card type ??
I have not been able to do it with "enum" ??

Below is the final code ... without errors ?

Edited 5 Years Ago by apanimesh061: n/a

#include <conio.h>
#include <iostream>
#include <cstring>
#include <ctype.h>
#include <string>

using namespace std;
using std::string;

enum CardType{
	MasterCard,
    BankCard,
	Visa,
	AmericanExpress,
	Discover,
	DinersClub,
	JCB
};

bool Validate(CardType cardType, string cardNumber){
	int number[16], len=0, srln = cardNumber.length();
	for(int i=0; i<srln; ++i){
		
		if(isdigit(cardNumber[i])){
			if(len == 16) return false;
			number[len++] = cardNumber[i];
		}
	}

	switch(cardType){

		case MasterCard:
			if(len!=16) return false;
			if(number[0] != 5 || number[1] == 0 || number[1] > 5)
               return false;
            break;

         case BankCard:
            if(len != 16)
               return false;
            if(number[0] != 5 || number[1] != 6 || number[2] > 1)
               return false;
            break;

         case Visa:
            if(len != 16 && len != 13)
               return false;
            if(number[0] != 4)
               return false;
            break;

         case AmericanExpress:
            if(len != 15)
               return false;
            if(number[0] != 3 || (number[1] != 4 && number[1] != 7))
               return false;
            break;

         case Discover:
            if(len != 16)
               return false;
            if(number[0] != 6 || number[1] != 0 || number[2] != 1 || number[3] != 1)
               return false;
            break;

         case DinersClub:
            if(len != 14)
               return false;
            if(number[0] != 3 || (number[1] != 0 && number[1] != 6 && number[1] != 8)
               || number[1] == 0 && number[2] > 5)
               return false;
            break;

         case JCB:
            if(len != 16 && len != 15)
               return false;
            if(number[0] != 3 || number[1] != 5)
               return false;
            break;
			
		 default:
			 cout<<"\n No such type of card exists .... !";
	}

	//Using the Luhn's Algorithm
	int sum = 0;
	for(int i=len-1; i>=0; ++i){
		if(i % 2 == len % 2){
			int n = number[i] * 2;
            sum += (n / 10) + (n % 10);
         }
         else
            sum += number[i];
      }
    return (sum % 10 == 0);
}

void valid(){
	cout << "The credit card number is VALID !";
}

void invalid(){
	cout << "The credit card number is INVALID !";
}

int main(){
	system("cls");
	string ccn, ct;
	cout << "Enter the Card Type :";
	cin >> ct;
	cout << "Enter the Credit card Number :";
    cin >> ccn;
	CardType card = Visa;
    bool val = Validate(card, ccn);
	if (val == true) valid();
	else invalid();
	system("pause");
	return 0;
}

Please tell me how to make the program ask the user the card type !
I could not use enum here !!!

#include <conio.h>
#include <iostream>
#include <cstring>
#include <ctype.h>
#include <string>

using namespace std;
using std::string;

enum CardType{
	MasterCard,
    BankCard,
	Visa,
	AmericanExpress,
	Discover,
	DinersClub,
	JCB
};
bool Validate(CardType cardType, string cardNumber){
	int number[16], len=0, srln = cardNumber.length();
	for(int i=0; i<srln; ++i){
		
		if(isdigit(cardNumber[i])){
			if(len == 16) return false;
			number[len++] = cardNumber[i];
		}
	}

	switch(cardType){
        cout << "\nIn the Switch()";
		case MasterCard:
			cout << "\n\nThis is a MasterCard";
			if(len!=16){
				cout<<"I came to if1 mc !";
				return false;
			}
			if(cardNumber[0] != 5 || cardNumber[1] == 0 || cardNumber[1] > 5){
				cout<<"I came to if2 mc !";
                return false;
			}
            break;

         case BankCard:
			cout << "\n\nThis is a BankCard";
			if(len != 16){
				cout<<"I came to if1 bc !";
                return false;
			}
			if(cardNumber[0] != 5 || cardNumber[1] != 6 || cardNumber[2] > 1){
				cout<<"I came to if2 bc !";
                return false;
			}
            break;

         [B][I]case Visa:
            cout << "\n\nThis is a Visa " << cardNumber[0] << "\n\n";
			if(len != 16 && len != 13){
				cout<<"I came to if1 v !\n";
                return false;
			}
			if(cardNumber[0] != 4){
				cout<<"I came to if2 v !\n";
                return false;
			}
            break;[/I][/B]

         case AmericanExpress:
			 cout << "\n\nThis is an Amex";
			 if(len != 15){
				 cout<<"I came to if1 amex !";
                 return false;
			 }
			 if(cardNumber[0] != 3 || (cardNumber[1] != 4 && cardNumber[1] != 7)){
				cout<<"I came to if2 amex !";
                 return false;
			 }
            break;

         case Discover:
			 cout << "\n\nThis is a Discover";
			 if(len != 16){
				 cout<<"I came to if1 d !";
               return false;
			 }
			 if(cardNumber[0] != 6 || cardNumber[1] != 0 || cardNumber[2] != 1 || cardNumber[3] != 1){
				 cout<<"I came to if2 d !";
               return false;
			 }
            break;

         case DinersClub:
			 cout << "\n\nThis is a DinersClub";
			 if(len != 14){
				 cout<<"I came to if1 dc !";
               return false;
			 }
			 if(cardNumber[0] != 3 || (cardNumber[1] != 0 && cardNumber[1] != 6 && cardNumber[1] != 8) || cardNumber[1] == 0 && cardNumber[2] > 5){
					 cout<<"I came to if2 dc !";
               return false;
			 }
            break;

         case JCB:
			 cout << "\n\nThis is a JCB";
			 if(len != 16 && len != 15){
				 cout<<"I came to if1 jcb !";
               return false;
			 }
			 if(cardNumber[0] != 3 || cardNumber[1] != 5){
				 cout<<"I came to if2 jcb !";
               return false;
			 }
            break;
			
		 default:
			 cout<<"\n No such type of card exists .... !";
	}
    cout << "\nOutside of the switch() !"; 

	//Using the Luhn's Algorithm
	int sum = 0;
	for(int i=len-1; i>=0; ++i){
		if(i % 2 == len % 2){
			int n = cardNumber[i] * 2;
            sum += (n / 10) + (n % 10);
         }
		else{
            sum += cardNumber[i];
            //cout << "/n" <<sum; 
         }
        return (sum % 10 == 0);
     }
}
void valid(){
	cout << " and the credit card number is VALID !";
}

void invalid(){
	cout << " and the credit card number is INVALID !";
}
int main(){
	system("cls");
	string ccn, ct;
	cout << "Enter the Card Type :";
	cin >> ct;
	cout << "Enter the Credit card Number :";
        cin >> ccn;
	CardType card = Visa;
        bool val = Validate(card, ccn);
	if (val == true) valid();	else invalid();
	cout << "\n\n\n";
	system("pause");
	return 0;
}

This is more corrected version .... debugs without errors.
The error is in output !
This is only for Visa card !
The error part is bold n italic !

The output is :

Enter the Card Type :Visa
Enter the Credit card Number :4000080706200002


This is a Visa 4

I came to if2 v !
 and the credit card number is INVALID !


Press any key to continue . . .

The value of cardNumber[0] is 4, then why is it entering the second 'if' in the case "Visa" ?
:confused:

If you wish to convert a string into a char * you could try

string str="String of characters";
char *array=new char[str.size()];
strcpy(array,str.c_str());

The .c_str() function gets a const char* representing the string.

This article has been dead for over six months. Start a new discussion instead.