Answered step by step
Verified Expert Solution
Question
1 Approved Answer
The code is suppose to read all the inputs and spit out the output However it stopped after two Here's the code someone help #include
The code is suppose to read all the inputs and spit out the output
However it stopped after two
Here's the code someone help
#include #include using namespace std; const int MAX_TRANSACTIONS = 100; struct Transaction { char type; // 'd' for debit, 'c' for credit, 'b' for balance string date; double value; }; int daysInMonth(int month, int year) { if (month == 2) { if ((year % 400 == 0) || ((year % 100 != 0) && (year % 4 == 0))) return 29; // Leap year else return 28; } else if (month == 4 || month == 6 || month == 9 || month == 11) { return 30; } else { return 31; } } int main() { Transaction transactions[MAX_TRANSACTIONS]; int numTransactions = 0; // Read the initial balance double balance; cin >> transactions[0].date >> balance; transactions[0].type = 'b'; // 'b' for balance int startDay, startMonth, startYear; sscanf(transactions[0].date.c_str(), "%d/%d/%d", &startMonth, &startDay, &startYear); int currentDay = startDay; int currentMonth = startMonth; int currentYear = startYear; // Read transactions until 'q' or more than 30 days while (true) { // Handle overdraft before checking transaction order if (transactions[numTransactions].type == 'd' && balance < transactions[numTransactions].value) { // Complete the transaction and assess overdraft fee balance -= 15; cout << "Overdraft on " << transactions[numTransactions].date << ". $15 overdraft fee assessed." << endl; } cin >> transactions[numTransactions + 1].type; if (transactions[numTransactions + 1].type == 'q') break; cin >> transactions[numTransactions + 1].date >> transactions[numTransactions + 1].value; int transactionDay, transactionMonth, transactionYear; sscanf(transactions[numTransactions + 1].date.c_str(), "%d/%d/%d", &transactionMonth, &transactionDay, &transactionYear); if (transactionYear < currentYear || (transactionYear == currentYear && transactionMonth < currentMonth) || (transactionYear == currentYear && transactionMonth == currentMonth && transactionDay < currentDay)) { // Out-of-order error cout << "Entries must be provided in chronological order. " << transactions[numTransactions + 1].date << " entered after " << transactions[numTransactions].date << endl; return 1; // Exit with an error code } // Normal transaction if (transactions[numTransactions + 1].type == 'd') { balance -= transactions[numTransactions + 1].value; } else { balance += transactions[numTransactions + 1].value; } numTransactions++; // Update current date currentDay = transactionDay; currentMonth = transactionMonth; currentYear = transactionYear; // Check if exactly 30 days int daysDifference = 0; while (daysDifference < 30) { currentDay--; daysDifference++; if (currentDay == 0) { currentMonth--; if (currentMonth == 0) { currentMonth = 12; currentYear--; } currentDay = daysInMonth(currentMonth, currentYear); } } if (daysDifference >= 30) { break; } } // Calculate and print average balance double averageBalance = balance / (numTransactions + 1); cout << "30-day average balance (" << transactions[0].date << "-" << transactions[numTransactions].date << "): $" << fixed << setprecision(2) << averageBalance << endl; return 0; } message.txt 4 KB
Example Input: 12/1/2023 500 d 12/11/2023 200 d 12/23/2023 100 c 12/28/2023 200 d 12/31/2023 250 Example Output: 30-day average balance (12/1/2023-12/30/2023): $360.00
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started