Im writing a program that reads credit card numbers and tells you whether they are valid or invalid cards. Im getting some error message and am not sure how to fix or rewrite it. Lines 34 and 43 i get error"implecite conversion loses integer percision: 'unsigned long' to int'.
#include<iostream>
#include<sstream>
#include<string>
#include<cmath>
usingnamespace std;
bool creditCardType(string cardNumber)
{
return
//For Visa cards
(cardNumber.compare(0, 1, "4") == 0) ||
//For Mastercards
(cardNumber.compare(0, 1, "5") == 0) ||
//For American Express card
(cardNumber.compare(0, 2, "37") == 0) ||
//For Discover cards
(cardNumber.compare(0, 1, "6") == 0);
}
int getDigit(int number)
{
int result =0;
while(number)
{
result = result + (number%10);
number = number/10;
}
return result;
}
// to calculate sum of numbers at even places.
int sumOfDoubleEvenPlaces(const string& cardNumber)
{
int sum_even_places = 0;
for (int i = cardNumber.length() -2; i>=0; i-=2)
{
sum_even_places = sum_even_places + getDigit(2*(cardNumber[i] - '0'));
}
return sum_even_places;
}
int sumofoddplace(const string& cardNumber)
{
int sum_odd_places = 0;
for (int i = cardNumber.length() -1; i>=0; i-=2)
{
sum_odd_places = sum_odd_places +((cardNumber[i] - '0'));
}
return sum_odd_places;
}
// function to check card is valid or not.
bool isValid(const string& cardNumber)
{
return ((sumOfDoubleEvenPlaces(cardNumber)+sumofoddplace(cardNumber))%10==0);
}
//Write a program that prompts the user to enter a credit card number as a long integer.
int main()
{
longint cardnNum;
int choice;
cout << "Please enter your credit card number: ";
cin >>cardnNum;
cout << endl;
stringstream ss;
ss << cardnNum;
string cardnumber = ss.str();
// You program will begin by asking the user what type of execution:
// 1= use asset logic on length of credit card number
// 2= print error message and continue on to next credit card number.
//Display the credit card number, length of credit card number and whether the number is valid or invalid.
cout <<"Press 1 to use asset logic on length of credit card number" << endl;
cout <<"Press 2 to use creditCardType logic on credit card number " << endl;
cin >> choice;
if(choice == 1)
{
cout << cardnumber << " is a ";
if(cardnumber[0] == '4')
cout <<" VISA";
if(cardnumber[0] == '5')
cout <<" MASTER";
if(cardnumber[0] == '6')
cout <<" DISCOVER";
if(cardnumber[0] == '3')
cout <<" AMERICAN EXPRESS";
cout << endl;
if((cardnumber.length()>=13 && cardnumber.length()<=16))
cout << cardnumber << " has a length of " <<cardnumber.length() <<" is a valid card " << endl;
else
cout << cardnumber<< " has a length of " <<cardnumber.length() <<" is an invalid card " << endl;
}
elseif(choice == 2)
{
if(creditCardType(cardnumber) && isValid(cardnumber))
{
cout << cardnumber << " is a ";
if(cardnumber[0] == '4')
cout <<" VISA";
if(cardnumber[0] == '5')
cout <<" MASTER";
if(cardnumber[0] == '6')
cout <<" DISCOVER";
if(cardnumber[0] == '3')
cout <<" AMERICAN EXPRESS";
cout <<" Is Valid card " << endl;
}
cout << cardnumber << " has invalid type so "<< cardnumber<<" is an invalid card " << endl;
}
system("pause");
return 0;
}
Are you sure it is an error and not a warning? Also the length function returns a std::size_t which is in your case a unsignedlong instead of an int. It shouldn't give you any errors but maybe a warning.