EECS 281 Lecture 0
Reading in Data with cin or getline
while (cin >> new_value) {
// execute only if new_value read properly
}
while (getline(cin, new_line)) {
// execute only if new_line read properly
}
Avoid Duplicating Code: http://waleedkhan.name/281/code-deduplication/
- Write functions, use classes, many more tips
Managing Program Data
Good: Class with 15 variables
Bad: Function(s) with 15 parameters
Worse: 15 global variables
Save memory in class or struct: Arrange member variables by size, from largest to smallest
- class object (largest) -> string -> double -> ... -> bool (smallest)
Write new and delete in pairs, and insert rest of code in between
Compiler Flags and #ifdef ...
1. Use the compiler flag –DDEBUG and
#ifdef DEBUG
#ifdef DEBUG
#define _(args) args
#else
#define _(args)
#endif
_(cout << "debug-only message" << endl);
OR
2. Use the compiler flag -DNDEBUG and // DND = do not debug?
#ifdef NDEBUG
XCode redirect file to cin
Xcode
// redirect file to cin
freopen("input01.txt", "r", stdin);
Also go to Product > Scheme > Edit Scheme > Run (on the
left) > Options (at the top), and set the custom working
directory to where your input files are located.
Select Arguments at the top and include any command-line
arguments you want to pass to your program on launch