Question: This code (in c++) is used to read an 8-digit card number and tell if it is valid or not, then display the correct check

This code (in c++) is used to read an 8-digit card number and tell if it is valid or not, then display the correct check digit. However, the check digit does not come out right. Any help would be appreciated

#include using namespace std;

int main()

{ int n; while (1) { int sum1 = 0; int sum2 = 0; int sum3 = 0;; int i = 0; cout << " Enter the credit card number: "; cin >> n; if (n == -1) break; else { while (n > 0) { if (i % 2 == 0) { sum1 = sum1 + n % 10; i = i + 1; } else { sum2 = ((n % 10) * 2); while (sum2 > 0) { sum3 = sum3 + sum2 % 10; sum2 = sum2 / 10; } i = i + 1; } n = n / 10; } if ((sum1 + sum3) % 10 == 0) cout << ("Valid Credit Card Number "); else { int i; for (i = 1; i <= 9; i++) { if ((sum3 + sum1 + i) % 10 == 0) break; } cout << "Not a Valid Credit Card Number. The check digit number to be added is " << i; } } } return 0; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!