CS 112 - Week 4 Lecture 1 - 2022-09-13 TODAY WE WILL * announcements * aside: the ifstream/ofstream fail method * example: reading everything from a file * examples: functions include an array parameter * prep for next class * SCHEDULE for the next TWO weeks: * TODAY: some more file i/o tidbits and array parameters * THURSDAY, Sept 15 - REVIEW for Exam 1, Sept 23, and if time, START discussion of writing your own classes * Friday, Sept 16 - typical lab * by 11:59 pm, get at least 1st attempts at Problems 1-4, and at least an attempt at some of Problems 5-7, submitted * BUT Homework 4 will not come out until AFTER Exam 1 * Tuesday, Sept 20 - writing your own classes * SUBMIT any final improvements/versions of Homeworks 1-3 programming problems by 11:59 pm on Tuesday, Sept 20 * 12:01 am, Sept 21: example solutions for Homeworks 1-3 programming problems will be reachable on Canvas * Thursday, Sept 22 - more on writing your own classes * Friday, Sept 23, DURING Lab: EXAM 1, taken IN BSS 317 * (and Homework 4 will be available sometime during the weekend after Exam 1) * READING: * for either Thursday after the Exam 1 Reviewq OR next Tuesday: Savitch, Chapter 10, starting at 10.2 (but mostly 10.3) ==== aside: ifstream/ofstream fail method ==== * you can call this method with no arguments on a file stream you have attempted to open, and it returns true if that open failed, and false otherwise my_stream.open(...); if (my_stream.fail()) { cout << "Sorry, could not open that!" << endl; exit_or_return_statement_dep_on_context; } ===== reading everything from a file ===== * ...when you don't know in advance how much is in there! * newer C++ compilers (like those available in both CS50 IDEs...) have getline and extraction operator >> versions that happen to return true if the latest read succeeded, and they return false otherwise * SO -- these can reasonably be used as the bool expression in a while loop! while (getline(in_stream, my_var)) { ...only reach here if latest read succeeded... } while (in_stream >> my_var) { ...only reach here if latest read succeeded... } ===== * can you have an array parameter in C++? YES -- but note that C++ views the array (under the hood) as where it STARTS * an array parameter does NOT have a size indicated!!! (because it represents future array arguments, and each of those WILL have a size when they are declared...) ...and the array argument is passed as the address of where the array starts in memory...! ret_type arr_funct(..., arr_type arr_param[], int arr_size, ...) * note that to indicate an expected array in a function signature, we'll use the notation: ; signature: arr_funct: .... arr_type[] int ... -> ret_type * IF the function is NOT to change its array parameter, it is considered good style to put the keyword const before its type: ret_type arr_funct(..., const arr_type arr_param[], int arr_size, ...) * see example function print_nums (whose argument array is NOT changed) and example function get_nums (whose argument array IS changed) ^ started in class, completed after class