Bird
Raised Fist0
C++programming~10 mins

Multiple input and output in C++ - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Multiple input and output
Start
↓
Read input1, input2
↓
Process inputs
↓
Output result1, result2
↓
End
The program reads multiple inputs, processes them, and then outputs multiple results.
Execution Sample
C++
#include <iostream>
using namespace std;
int main() {
  int a, b;
  cin >> a >> b;
  cout << a + b << " " << a * b << endl;
  return 0;
}
This code reads two integers, then outputs their sum and product.
Execution Table
StepActionInput ValuesVariables AfterOutput
1Start program-a: uninitialized, b: uninitialized-
2Read inputsa=3, b=4a:3, b:4-
3Calculate sum and product-a:3, b:4-
4Output results-a:3, b:47 12
5End program-a:3, b:4-
💡 Program ends after outputting sum and product of inputs.
Variable Tracker
VariableStartAfter InputAfter CalculationFinal
auninitialized333
buninitialized444
Key Moments - 2 Insights
Why do we use 'cin >> a >> b;' instead of two separate input lines?
Using 'cin >> a >> b;' reads both inputs in one line, making the code shorter and easier to read, as shown in execution_table step 2.
How does the program output two values in one line?
The program uses 'cout << a + b << " " << a * b << endl;' to print both results separated by a space, as seen in execution_table step 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what are the values of 'a' and 'b' after step 2?
Aa=0, b=0
Ba=7, b=12
Ca=3, b=4
Da and b are uninitialized
💡 Hint
Check the 'Variables After' column in step 2 of the execution_table.
At which step does the program output the sum and product?
AStep 4
BStep 3
CStep 2
DStep 5
💡 Hint
Look at the 'Output' column in the execution_table.
If the input values were changed to 5 and 6, what would be the output?
A5 6
B11 30
C30 11
DError
💡 Hint
Sum is a+b and product is a*b as shown in the code and execution_table.
Concept Snapshot
Multiple input and output in C++:
- Use 'cin >> var1 >> var2;' to read multiple inputs.
- Use 'cout << val1 << " " << val2 << endl;' to output multiple values.
- Inputs and outputs can be on the same line.
- Variables hold input values for processing.
- Program ends after outputting results.
Full Transcript
This example shows how a C++ program reads two integers from the user using 'cin >> a >> b;'. Then it calculates the sum and product of these two numbers. Finally, it outputs both results on the same line separated by a space using 'cout'. The execution table traces each step: starting the program, reading inputs, calculating results, outputting them, and ending the program. The variable tracker shows how variables change after input and calculation. Key moments clarify why inputs and outputs are done in one line. The quiz tests understanding of variable values and output timing. This helps beginners see how multiple inputs and outputs work together in a simple program.

Practice

(1/5)
1.

Which of the following statements correctly reads two integers a and b from the user in C++?

easy
A. std::cout >> a >> b;
B. std::cin << a << b;
C. std::cout << a << b;
D. std::cin >> a >> b;

Solution

  1. Step 1: Understand input operator usage

    In C++, std::cin is used for input and the extraction operator >> reads values.
  2. Step 2: Identify correct syntax for multiple inputs

    Using std::cin >> a >> b; reads two integers from the user correctly.
  3. Final Answer:

    std::cin >> a >> b; -> Option D
  4. Quick Check:

    Input uses std::cin with >> operator [OK]
Hint: Use std::cin with >> to read multiple inputs [OK]
Common Mistakes:
  • Using << instead of >> for input
  • Using std::cout for input
  • Mixing input and output operators
2.

Which of the following lines correctly prints three variables x, y, and z separated by spaces in C++?

easy
A. std::cout << x << ' ' << y << ' ' << z;
B. std::cout << x, y, z;
C. std::cout >> x >> y >> z;
D. std::cin << x << y << z;

Solution

  1. Step 1: Understand output operator usage

    In C++, std::cout is used for output and the insertion operator << prints values.
  2. Step 2: Print multiple variables with spaces

    Using std::cout << x << ' ' << y << ' ' << z; prints variables separated by spaces correctly.
  3. Final Answer:

    std::cout << x << ' ' << y << ' ' << z; -> Option A
  4. Quick Check:

    Output uses std::cout with << and spaces [OK]
Hint: Use std::cout with << and spaces to print multiple values [OK]
Common Mistakes:
  • Using commas instead of << for output
  • Using >> operator for output
  • Using std::cin for output
3.

What is the output of the following C++ code if the user inputs 5 10?

int a, b;
std::cin >> a >> b;
std::cout << a + b << ' ' << a * b << std::endl;
medium
A. 15 50
B. 510
C. 5 10
D. Syntax Error

Solution

  1. Step 1: Read input values

    The code reads two integers: a = 5 and b = 10 from user input.
  2. Step 2: Calculate and print sum and product

    The output prints a + b = 15 and a * b = 50 separated by a space.
  3. Final Answer:

    15 50 -> Option A
  4. Quick Check:

    Sum and product printed with space [OK]
Hint: Add and multiply inputs, print with space [OK]
Common Mistakes:
  • Concatenating numbers as strings
  • Forgetting space between outputs
  • Confusing input and output operators
4.

Identify the error in the following C++ code snippet:

int x, y;
std::cin << x << y;
std::cout << x << y;
medium
A. Missing semicolon after std::cin
B. >> used instead of << for output
C. << used instead of >> for input
D. Variables x and y not declared

Solution

  1. Step 1: Check input operator usage

    Input uses std::cin with extraction operator >>, but code uses << which is wrong.
  2. Step 2: Check output operator usage

    The code correctly uses std::cout with insertion operator <<.
  3. Final Answer:

    << used instead of >> for input -> Option C
  4. Quick Check:

    Input needs >> operator, not << [OK]
Hint: Input uses >>, output uses << operators [OK]
Common Mistakes:
  • Swapping << and >> operators
  • Confusing std::cin and std::cout
  • Ignoring operator direction
5.

Given the following code, what will be the output if the user inputs 3 4 5?

int a, b, c;
std::cin >> a >> b >> c;
std::cout << a + b << ' ' << b + c << ' ' << a + c << std::endl;
medium
A. Runtime Error
B. 7 9 8
C. 3 4 5
D. 12 9 7

Solution

  1. Step 1: Read three integers from input

    The variables are assigned as a=3, b=4, c=5 from user input.
  2. Step 2: Calculate sums and print

    Prints a + b = 7, b + c = 9, and a + c = 8 separated by spaces.
  3. Final Answer:

    7 9 8 -> Option B
  4. Quick Check:

    Sum pairs printed correctly [OK]
Hint: Add pairs of inputs and print with spaces [OK]
Common Mistakes:
  • Adding all three instead of pairs
  • Mixing input order
  • Forgetting spaces between outputs
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard