TalentJR

🧩 C++ programming tutorial for beginners · Class 11 · Lesson 1 of 18 · Free

C++ Basics – cout, cin, Variables, Loops, Functions and References

⏱ About 35 minutes · ▶ run real C++ here — nothing to install · no sign-in

In this lesson you will

  • Write and run a C++ program that prints text with cout and reads input with cin
  • Use int, double, char, bool and string variables with if/else and loops
  • Write functions and use references to change a variable inside a function

Your class teacher types 40 report cards by hand every term. Totals, averages and grades take her a whole evening. In this lesson you write your first C++ programs, and by the end you can make the computer do that work in a second.

What is C++ and how do I write my first C++ program?

C++ is a programming language built on top of C. It adds classes, a big standard library and safer tools like string. It is used for games, browsers, compilers and fast apps.

C++ is compiled. A program called a compiler turns your code into machine code first, then the computer runs it. When you press ▶ Run, a real C++ compiler (clang) does this inside your browser.

Every C++ program starts running from main(). cout (say "see-out") prints text. The << arrows push values into cout, one after another. Every statement ends with a semicolon ;.

Try it: add a third cout line that prints your school's name, and run again.

🧩 C++ — you can edit this code
Output
Hello from C++!
Today's attendance: 38 of 40

💡 #include <iostream> brings in cout and cin. 'using namespace std;' is a shortcut for small programs, so you can write cout instead of std::cout.

What are the data types in C++ (int, double, char, bool, string)?

A variable is a named box that holds a value. In C++ you must say the type of the box when you create it. The type decides what the box can hold.

int holds whole numbers. double holds numbers with a decimal point. char holds one character in single quotes. bool holds true or false. string holds text in double quotes.

sizeof tells you how many bytes a type uses. On this compiler an int is 4 bytes and a double is 8 bytes. Other computers can differ for some types.

Try it: change fee to 1250.75 and run again. Then change the name and see .size() change. It counts every character, including the space.

🧩 C++ — you can edit this code
Output
Name: Aarav Mehta (11 characters)
Roll 17, Section B
Fee: Rs 1250.5, paid: 1
int uses 4 bytes, double uses 8 bytes on this compiler

💡 cout prints a bool as 1 (true) or 0 (false). Write cout << boolalpha; once to see the words true and false.

How do I take user input in C++ with cin and getline?

cin (say "see-in") reads what the user types. cin >> marks reads one word or number and stores it in marks. It stops at a space.

To read a full line with spaces, like a full name, use getline(cin, name).

The box below runs with the typed lines shown under "inputs". Edit them and run again to try other values.

🧩 C++ — you can edit this code
Output
Enter your full name: Priya Nair
Enter Maths and Science marks: 88 76
Priya Nair scored 164 out of 200

💡 Read with getline before cin >> here. If cin >> comes first, it leaves the Enter key behind and the next getline reads an empty line.

How do if/else and loops work in C++?

if checks a condition. The code in its braces runs only when the condition is true. else if and else handle the other cases. Use == to compare, never a single =.

A for loop repeats code a fixed number of times. for (int i = 1; i <= 5; i++) means: start i at 1, keep going while i is 5 or less, add 1 each time.

A while loop repeats while a condition stays true. If you learned Python or JavaScript, the ideas are the same; only the brackets and semicolons change.

int marks[5] holds 5 ints in a row, called an array. marks[0] is the first one and marks[4] the last. Lesson 5 shows vector, a better list that can grow.

Try it: change the cut-off for A from 90 to 85 and run again.

🧩 C++ — you can edit this code
Output
Student 1: 92 -> A
Student 2: 67 -> B
Student 3: 45 -> C
Student 4: 81 -> B
Student 5: 30 -> Needs help
Hitting fours, 20 runs takes 5 balls

How do functions and references work in C++?

A function is a named block of code you can call again and again. You write its return type, its name and its parameters: double average(int a, int b).

