C++ How ToLesson 59Free preview
C++ Add Two Numbers
This lesson is part of C++ · All levels
Summary
Adding two numbers together is the first exercise to understand the data stream in C++: read input with a ring, save it to a variable, count it, and then perform it with a cout. The choice of data type determines the result, for example the addition of int and double is automatically converted to double. This little exercise is about getting used to handling user input and displaying a neat output.
Key points
- Cin >> a >> b; read two values from the keyboard according to the type of variable.
- The result of the addition is stored in a variable so that it can be reused or reprinted.
- The combination of int and double results in double because of the operand promotion.
- For decimal results that are neat, use the fixed and setprecision of <iomanip>.
Full lesson
Code examples and quizzes
Full lessons, code examples, and quizzes are available to subscribers. 5 questions.