i need an expert in c++

profileosama001100
161-Assign06.pdf

Western Oregon University Page 1 of 3

CS-161 Assignment #6

General Submit this lab using the Moodle system by the beginning of lab on the due date.

 Each code file should have the standard comment at the top (see Lab1).

 Read information into all programs in the specified order. Do not ask for extra input. If a program says something like “ask the user for their name and the number of hours they worked” you MUST read in the name first, then the number of hours and not prompt for any other input.

 Readability and maintainability of your code counts. Poor formatting, confusing variable names, unnecessarily complex code, etc… will all result in deductions to your score.

Concepts Upon completion of this lab the student will be able to write functions to break up a program into reusable blocks.

Background The last digit of a credit card number is always chosen to provide a checksum of the other digits. (The scheme used is called the Luhn algorithm: http://en.wikipedia.org/wiki/Luhn_algorithm). If one digit in a credit card is changed, or they are typed in the wrong order, the checksum should detect the data entry mistake. To check a credit card number, you need to:

 Starting from the RIGHT hand side of the number, add up all the digits, BUT for every second digit, do the following to the digit

o Double the number (i.e. 4  8, 6  12) o If the number ends up being two digits, add the two digits (i.e. 12  1 + 2  3) and use

that as the number

 If the total is evenly divisible by 10, the number is valid. If not, the number is invalid. Samples: Given the number "79927398713", we would start on the right with the 3 - it would not be doubled, but the 1 would be. Then 7 is not doubled, but 8 is…

Digit 7 9 9 2 7 3 9 8 7 1 3

Doubled 18 4 6 16 2

Two Digits  One

9 7 Sum

Digits added 7 9 9 4 7 6 9 7 7 2 3 70

Since 70 is divisibly by 10, it is possibly a valid credit card number. Given the number "12345671234567", we would start from the 7 on the right, it is not doubled, but the 6 is. Then 5 is not doubled, but 4 is…

Digit 1 2 3 4 5 6 7 1 2 3 4 5 6 7

Doubled 2 6 10 14 4 8 12

Two Digits  One

1 5 3 Sum

Digits added 2 2 6 4 1 6 5 1 4 3 8 5 3 7 57

Since 57 is not divisibly by 10, it is NOT a possibly valid credit card number. Also, you can use identify the issuer of a credit card with the beginning digit(s):

 Starts with 4 : Visa

 Starts with 5 : MasterCard

 Starts with 34 or 37 : American Express

Western Oregon University Page 2 of 3

CS-161 Assignment #6

Problem : Credit Card Checker Filename : assign6.cpp (This is the file name you MUST use for the file you submit for this problem.) Write a program that reads in a string representing a credit card number. It should then print out if the number is valid and what kind of card it is. Sample runs: Run 1:

Enter card number: 79927398713 Valid number, unknown issuer Run 2:

Enter card number: 43214321 Not a valid number, Visa Required Functions: You MUST create and use the following functions in your code:

 string getInput(const string& prompt); Takes in a string as a parameter - prints that to the console, then gets a string of input from the console and returns the input.

 int charToInt(char digit); Turns a char like '3' into the number it represents (3). Behavior is undefined for chars that do not representing digits.

 int doubledDigitValue(int number); Processes a number to be doubled according to the rule for a doubled digit in the Luhn algorithm. Double the number and either return this value if the number is still single digits, or return the sum of the digits if it is a double digit number. Calling doubledDigitValue(4) should give 8. Calling doubledDigitValue(8) should give 7.

 int sumOfDigits(const string& cardNumber); Sums the digits of a credit card number according to the Luhn algorithm. i.e. Calling sumOfDigits("79927398713") should result in 70. (This should make use of charToInt and doubledDigitValue.) Hint: the algorithm is supposed to work from right to left. Start by just printing out the string one char at a time in reverse order. Then worry about actually adding up their values. Don't forget that every second one gets doubled.

 bool isValid(const string& cardNumber); Returns true/false indicating if a credit card number is potentially a valid number according to Luhn algorithm. (This should use a call to sumOfDigits!)

 string getCardType(const string& cardNumber); Returns a string representing the type of credit card a number is: Visa, MasterCard, American Express, or Unknown.

Each function (other than main) MUST be preceded by a doxygen style comment consisting of at least: @brief @param for each parameter @return (unless void)

A program that works but does not use functions will receive 0 pts. Feel free to add more functions, but you must at least create and use the listed ones.

Western Oregon University Page 3 of 3

CS-161 Assignment #6

Strategy: You can and should build functions one by one and test them in main. For example, to test getInput you might write in your main function: string card = getInput("Enter card number:"); cout << card << endl; //should be whatever you enter Once you are sure that function is working correctly, you can delete (or comment out) those tests and start working on the next function – testing it as you go. Once they are all written, you can write the "real" main function… it should only take a few lines of code to express. Here is a diagram of how the functions should relate to each other. It shows that main should at some point call isValid and give it a string like "43214368". isValid will do some work with that string and eventually return true or false. As part of doing its work, isValid should call sumOfDigits and pass it the string representing the card number. sumOfDigits will return an int like 45 based on doing the

Optional challenge: Break out all your functions other than getInput into a file "CreditFunctions.cpp". Declare them all in a file "CreditFunctions.h" that is included from assign6.cpp. assign6.cpp should now just have the getInput and main functions. Submit all three files.

Do NOT worry about solving the "problem" until all your functions are working. You can and should make use of functions to build other functions! Sample parameter/return values are just SAMPLES – do not try to hard code in these values!!!