Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Document preview thumbnail
Preview 2 out of 10 pages
Class notes

CSCE 120 Exam 2 Notes

Document preview thumbnail
Preview 2 out of 10 pages

CSCE 120 Exam 2 Notes - based on lecture slides, includes code snippets, can take to exam

Content preview

CSCE 120 — Exam 2 Reference Sheet
Exceptions | Streams | Pointers | Memory | Arrays | Classes | OOP


1 EXCEPTIONS & DATA VALIDATION

Exception Hierarchy Try / Catch
std::exception try {
std::runtime_error int x = stoi("hello");
overflow_error, range_error }
std::logic_error catch(std::invalid_argument const& e){
std::invalid_argument cout << e.what(); // "stoi"
std::out_of_range }
catch(std::out_of_range const& e){
cout << e.what();
}
Throwing catch(...){ // LAST: catches anything
#include <stdexcept> cout << "unknown error";
throw std::runtime_error("msg"); }
throw std::invalid_argument("bad");
throw std::out_of_range("too big");
Exception Propagation
• If f() throws & doesnt catch → travels up call stack
Integer Limit Checks • If main() doesnt catch → program crashes
#include <limits> // OR #include <climits> • Execution resumes after the matching catch block
std::numeric_limits<int>::max() // = INT_MAX • Catch in same function that threw = usually wrong
std::numeric_limits<int>::min() // = INT_MIN

// Check BEFORE arithmetic (not after!): stoi / stod
if (a > INT_MAX - b) // overflow check
throw std::out_of_range("overflow"); #include <string>
return a + b; int x = stoi("120"); // x = 120
int y = stoi("hello"); // throws invalid_argument
int z = stoi("9999999999");// throws out_of_range
WRONG: if(x+y > INT_MAX) — x+y already overflowed! double d = stod("3.14"); // d = 3.14
RIGHT: if(x > INT_MAX - y) — check before the op.
ALWAYS catch by const& — not by value!
Catch by value causes "slicing": child info lost.
e.what() returns the string msg from the constructor.

, 2 STREAMS (File, String, Formatting)

Headers & Types Stream Error Flags
Header Provides Flag Meaning
#include <fstream> ifstream, ofstream, fstream eof End of file reached
#include <sstream> istringstream, ostringstream fail Bad read (recoverable)
#include <iomanip> setprecision, setw, fixed... bad Stream corrupted


File Output (ofstream) stream.fail() // true if fail or bad set
stream.eof() // true if eof set
std::ofstream ofs("out.txt"); // overwrites if(stream){} // same as !stream.fail()
if(ofs.is_open()) ofs << "hi" << endl;
// Append mode: // Fix a failed read:
std::fstream fs("f.txt", std::ios::out|std::ios::app); stream.clear(); // 1. reset flags
stream.ignore(1000,"\n"); // 2. skip bad data
// OR: string garbage; stream >> garbage;
File Input (ifstream)
std::ifstream ifs("data.txt");
int x; ifs >> x; // read one int Output Buffer
// Read whole file word-by-word: • Output is buffered — may not appear if program crashes
std::string w; • endl flushes buffer; \n does NOT
while(ifs >> w) { /* process w */ }
• cout.flush() also works

String Streams iomanip Formatting
#include <sstream> double n = 123.4567;
std::istringstream iss("10 20 30"); cout << n; // 123.457 (6 sig figs)
int a,b,c; iss >> a >> b >> c; cout << setprecision(4) << n; // 123.5
cout << fixed << n; // 123.4567 (4 dec)
std::ostringstream oss; cout << scientific << n; // 1.2346e+02
oss << "val=" << 42; cout << setw(10) << n; // " 123.457" (next only!)
std::string s = oss.str(); // "val=42" cout << left << setw(10) << n; // "123.457 "
cout << boolalpha << true; // "true"

Getline + istringstream (COMMON PATTERN)
Seeking
std::string line;
std::getline(std::cin, line); // read full line fs.seekp(n); // move write pos to byte n
std::istringstream iss(line); fs.seekg(n); // move read pos to byte n
int sum=0, x=0; fs.tellp(); fs.tellg(); // get positions
while(iss >> x) sum += x; // NOTE: writing overwrites existing chars!



open() / close()
std::ofstream ofs("file1.txt");
ofs << "hello";
ofs.close();
ofs.open("file2.txt");
ofs << "goodbye";

Document information

Uploaded on
August 4, 2026
Number of pages
10
Written in
2026/2027
Type
Class notes
Professor(s)
Beideman
Contains
All classes
$10.98

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Sold
1
Followers
0
Items
10
Last sold
3 year ago



Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions