C++ Tutorial

C++ User Input

cin reads from the keyboard. Pair it with cout so the user knows what to type.

cin fills a variable

cin is the standard input stream. The extraction operator >> reads the next value and stores it in a variable you already declared. The type of that variable decides how the text is interpreted: an int wants digits, a double accepts a decimal point.

Example

#include <iostream>
using namespace std;

int main() {
  int age;
  cout << "Enter your age: ";
  cin >> age;
  cout << "You entered " << age << endl;
  return 0;
}

In /cpp/try, type the input in the stdin box on the left, then compile. That box is what cin reads. The C++ editor uses g++.

Always prompt with cout

A program that waits on cin with no message looks frozen. Print a question first. End the prompt with a space or a newline so the typed value does not jam against the label.

Example

#include <iostream>
using namespace std;

int main() {
  double price;
  cout << "Price in euros: ";
  cin >> price;
  cout << "Recorded: " << price << endl;
  return 0;
}

Several values in a row

You can chain extractions the same way you chain insertions. cin skips whitespace: spaces, tabs, and newlines between values. On StudyGrid, put each number on its own line in stdin, or separate them with spaces.

Example

#include <iostream>
using namespace std;

int main() {
  int a, b;
  cout << "Enter two integers: ";
  cin >> a >> b;
  cout << "sum is " << a + b << endl;
  return 0;
}

Sample stdin for that program: 4 9 or two lines, 4 then 9. Both work.

The type must match

If the variable is int and the user types hello, extraction fails. In a small beginner program the rest of the run is then unreliable. Ask for the kind of value you declared, and test with matching stdin.

VariableTypical stdin
int n;42
double x;3.14
char c;A

Words versus whole lines

cin >> on a string reads one word: it stops at the first space. A full sentence needs getline, which the strings chapter covers. For numbers and single words, >>is the tool.

Declare the variable before cin. cin >> n cannot create n. The type has to exist already.

iostream covers both sides

One include, <iostream>, gives you cout and cin. Output asks. Input answers. Most interactive programs are that pair, repeated.

Next: the types you can store in those variables.

Worked examples

The short listings above are there so you can see the grammar. The programs here use the same statements on quantities that already have units: a speed, a pH, a count of bases. They are classroom numbers. Air resistance is ignored. g is 9.81 m/s² unless a line says otherwise.

Open them in the C++ editor at /cpp/try. Change one measurement and check whether the result still has the right unit.

Physics

Kinetic energy from the keyboard

Translational kinetic energy for a rigid body is half m v². Mass must be kilograms, speed metres per second, or the joule on the end of the print is a lie. 2 kg at 3 m/s is 9 J.

cin >> mass writes into mass. Pair each read with a prompt. If you only cin, the screen sits there and looks hung.

KE = ½ m v²

This is not rotational KE (½ I ω²), and it ignores rest energy. Fine for a trolley on a bench.

Example

#include <iostream>
using namespace std;

int main() {
  double mass, speed;
  cout << "mass in kg: ";
  cin >> mass;
  cout << "speed in m/s: ";
  cin >> speed;
  cout << "KE = " << 0.5 * mass * speed * speed << " J" << endl;
  return 0;
}

Maths

Circumference from a typed radius

The circumference of a circle is 2πr. π here is 3.14159, which is already more digits than a school ruler can justify. For r = 2, C is about 12.57.

C = 2 π r

Example

#include <iostream>
using namespace std;

int main() {
  double r;
  cout << "radius: ";
  cin >> r;
  cout << "C = " << 2.0 * 3.14159 * r << endl;
  return 0;
}

FAQ: C++ User Input

Common questions about this page.

What is the StudyGrid C++ tutorial?

The StudyGrid C++ tutorial is a full beginner-to-advanced track: syntax, types, input, loops, functions, classes, the STL, templates, maps, and lambdas. Each chapter has copy-and-run examples.

Should I run c++ cin examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn c++ cin in this C++ C++ lesson (C++ User Input).

Is the C++ editor the same as Try Python or Try HTML?

No. Try C++ compiles with g++ at /cpp/try and shows stdout plus compiler messages. Try Python stays at /try. Try HTML stays at /html/try. C++ lessons never open those editors.

Do I need to install a compiler to learn C++?

No. Open a chapter, click Try it in C++, and compile in the browser. You can also download a .cpp file and compile locally with g++.

Where should I start the C++ tutorial?

Start at C++ Intro, then Get Started and Syntax. After the first program, continue to output, variables, and if-else. After classes, open C++ Examples, then templates, map, and lambdas. Use Next at the bottom of each chapter.

Is the C++ tutorial free?

Yes. The C++ workshop on StudyGrid (studygrid.in) is free: dashboard, chapters, and the compile-and-run editor.