I have been assigned a variation of the infamous ATM C++ program and am having t
ID: 3596993 • Letter: I
Question
I have been assigned a variation of the infamous ATM C++ program and am having trouble formatting, I have been given a framework code which I will post. Here are the instructions: ATM App PROGRAM 1. Accepting a deposit. a. Well formatted instructions displayed to the customer on how to make a deposit. b. The customer is expected to enter dollars and cents separated by a decimal point (e.g. “1000.45”, “995.21”) c. The deposit MUST be read as a string data type. 2. Validate the deposit a. The only valid format is $$$$.$$ (e.g. “1000.00”, “23.45”, “100.61”) b. Valid characters i. Zero (0) through nine (9) ii. Decimal point (“.”) iii. All other characters are invalid. c. One (1) and only one (1) decimal point. (e.g. “1..23” is invalid) d. Only two (2) numbers (i.e. cents) follow the decimal point (e.g. “0.21”). 3. Status flags. The use of status and/or validation flags is necessary to complete this program. The following are the validation conditions and values. a. VALID_DEPOSIT = 0; b. INVALID_TOO_MANY_DECIMAL_PLACES = 1; c. INVALID_NO_DECIMAL_PLACES = 2; d. INVALID_CHARACTER_ENTERED = 3; 4. Must display appropriate message for successfully validated deposits and for all invalid deposits. TEST DATA The following are examples of test data. The submitted program, at a minimum, must successfully process these inputs. 1. 9999.99 2. 0.00 3. 998.99 4. 145,01 (a comma instead of a decimal point) 5. 1.456 6. 3..56 (two or more decimal points) 7. 1.01 8. A5.98 And here is the framework: #include #include using namespace std; int main() { string deposit; bool decimalPlace = false; // Flag set based on decimal point found or not int howManyDecimalPoints = 0; // Must count number of decimal points int howManyDecimalPlaces = 0; // Must count number of decimal places (i.e. how many places to the right) bool validNumberEntered = true; // Flag to indicate validation status of the user input const int VALID_DEPOSIT = 0; // user input passes all validation checks const int INVALID_TOO_MANY_DECIMAL_PLACES = 1; const int INVALID_NO_DECIMAL_PLACES = 2; const int INVALID_CHARACTER_ENTERED = 3; const int INVALID_TOO_LARGE_OF_A_NUMBER = 4; // Set default int validationResults = VALID_DEPOSIT; // Assume valid deposit entered cout << "The maximum deposit accepeted is $9999.99. "; cout << "Please enter your deposit: "; getline(cin, deposit); // Get deposit entered int howBig = size(deposit); cout << "Your deposit entered: " << deposit << endl; // Parse string for validation. Remember we start with 0 for (int i = 0; i < howBig; i++) { if (deposit[i] <= '7') { cout << "Your deposit entered is a valid entry" << endl; } // get the user input //char temp = deposit[i]; // Display current inputted char (temporary code to demonstrate how indexing works) // Delete when understood //cout << "This is the current character " << temp << endl; // The following is the pseudo code for validation // For all the characters entered // If number (0-9) entered? // If decimal place found already? count number of decimal places // else // If Decimal point? If true, set flag and count // else all other input is invalid // End for loop } // More validation pseudo code // Was one and only one decimal point found? Else invalid // Display appropriate message for each case // (zero, 2 or more than 2 decimal places) // Also set appropriate flag each specific case // Did input meet all validation criteria? // True - display appropriate messsage // False - display appropriate message // Gracefully exit cout << "Transaction complete. Press [ENTER] to quit." << endl; cin.get(); return(0); }
Explanation / Answer
#include<iostream>
#include<string>
#include<string.h>
#include<stdlib.h>
using namespace std;
int main(){
string deposit;
const int VALID_DEPOSIT = 0;
const int INVALID_NO_DECIMAL_PLACES = 2;
const int INVALID_TOO_MANY_DECIMAL_PLACES = 1;
const int INVALID_TOO_LARGE_OF_A_NUMBER = 4;
const int INVALID_CHARACTER_ENTERED = 3;
int validationResults = VALID_DEPOSIT;
double amt;
cout << "Enter deposit amount : $";
getline(cin,deposit);
int count = 0;
for (int i = 0; i<deposit.size(); i++){
if (deposit[i] == '.'){
count++;
if (count == 2){
validationResults = INVALID_TOO_MANY_DECIMAL_PLACES;
break;
}
continue;
}
if (deposit[i] < '0' || deposit[i] > '9'){
validationResults = INVALID_CHARACTER_ENTERED;
break;
}
}
if (count == 0){
validationResults = INVALID_NO_DECIMAL_PLACES;
}
if (validationResults == 0){
amt = atof(deposit.c_str());
if (amt > 9999.99)
validationResults = INVALID_TOO_LARGE_OF_A_NUMBER;
}
switch (validationResults) {
case 0 : cout << "Your deposit entered is a valid entry ";
cout << "Transaction complete ";
cout << "Press [ENTER] to quit ";
cin.get();
break;
case 1 : cout << "Invlid Entry: too many decimals ";
break;
case 2 : cout << "Invlid Entry: no decimals ";
break;
case 3 : cout << "Invlid Entry: characters enteres ";
break;
case 4 : cout << "Invlid Entry: too large ";
break;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.