设为首页 加入收藏

TOP

1.2 A First Look at Input/Output (1)
2013-10-07 16:13:46 来源: 作者: 【 】 浏览:85
Tags:1.2 First Look Input/Output

The C++(www.cppentry.com) language does not define any statements to do input or output (IO). Instead, C++(www.cppentry.com) includes an extensive standard library that provides IO (and many other facilities). For many purposes, including the examples in this book, one needs to know only a few basic concepts and operations from the IO library.

Most of the examples in this book use the iostream library. Fundamental to the iostream library are two types named istream and ostream, which represent input and output streams, respectively. A stream is a sequence of characters read from or written to an IO device. The term stream is intended to suggest that the characters are generated, or consumed, sequentially over time.

Standard Input and Output Objects

The library defines four IO objects. To handle input, we use an object of type istream named cin (pronounced see-in). This object is also referred to as the standard input. For output, we use an ostream object named cout (pronounced see-out). This object is also known as the standard output. The library also defines two other ostream objects, named cerr and clog (pronounced see-err and seelog, respectively). We typically use cerr, referred to as the standard error, for warning and error messages and clog for general information about the execution of the program.

Ordinarily, the system associates each of these objects with the window in which the program is executed. So, when we read from cin, data are read from the window in which the programis executing, andwhen we write to cout, cerr, or clog, the output is written to the same window.

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:

  1. #include <iostream>   
  2. int main()   
  3. {   
  4. std::cout << "Enter two numbers:" << std::endl;   
  5. int v1 = 0, v2 = 0;   
  6. std::cin >> v1 >> v2;   
  7. std::cout << "The sum of " << v1 << " and " << v2   
  8. << " is " << v1 + v2 << std::endl;   
  9. return 0;   
  10. }  

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

  1. 3 7  

followed by a newline, then the program produces the following output:

The sum of 3 and 7 is 10

The first line of our program

  1. #include <iostream> 

tells the compiler that we want to use the iostream library. The name inside angle brackets (iostream in this case) refers to a header. Every program that uses a library facility must include its associated header. The #include directive must be written on a single line—the name of the header and the #include must appear on the same line. In general, #include directives must appear outside any function. Typically, we put all the #include directives for a program at the beginning of the source file.

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇1.1 Writing a Simple C++ Progra.. 下一篇1.2 A First Look at Input/Outpu..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·数据库:推荐几款 Re (2025-12-25 12:17:11)
·如何最简单、通俗地 (2025-12-25 12:17:09)
·什么是Redis?为什么 (2025-12-25 12:17:06)
·对于一个想入坑Linux (2025-12-25 11:49:07)
·Linux 怎么读? (2025-12-25 11:49:04)