OUTPUT IS NOT MATCHING. THIS LINES BELOW ARE REPEATING: \"Cardholder: John Card
ID: 3787130 • Letter: O
Question
OUTPUT IS NOT MATCHING.
THIS LINES BELOW ARE REPEATING:
"Cardholder: John
Card Number: 4000111122223333
Institution: 301
Expires: 12/2020
Number at the back: 15 "
PLEASE FIX MY CODE DO GET THE EXACT OUTPUT:
Credit Card app
===================
Please enter your name: John
Please enter your credit card number, institution code, expiry date, and security number as follows: 4000123412341234 999 12/1234 999
> 1000111122223333 17 17/1972 15
Invalid input
Please enter your credit card number, institution code, expiry date, and security number as follows: 4000123412341234 999 12/1234 999
> 4000111122223333 17 17/1972 15
Invalid input
Please enter your credit card number, institution code, expiry date, and security number as follows: 4000123412341234 999 12/1234 999
> 4000111122223333 301 17/1972 15
Invalid input
Please enter your credit card number, institution code, expiry date, and security number as follows: 4000123412341234 999 12/1234 999
> 4000111122223333 301 12/1972 15
Invalid input
Please enter your credit card number, institution code, expiry date, and security number as follows: 4000123412341234 999 12/1234 999
> 4000111122223333 301 12/2020 15
Cardholder: John
Card Number: 4000111122223333
Institution: 301
Expires: 12/2020
Number at the back: 15
Thank you!
CODE:
a) CreditCard.cpp :
#include "CreditCard.h"
#include <cstring>
#include <iostream>
namespace sict {
using namespace std;
CreditCard *cc = new CreditCard;
void CreditCard::initialize(unsigned long long creditCardNumber, int instCode,
int expiryYear, int expiryMonth,
int numberInTheBack) {
m_cardNumber = creditCardNumber;
m_institutionCode = instCode;
m_expiryYear = expiryYear;
m_expiryMonth = expiryMonth;
m_numberInTheBack = numberInTheBack;
m_numberInTheBack = numberInTheBack;
}
void CreditCard::name(const char cardHolderName[]) {
strcpy(m_cardHolderName, cardHolderName);
}
void CreditCard::write() {
cout << "Cardholder:" << m_cardHolderName << endl;
cout << "Card Number:" << m_cardNumber << endl;
cout << "Institution:" << m_institutionCode << endl;
cout << "Expires:" << m_expiryMonth << "/" << m_expiryYear << endl;
cout << "Number at the back:" << m_numberInTheBack << endl;
}
bool CreditCard::isValid() {
bool nameCheck = false, numCheck = false, instCheck = false,
yearCheck = false, monthCheck = false, backNumCheck = false,
finalCheck = false;
if (strlen(m_cardHolderName)) {
nameCheck = true;
}
if (m_cardNumber <= MAX_CARD_NUMBER && m_cardNumber >= MIN_CARD_NUMBER) {
numCheck = true;
}
if (m_institutionCode <= MAX_INST_NUMBER &&
m_institutionCode >= MIN_INST_NUMBER) {
instCheck = true;
}
if (m_expiryYear <= MAX_EXP_YEAR && m_expiryYear >= MIN_EXP_YEAR) {
yearCheck = true;
}
if (m_expiryMonth <= 12 && m_expiryMonth >= 1) {
monthCheck = true;
}
if (m_numberInTheBack % 100 < 100) {
backNumCheck = true;
}
if (nameCheck && numCheck && instCheck && yearCheck && monthCheck &&
backNumCheck) {
finalCheck = true;
} else {
finalCheck = false;
}
return finalCheck;
}
}
b) CreditCard.h :
#include <iostream>
#ifndef CREDITCARD_H
#define CREDITCARD_H
#define MAX_NAME_LENGTH 40
#define MIN_INST_NUMBER 100
#define MAX_INST_NUMBER 999
#define MIN_EXP_YEAR 2017
#define MAX_EXP_YEAR 2037
#define MIN_CARD_NUMBER 4000000000000000
#define MAX_CARD_NUMBER 4000999999999999
namespace sict {
class CreditCard {
public:
void initialize(unsigned long long creditCardNumber, int instCode,
int expiryYear, int expiryMonth, int numberInTheBack);
void name(const char cardHolderName[]);
void write();
bool isValid();
private:
char m_cardHolderName[MAX_NAME_LENGTH];
unsigned long long m_cardNumber;
int m_institutionCode;
int m_expiryYear;
int m_expiryMonth;
int m_numberInTheBack;
};
}
#endif
c) main.cpp :
#include <iostream>
using namespace std;
#include "CreditCard.h"
using namespace sict;
int main() {
CreditCard myCC;
char name[41];
int instCode;
int expiryYear;
int expiryMonth;
unsigned long long cardNumber;
int backNumber;
char slash;
cout << "Credit Card app" << endl << "===================" << endl << endl;
cout << "Please enter your name: ";
cin >> name;
do {
cout << "Please enter your credit card number, institution code, "
<< "expiry date, and security number as follows: "
<< "4000123412341234 999 12/1234 999" << endl
<< "> ";
cin >> cardNumber >> instCode >> expiryMonth >> slash >> expiryYear >>
backNumber;
cout << endl;
myCC.name(name);
myCC.initialize(cardNumber, instCode, expiryYear, expiryMonth,
backNumber);
if(!myCC.isValid()){
myCC.write();
cout << "Invalid input" << endl;
} else if(myCC.isValid()) {
myCC.write();
}
} while (!myCC.isValid());
cout << endl << "Thank you!" << endl;
return 0;
}
Explanation / Answer
Hi,
In the main.cpp you should use if and else if statement in place of do while loop. Kindly find below updated main.cpp code :
main.cpp :
#include <iostream>
using namespace std;
#include "CreditCard.h"
using namespace sict;
int main() {
CreditCard myCC;
char name[41];
int instCode;
int expiryYear;
int expiryMonth;
unsigned long long cardNumber;
int backNumber;
char slash;
cout << "Credit Card app" << endl << "===================" << endl << endl;
cout << "Please enter your name: ";
cin >> name;
cout << "Please enter your credit card number, institution code, "
<< "expiry date, and security number as follows: "
<< "4000123412341234 999 12/1234 999" << endl
<< "> ";
cin >> cardNumber >> instCode >> expiryMonth >> slash >> expiryYear >>
backNumber;
cout << endl;
myCC.name(name);
myCC.initialize(cardNumber, instCode, expiryYear, expiryMonth,
backNumber);
if(!myCC.isValid())
{
while(!myCC.isValid())
{
cout << "Invalid input" << endl;
cin >> cardNumber >> instCode >> expiryMonth >> slash >> expiryYear >>
backNumber;
cout << endl;
myCC.name(name);
myCC.initialize(cardNumber, instCode, expiryYear, expiryMonth,
backNumber);
}
}
else if(myCC.isValid()) {
myCC.write();
}
cout << endl << "Thank you!" << endl;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.