/*--- CS 111 - Week 15 Lecture 1 - 2025-12-09 compile using: g++ 111lect15-1.cpp -o 111lect15-1 run using: ./111lect15-1 by: Sharon Tuttle last modified: 2025-12-10 ---*/ #include <cstdlib> #include <iostream> #include <string> #include <cmath> #include <fstream> using namespace std; /*=== signature: assn_header: string int -> void purpose: expects a course name and an assignment number, has the SIDE EFFECT of printing the course name on one line, and "Assignment #" followed by the assignment number on the next line, and then adding another newline, and returns NOTHING tests: assn_header("CS 111", 5); ...should print (followed by a blank line): CS 111 Assignment #5 assn_header("Math 253", 2); ...should print: (followed by a blank line): Math 253 Assignment #2 ===*/ void assn_header(string course, int assn_num) { cout << course << endl; cout << "Assignment #" << assn_num << endl; cout << endl; } /*--- demo a few more C++ things, and test the function above ---*/ int main() { cout << boolalpha; // demo the difference between prefix and postfix ++ int quant = 410; int value; value = 3 + (quant++); cout << "quant: " << quant << endl << "value: " << value << endl; quant = 410; value = 3 + (++quant); cout << "quant: " << quant << endl << "value: " << value << endl; // NOTICE how the tests for this void function are written!! cout << endl; cout << "*** Testing: assn_header ***" << endl; cout << "SHOULD see CS 111, Assignment #5, and a blank line: " << endl; assn_header("CS 111", 5); cout << "SHOULD see Math 253, Assignment #2, and a blank line: " << endl; assn_header("Math 253", 2); //===== // demo of getline function // (allowing you to read a string containing blanks/tabs!) // first: note that, with a string variable, >> will read in // JUST to the first blank, tab, or newline string fave_topping; cout << "Enter your favorite (single-word) waffle topping: " << endl; cin >> fave_topping; cout << "You like " << fave_topping << " on your waffles!" << endl; // when going from >> to getline, // may need an extra getline call to get // "past" a leftover newline // (comment out this getline to see what happens if // you do not do this) getline(cin, fave_topping); // but getline reads all the characters, even blanks/tabs, // until a newline character is reached cout << endl; cout << "Enter another (can-be-multi-word) favorite waffle topping: " << endl; getline(cin, fave_topping); cout << "You like " << fave_topping << " on your waffles!" << endl; //===== // working demo: reading every, say, number from a file // first: write a file containing some multiples of 3 ofstream demo_fout; demo_fout.open("mults-of-3.txt"); for (int i=0; i<13; i++) { demo_fout << (i*3) << " "; } demo_fout.close(); // now: read every number from that file ifstream demo_fin; demo_fin.open("mults-of-3.txt"); int next_num; cout << endl; cout << "*** CONTENTS from mults-of-3.txt ***" << endl; while (demo_fin >> next_num) { cout << "Read: " << next_num << endl; } demo_fin.close(); //===== // working demo: reading every, say, line from a file // first: write a file containing some waffle toppings string waffle_toppings_file; cout << endl; cout << "Enter a file name to write some waffle toppings to: " << endl; cin >> waffle_toppings_file; ofstream waffle_tops_fout; waffle_tops_fout.open(waffle_toppings_file); waffle_tops_fout << "Butter" << endl; waffle_tops_fout << "Maple Syrup" << endl; waffle_tops_fout << "Whipped Cream" << endl; waffle_tops_fout << "Loganberry Jelly" << endl; waffle_tops_fout << "Nutella" << endl; waffle_tops_fout.close(); // now: read every waffle topping from that file ifstream waffle_tops_fin; waffle_tops_fin.open(waffle_toppings_file); string next_topping; cout << endl; cout << "*** CONTENTS from " << waffle_toppings_file << " ***" << endl; while (getline(waffle_tops_fin, next_topping)) { cout << "Read: " << next_topping << endl; } cout << endl; waffle_tops_fin.close(); return EXIT_SUCCESS; }