A Program That Uses the IO Library

In our bookstore problem, we’ll have several records that we’ll want to combine into a single total. As a simpler, related problem, let’s look first at how we might add two numbers. Using the IO library, we can extend our main program to prompt the user to give us two numbers and then print their sum:

#include <iostream>int main(){    std::cout << "Enter two numbers:" << std::endl;    int v1 = 0, v2 = 0;    std::cin >> v1 >> v2;    std::cout << "The sum of " << v1 << " and " << v2              << " is " << v1 + v2 << std::endl;    return 0;}

This program starts by printing

Enter two numbers:

on the user’s screen and then waits for input from the user. If the user enters

3 7

followed by a newline, then the program ...

Get C++ Primer, Fifth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.