So far, the console was only told to output hardcoded values onto the screen. Other than outputting values, the console is also able to accept any inputs that are given by users.

The library <iostream> not only has cout, a command that outputs values, but it also includes cin, a command that accepts inputs. cin stands for Console INput and its syntax is formatted as follows.

<span id="L1" class="line"><span class="n">cin</span> <span class="o">&gt;&gt;</span> <span class="n">variableName</span><span class="p">;</span></span>

The cin command uses >> in its format while cout uses of <<. To differentiate them, imagine the symbols as lines moving inwards (>>) or moving outwards (<<). cin will tell the console to put all inputs into a specified variable. Values cannot go into a cin command. Like cout, each cin command can accept multiple inputs by separating each variable name with >>.

Example Code:
<span id="L1" class="line"><span class="cp">#include &lt;iostream&gt;</span></span>
<span id="L2" class="line"><span class="k">using</span> <span class="k">namespace</span> <span class="n">std</span><span class="p">;</span></span>
<span id="L3" class="line"><span class="c1">// This code takes two numbers and returns their sum</span></span>
<span id="L4" class="line"><span class="kt">int</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span></span>
<span id="L5" class="line">    <span class="kt">int</span> <span class="n">num</span><span class="p">,</span> <span class="n">num2</span><span class="p">;</span>                       <span class="c1">// initializes variables</span></span>
<span id="L6" class="line">    <span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">"Pick 2 numbers!"</span> <span class="o">&lt;&lt;</span> <span class="n">endl</span><span class="p">;</span>   <span class="c1">// displays message</span></span>
<span id="L7" class="line">    <span class="n">cin</span> <span class="o">&gt;&gt;</span> <span class="n">num</span> <span class="o">&gt;&gt;</span> <span class="n">num2</span><span class="p">;</span>                  <span class="c1">// receives input</span></span>
<span id="L8" class="line">    <span class="c1">// the first input is placed in num and the second is placed in num2</span></span>
<span id="L9" class="line">    <span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">"The Sum is "</span> <span class="o">&lt;&lt;</span> <span class="n">num</span> <span class="o">+</span> <span class="n">num2</span><span class="p">;</span> <span class="c1">// outputs the sum</span></span>
<span id="L10" class="line">    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>                            <span class="c1">// returns 0</span></span>
<span id="L11" class="line"><span class="p">}</span></span>
Sample run:
>>> Pick 2 numbers!
3
87
>>> The Sum is 90

M3C/MOJ

Markville Coding Online Judge is a website that has problems from past contests as well as problems from other sources for students to practice coding. It also features an automated judge that will test any submitted code against its problems. Users could submit their code to be tested by the judge. This website also has live coding contests to allow users to practice for the CCC in February.

Only registered users are able to use the online judge on the site. To register, click the register button in the top right corner. To join the Markville Secodnary School Organization on M3C, go to your profile and change the organization to Markville Secondary School CS. :))

Submitting Code

The problems page will display a list of all problems that the online judge can test. The points represent the general difficulty of that problem. (The easier problems are worth less points)

image
  • Each problem will ask for a specific output given a specific input. The details and examples of the problem are located on the contest problem page.

  • On the contest problem page, the Submit link will be located on the right side.

  • Copy and paste the entire code or upload the code file into the submission page

  • Ensure that the language chosen in the drop-down menu is C++ 03 (g++ 4.1.3)

  • Pray that the code works and outputs all intended values. This document, from now on, will mostly use contest questions found on these sites for its practice questions. Expect that the contest questions found in the CCC to be of similar nature to the questions on these sites.

In Summary

  • cin is a command found in the <iostream> library that takes a user input and stores it into a variable.

  • M3C is a site made for students to practice contest-styled coding problems

Contest Questions