This is a simple game I'm writing to test the idea of using games to teach C++.
https://learncodethehardway.com/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
45 lines
780 B
45 lines
780 B
7 months ago
|
#include <iostream>
|
||
|
#include <algorithm>
|
||
|
#include <vector>
|
||
|
#include <stdexcept>
|
||
|
|
||
|
using namespace std;
|
||
|
|
||
|
class ExpectFail {
|
||
|
public:
|
||
|
string message;
|
||
|
|
||
|
ExpectFail(string m) : message(m) {};
|
||
|
};
|
||
|
|
||
|
void expect(bool test, string msg) {
|
||
|
if(!test) {
|
||
|
throw ExpectFail("Expectation failed.");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
int area(int length, int width) {
|
||
|
expect(length > 0 && width > 0, "Bad area given.");
|
||
|
|
||
|
return length * width;
|
||
|
}
|
||
|
|
||
|
int main() {
|
||
|
vector<int> numbers;
|
||
|
|
||
|
try {
|
||
|
numbers.push_back(100);
|
||
|
numbers.push_back(-100);
|
||
|
|
||
|
cout << "Area " << area(numbers.at(1), 100) << "\n";
|
||
|
|
||
|
return 0;
|
||
|
} catch(out_of_range &e) {
|
||
|
cout << "Out of range ERROR: " << e.what() << "\n";
|
||
|
return 2;
|
||
|
} catch(...) {
|
||
|
cout << "Exception: something went wrong\n";
|
||
|
return 3;
|
||
|
}
|
||
|
}
|