Not logged in. Log in with GitHub. About Websheets.

List of all available Websheets


Viewing cpp/cs104/exceptions/divide_assert by redekopp@usc.edu. You have unsaved changes. Log in above to create, edit, preview and publish Websheets.
PropertyValue
Description
html markup
shown to student
 
A brute force approach to error checking is an <tt>assert</tt> statement.  You must <tt>#include<cassert></tt>.
<p>
The <tt>assert</tt> statement acts like an if statement that will halt the program if the condition is <tt>false</tt> (i.e. the condition must be <tt>true</tt> for the program to continue).
<p>
The syntax is:
   <p>
      <pre>
      assert(condition);
      </pre>
Add an appropriate assert statement below to stop execution if the denominator is 0.
<p>
However, realize that using an assert is a pretty heavy-handed (drastic) approach.  It is useful during debugging and integration of software components, 
   but probably not a great solution in software for release. We want to handle errors more gracefully.
   <p>
      <strong>Exceptions will help us</strong>
Remarks
Comments, history, license, etc.
 
Copied from problem cpp/cs104/exceptions/divide1 (author: redekopp@usc.edu)
remove
Engine
Template / Reference solution
 
#include <iostream>
#include <cassert>
#include <stdexcept>
using namespace std;
int divide(int num, int denom)
{
   \[
REDACTED
]\
   return(num/denom);
}
int f1(int x)
{
   return divide(x, x-2);
}
int main()
{
   int res = -1, a;
   cout << "Enter: " << endl;
   cin >> a;
   res = f1(a);
   cout << res << endl;
   return 0;
}
C++ test suite
json list of stdin/args tests
e.g. [{"stdin":"hi", "args":["4", "5"]},
{"stdin":"noargs"}]

to just run once with no input use [{}]
 
[{"stdin":"4"}, {"stdin":"2"} ]
Solution visibility remove
Is example?
i.e., just a demo
remove


Optional properties:

Note: problems are open-source by default (see 'Public permissions'). Assumed license for open problems is Creative Commons 4.0 Attribution-ShareAlike unless specified in 'Remarks'.