/*--- Little EXTRA getline example NOTE: EXPECTS files in the same folder named: * registrants.txt * clients.txt (so should CREATE these, however you'd like, before running this) compile using: g++ getline-ex.cpp -o getline-ex run using: ./getline-ex by: Sharon Tuttle last modified: 2023-12-08 ---*/ #include <cstdlib> #include <iostream> #include <string> #include <cmath> #include <fstream> using namespace std; /*--- test the functions above ---*/ int main() { cout << boolalpha; string whole_name; // EXAMPLE 1 cout << endl; cout << "Enter a whole name: "; // reads everything typed up until typing Enter into whole_name getline(cin, whole_name); cout << "You entered: " << whole_name << endl; // EXAMPLE 2 ifstream inny; inny.open("registrants.txt"); cout << endl; if (inny.fail()) { cout << "OOPS, need a file registrants.txt for this example!" << endl << " Create one, and then try again." << endl; return EXIT_FAILURE; } // reads the first line of registrants.txt into whole_name getline(inny, whole_name); cout << "First registrant: " << whole_name << endl; inny.close(); // EXAMPLE 3 ifstream another_inny; another_inny.open("clients.txt"); cout << endl; if (another_inny.fail()) { cout << "OOPS, need a file clients.txt for this example!" << endl << " Create one, and then try again." << endl; return EXIT_FAILURE; } while (getline(another_inny, whole_name)) { cout << "next client: " << whole_name << endl; } another_inny.close(); cout << endl; return EXIT_SUCCESS; }