/*--- CS 111 - Week 13 Lecture 2 - 2024-11-21 by: Sharon Tuttle last modified: 2024-11-21 ---*/ #include <cstdlib> #include <iostream> #include <string> #include <cmath> using namespace std; /*=== signature: vertical: string -> int purpose: expects any string, has the SIDE-EFFECT of printing that string to the screen in a "vertical" fashion, and returns the length of the string tests: vertical("moo") == 3 and has the side-effect of printing to the screen m o o vertical("oink") == 4 and has the side-effect of printing to the screen o i n k vertical("") == 0 and does not print anything to the screen ===*/ int vertical(string desired_msg) { // set up the current location in the string, // initially 0 int curr_loc = 0; // while I'm not finished walking through the string while (curr_loc < desired_msg.length()) { // print the character at the current location // on its own line cout << desired_msg.at(curr_loc) << endl; // move to the next location curr_loc = curr_loc + 1; } // return the string's length return desired_msg.length(); } /*--- test the functions above ---*/ int main() { cout << boolalpha; cout << endl; cout << "*** Testing: vertical ***" << endl; // because vertical has side-effects, for its tests // I need to print for the user a description // of what they should see IF the function is // working correctly cout << endl; cout << "Should see a vertical moo followed by true:" << endl; cout << (vertical("moo") == 3) << endl; cout << endl; cout << "Should see a vertical oink followed by true:" << endl; cout << (vertical("oink") == 4) << endl; cout << endl; cout << "Should see just true:" << endl; cout << (vertical("") == 0) << endl; cout << endl; cout << "Enter a message to print vertically: " << endl; string message; getline(cin, message); cout << endl; cout << "Compare the following two outputs: " << endl; cout << vertical(message) << endl; cout << endl; vertical(message); //==== // after class: demoing a little example array // an array looky, able to hold 3 int values int looky[3]; // a not-fancy example of initializing the // elements in this array looky[0] = 13; looky[1] = 5; looky[2] = 100; // printing looky's contents: cout << endl; cout << "looky[0]: " << looky[0] << endl; cout << "looky[1]: " << looky[1] << endl; cout << "looky[2]: " << looky[2] << endl; // demoing changing an array element looky[2] = 44; // now the array contains... cout << endl; cout << "After changing looky[2] to 44: " << endl; int index = 0; // want the array index to be strictly // LESS than the array's size! while (index < 3) { cout << "looky[" << index << "]: " << looky[index] << endl; index = index + 1; } cout << endl; return EXIT_SUCCESS; }