#include using namespace std; #define C_STYLE_PI 3.1415926 int main() { // use const to define constants that never // (or rarely) change const double PI = 3.1415926; // constants do not change unless we recompile the code const double interest_rate = 0.09; double balance = 100.0; // subtract withdrawal amount double amount; cout << "Enter ATM Withdrawal Amount: "; cin >> amount; // balance = balance - amount; balance -= amount; // add earned interest // balance = balance * (interest_rate + 1); // balance *= interest_rate + 1; balance += balance * interest_rate; cout << "You earned some interest! Total balance: $"; cout.precision( 2 ); cout.width( 20 ); // cout.width( -20 ); // left justify cout.fill( 'a' ); cout << balance << endl; // float -- typically has 7 digits of accuracy // double -- typically has 14 digits of accuracy #if 0 int x, y; // these lines are commented out y = x + 10; cout << y << endl; #endif char answer; cout << "Do you want to know what your balance is? (y/n): "; cin >> answer; cout << "You entered '" << answer << "'\n"; // if ( condition ) { // code-to-run-if-condition-is-true // } // operator == is the test for equality // operator != is the test for inequality // operator > >= < <= if ( answer == 'y' ) { cout << "Okay, your balance is $" << balance << endl; } else { cout << "Okay, no balance for you." << endl; } // if ( condition ) { // code-to-run-if-condition-is-true // } else { // code-to-run-if-condition-is-false // } // LOOK OUT! Don't use = when you mean == if ( answer = 'y' ) { cout << "STILL ANSWERED 'y'" << endl; } // A "condition" is a true/false value (boolean or bool) // In C++, false == 0 and true == non-zero int x = 10, y = 20; if ( x > 0 ) { cout << "x is greater than 0.\n"; } // use && to mean Logical-AND if ( x > 0 && y > 0 ) { cout << "both x and y are greater than 0.\n"; } // use || to mean Logical-OR if ( x > 100 || y > 0 ) { cout << "x is greater than 100 OR y is greater than 0.\n"; } // compound condition is evaluated left-to-right // and it stops (short-circuits) if possible if ( x > 100 && y > 0 ) { cout << "x is greater than 100 AND y is greater than 0.\n"; } if ( x + y > 25 && x > 0 ) { cout << "This ought to be true.\n"; } // if-else chain: // // if ( condition1 ) // { // code-to-run-if-condition1-is-true // } // else if ( condition2 ) // { // code-to-run-if-condition2-is-true // } // else if ( condition3 ) // { // code-to-run-if-condition3-is-true // } // else // catch-all default case // { // code-to-run-if-all-conditions-are-false // } double grade = 87.5; char letter_grade; if ( grade >= 90.0 ) { letter_grade = 'A'; } else if ( grade >= 80.0 ) { letter_grade = 'B'; } else { letter_grade = 'F'; } return 0; }