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

List of all available Websheets


Viewing cpp/control/break_continue 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 program that reads any number of lines of text from the user.
It should print out each one, with its line number. However,
<ul>
<li>if the line is <tt>SKIP</tt>, skip it
<li>if the line is <tt>DONE</tt>, stop reading completely
</ul>
E.g., for the input
<pre>
Hello World
Bonjour, Monde
SKIP
Hallo, Welt
DONE
Hola, Mundo
</pre>
the output should be
<pre>
1 Hello World
2 Bonjour, Monde
3 Hallo, Welt
Printed 3 lines.
</pre>
(The summary line logic is written for you already.)
Public permissions remove
Engine
Template / Reference solution
 
#include <iostream>
using namespace std;
int main() {
   string line_of_text;
   int counter = 0;
   while (true) {
      getline(cin, line_of_text);
      if (cin.fail())
         \[break    ]\;
      
      if (line_of_text == "SKIP") 
         \[continue]\;
      if (line_of_text == "DONE") 
         \[break    ]\;
      counter++;
      cout << counter << " " << line_of_text << endl;
   }
   cout << "Printed " << counter << " lines." << endl;
}
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 World\nBonjour, Monde\nSKIP\nHallo, Welt\nDONE\nHola, Mundo\n"},
   {"stdin": "SKIP\nSKIP\nSKIP\nto my loo\nSKIP\nto the loo\nmy darling\n"},
   {"stdin": "apple\norange\nDONE\nDONE\nSKIP\nbanana\n"}
]
Solution visibility 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'.