Normally C++ passes a COPY of each value into a function. Changing the copy does not change the original variable.

A reference is another name for an existing variable. You write it with &, like int& total. A function that takes a reference can change the caller's variable.

Try it: remove the & from addBonus and run again. The bonus disappears, because the function only changed a copy.

🧩 C++ — you can edit this code
Output
Average: 84.3333
English after bonus: 83
English now: 100

💡 We divide by 3.0, not 3. Dividing an int by an int throws away the decimal part.

🤔 Predict the output

Think like C++: read the code and choose what it prints. You get the reason straight away.

Question 1. What does this print?

int a = 7, b = 2;
cout << a / b << " " << a % b << "\n";

Question 2. What does this print?

string s = "Cric";
s += "ket";
cout << s.size() << s[0] << "\n";

Question 3. What does this print?

int x = 5;
int& r = x;
r = r + 10;
cout << x << "\n";

🧩 Your challenge

Make a mini report card. Read a student's name (one word) and marks in 5 subjects. Print the total, the average and a grade. Write a function char gradeFor(double avg) that returns 'A' for 90 and above, 'B' for 75 and above, 'C' for 33 and above, else 'F'. For Riya with 95 88 92 79 90 it should print: Total: 444 Average: 88.8 Grade: B

Expected output (with the inputs given):
Enter name: Riya
Enter 5 marks: 95 88 92 79 90
Report card for Riya
Total: 444
Average: 88.8
Grade: B
🧩 C++ — you can edit this code

⚠️ Common mistakes (and how to fix them)

❌ Wrong

int runs = 45
cout << runs << "\n";

Line 1: error: expected ';' at end of declaration

✅ Right

int runs = 45;
cout << runs << "\n";

Every C++ statement must end with a semicolon. The compiler reports the error on the line where it expected the ;.

❌ Wrong

int total = 245;
double avg = total / 3;
cout << avg << "\n";

Compiles and runs, but prints: 81 ⏎

✅ Right

int total = 245;
double avg = total / 3.0;
cout << avg << "\n";

total / 3 is int divided by int, so the answer is 81 before it is stored in the double. Use 3.0 to keep the decimal part.

❌ Wrong

#include <iostream>
using namespace std;

void addGraceMarks(int marks) {
    marks = marks + 5;
}

int main() {
    int physics = 30;
    addGraceMarks(physics);
    cout << physics << "\n";
    return 0;
}

Compiles and runs, but prints: 30 ⏎

✅ Right

#include <iostream>
using namespace std;

void addGraceMarks(int& marks) {
    marks = marks + 5;
}

int main() {
    int physics = 30;
    addGraceMarks(physics);
    cout << physics << "\n";
    return 0;
}

Without &, the function gets a copy and changes only the copy. Add & to take a reference, so the change reaches physics.

📌 Remember

  • Every C++ program starts in main(); cout << prints and cin >> reads input.
  • Give each variable a type: int, double, char, bool or string.
  • int / int drops the decimal part; divide by 3.0 to keep it.
  • if/else, for and while work like in Python and JavaScript, with braces and semicolons.
  • A function gets copies of its arguments; a reference (&) lets it change the original.

Finished the quiz and the challenge?

Questions students ask

What is the difference between C and C++?

C++ started as C with classes. It keeps almost all of C and adds classes, references, string, templates and the Standard Template Library. Most C code also compiles as C++, with a few small differences.

Should I use endl or "\n" in C++?

Both end the line. endl also flushes, which means it pushes the text out to the screen right away. That is slower when you print many lines, so most programmers use "\n" and keep endl for the rare times a flush is needed.

Is C++ still used in 2026?

Yes. Game engines, web browsers, databases and many fast programs are written in C++. It also stays popular in programming contests because it runs quickly.

Why do people write using namespace std?

The standard library keeps its names inside a namespace called std, so cout is really std::cout. The using line lets you skip std:: in small programs. In big projects people usually write std:: to avoid name clashes.