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

List of all available Websheets


Viewing cpp/streams/cinlen by daveagp@gmail.com. You have unsaved changes. Log in above to create, edit, preview and publish Websheets.
PropertyValue
Description
html markup
shown to student
 
Write a <i>file size calculator</i>: it should tell you how long
its input <tt>cin</tt> was. For instance, if <tt>word.txt</tt> contains
<tt>Hello</tt>, then <tt>./cinlen &lt; word.txt</tt> should print <tt>5</tt>.
<p>
You will need to use <tt>get</tt> and <tt>fail</tt>. Remember that <tt>fail</tt> returns <tt>true</tt> <i>after</i> reading past the end of the file.
Public permissions remove
Engine
Template / Reference solution
 
#include <iostream>
#include <cstring>
using namespace std;
int main() {
   int result = 0;
   while (\[!cin.fail()]\) {
\[
      // get the next character
      char ch = (char) cin.get();
      if (!cin.fail())
         result++;
]\
   }
   cout << \[result]\ << 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": "Hello"},
   {"stdin": "2  spaces"},
   {"stdin": "/==\\\n|oo|\n\\==/"}
]
Solution visibility remove
Add compiler flag(s)
json list of flags
e.g. ["-Wno-unused-variable"]
 
["-Wno-unused-variable"]
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'.