Variables
Introduction to variables and basic data types.
StartKey Concepts
Review core concepts you need to learn to master this subject
User Input
Variables
Arithmetic Operators
int
Type
double
Type
Chaining the Output
char
Type
string
Type
User Input
User Input
int tip = 0;
std::cout << "Enter amount: ";
std::cin >> tip;
std::cin
, which stands for “character input”, reads user input from the keyboard.
Here, the user can enter a number, press enter, and that number will get stored in tip
.
- 1The “Hello World!” program simply writes to the screen. It does not read anything, calculate anything, or allow for user input. That’s no fun! Real programs tend to produce results based on some …
- 2“Every variable in C++ must be declared before it can be used!” Suppose we are building a game and we want to keep track of a player’s score that goes from 0 to 10. We need a variable! Before w…
- 3After we declare a variable, we can give it a value! Suppose that we have declared an int variable called score, to set it to 0, we can simply write: score = 0; - The score is the name of the v…
- 4We can both declare and assign a value to a variable in a single initialization statement. Suppose we have these two lines: // Declare a variable int score; // Initialize a variable score = 0; …
- 5Computers are incredible at doing calculations. Now that we have declared variables, let’s use them with arithmetic operators to calculate things! Here are some arithmetic operators: - + additi…
- 7Like we mentioned in the introduction, another way to assign a value to a variable is through user input. A lot of times, we want the user of the program to enter information for the program. We h…
- 8Now that we’ve learned about the basics of variables and cin, let’s write a program! The mad scientist Kelvin has mastered predicting the weather in his mountain-side meteorology lab. Recently, K…
- 9Let’s go back to the temperature.cpp that we wrote. This time, instead of giving tempf a value of the current temperature in New York: tempf = 83; Let’s ask the user what the temperature is …
What you'll create
Portfolio projects that showcase your new skills
Dog Years
Dogs have ages that don't quite work like ours. This can be a little ruff when you want to know how old your dog would be if he/she is a human. Don't fret — build a dog-age-converting program to translate your dog's age into human years!
Quadratic Formula
Write a C++ program that solves the quadratic equation.
Piggy Bank
You just returned from a trip to South America and you came back with three different kinds of currencies. Let's convert them to USD!
How you'll master it
Stress-test your knowledge with quizzes that help commit syntax to memory