C++ cin and cout Example

This C++ example uses cin, cout, and simple arithmetic. It is a practical first test for browser-side C++ input and terminal output.

C++ Compiler

Code Example

#include <iostream>
#include <string>

using namespace std;

int main() {
    string name;
    int first;
    int second;

    cout << "Enter name: ";
    cin >> name;
    cout << "Enter two numbers: ";
    cin >> first >> second;

    cout << name << ", sum = " << first + second << endl;
    return 0;
}

Expected Output

Enter name: Mayur
Enter two numbers: 7 8
Mayur, sum = 15

Practice Steps

  1. Open the C++ compiler and paste the code.
  2. Run it and enter a name plus two numbers.
  3. Move the addition logic into a function for extra practice.
HOME LEARN COMMUNITY DASHBOARD