Week 4 (CS 200)
Table of Contents
While loops, casting, file I/O
1. Class schedule
- Tuesday: While loops, casting, file I/O
- Thursday: Lab
2. Assignment links
| Assignment | Reading link | Canvas link |
|---|---|---|
| 📚 Intro: While loops | Read | Canvas quiz |
| 📚 Intro: File I/O | Read | Canvas quiz |
| 💾 Lab: While loops and file I/O | Read | Canvas assignment |
| 💬 Discussion: Deadly errors | Canvas discussion | |
| 🙆 Update: Week 4 | Cavans assignment |
3. Example code
3.1. While loops
3.1.1. While not old enough
#include <iostream> using namespace std; int main() { int age; cout << "Enter your age: "; cin >> age; while ( age < 18 ) // if ( age < 18 ) { cout << age << ": You can't vote" << endl; age++; } //else //{ cout << "You can vote at age " << age << endl; //} return 0; }
3.1.2. While not an intercept
#include <iostream> using namespace std; int main() { int x = -10, y = 5; while ( x != 0 && y != 0 ) { cout << "(" << x << "," << y << ") is not an intercept" << endl; x++; y++; } // x == 0 || y == 0 cout << "(" << x << "," << y << ") IS an intercept" << endl; return 0; }
3.1.3. Menu loop
#include <iostream> using namespace std; int main() { bool running = true; while ( running ) { cout << "1. Favorite movie" << endl; cout << "2. Show favorite game" << endl; cout << "3. Quit" << endl; cout << ">> "; int choice; cin >> choice; cout << endl; switch( choice ) { case 1: cout << "Lost Skeleton of Cadavra" << endl; break; case 2: cout << "IDK Achaea?" << endl; break; case 3: running = false; break; } } return 0; }
3.2. File I/O
3.2.1. File formats
#include <iostream> // Console I/O #include <fstream> // File I/O using namespace std; int main() { ofstream fout; // Output file stream variable cout << "Generating TXT file..." << endl; fout.open( "text.txt" ); fout << "Hello, world" << endl; fout.close(); cout << "Generating HTML page..." << endl; fout.open( "page.html" ); fout << "<h1> Header 1 </h1>" << endl; fout << "<p> This is a <em>paragraph</em> <br> Hello </p>" << endl; fout.close(); cout << "Generating Spreadsheet..." << endl; fout.open( "spreadsheet.csv" ); fout << "COURSE,ENROLLMENT" << endl; fout << "CS200,17" << endl; fout << "CS235,15" << endl; fout << "CS250,20" << endl; fout.close(); return 0; }
3.2.2. File output - logging
#include <iostream> #include <fstream> using namespace std; int main() { ofstream uselog( "logs/usage.txt" ); uselog << "PROGRAM START" << endl; ofstream errlog( "logs/errors.txt" ); uselog << "CREATED ERROR LOG" << endl; //errlog.open( "errors.txt" ); bool running = true; uselog << "CREATED RUNNING VARIABLE" << endl; while ( running ) { cout << "1. Favorite movie" << endl; cout << "2. Show favorite game" << endl; cout << "3. Quit" << endl; cout << ">> "; int choice; cin >> choice; uselog << "USER ENTERED " << choice << endl; cout << endl; switch( choice ) { case 1: cout << "Lost Skeleton of Cadavra" << endl; uselog << "SHOWED USER FAVORITE MOVIE" << endl; break; case 2: cout << "IDK Achaea?" << endl; uselog << "SHOWED USER FAVORITE GAME" << endl; break; case 3: running = false; break; default: cout << "Invalid choice!" << endl; errlog << "USER ENTERED INVALID CHOICE #" << choice << endl; } } uselog << "PROGRAM ENDED" << endl; return 0; }
3.2.3. File input - data file
#include <iostream> #include <fstream> using namespace std; int main() { int choice; ifstream fin( "data/userprefs.txt" ); if ( fin.fail() ) { cout << "ERROR: Could not find file!" << endl; return 1; } string movie, game; getline( fin, movie ); getline( fin, game ); fin.close(); bool running = true; while ( running ) { cout << "Favorite movie: " << movie << endl; cout << "Favorite game: " << game << endl; cout << endl << "UPDATE MENU" << endl; cout << "1. Favorite movie" << endl; cout << "2. Show favorite game" << endl; cout << "3. Quit" << endl; cout << ">> "; cin >> choice; cin.ignore(); cout << endl; switch( choice ) { case 1: cout << "Enter new movie: "; getline( cin, movie ); break; case 2: cout << "Enter new game: "; getline( cin, game ); break; case 3: running = false; break; } } ofstream fout( "data/userprefs.txt" ); fout << movie << endl; fout << game << endl; return 0; }