设为首页 加入收藏

TOP

1.4 Flow of Control (5)
2013-10-07 16:14:56 来源: 作者: 【 】 浏览:82
Tags:1.4 Flow Control

ENTERING AN END-OF-FILE FROM THE KEYBOARD

When we enter input to a program from the keyboard, different operating systems use different conventions to allow us to indicate end-of-file. On Windows systems we enter an end-of-file by typing a control-z—hold down the Ctrl key and press z— followed by hitting either the Enter or Return key. On UNIX systems, including on Mac OS X machines, end-of-file is usually control-d.


COMPILATION REVISITED

Part of the compiler’s job is to look for errors in the program text. A compiler cannot detect whether a program does what its author intends, but it can detect errors in the form of the program. The following are the most common kinds of errors a compiler will detect.

Syntax errors: The programmer has made a grammatical error in the C++(www.cppentry.com) language. The following programillustrates common syntax errors; each comment describes the error on the following line:

  1. // error: missing ) in parameter list for main   
  2. int main ( {   
  3. // error: used colon, not a semicolon, after endl   
  4. std::cout << "Read each file." << std::endl:   
  5. // error: missing quotes around string literal   
  6. std::cout << Update master. << std::endl;   
  7. // error: second output operator is missing   
  8. std::cout << "Write new master." std::endl;   
  9. // error: missing ; on return statement   
  10. return 0   
  11. }  

Type errors: Each itemof data in C++(www.cppentry.com) has an associated type. The value 10, for example, has a type of int (or, more colloquially, “is an int”). The word "hello", including the double quotation marks, is a string literal. One example of a type error is passing a string literal to a function that expects an int argument.

Declaration errors: Every name used in a C++(www.cppentry.com) program must be declared before it is used. Failure to declare a name usually results in an error message. The two most common declaration errors are forgetting to use std:: for a name from the library and misspelling the name of an identifier:

  1. #include <iostream>   
  2. int main()   
  3. {   
  4. int v1 = 0, v2 = 0;   
  5. std::cin >> v >> v2; // error: uses "v" not "v1"   
  6. // error: cout not defined; should be std::cout   
  7. cout << v1 + v2 << std::endl;   
  8. return 0;   
  9. }  

Error messages usually contain a line number and a brief description of what the compiler believes we have done wrong. It is a good practice to correct errors in the sequence they are reported. Often a single error can have a cascading effect and cause a compiler to report more errors than actually are present. It is also a good idea to recompile the code after each fix—or after making at most a small number of obvious fixes. This cycle is known as edit-compile-debug.

EXERCISES SECTION 1.4.3

Exercise 1.16: Write your own version of a program that prints the sum of a set of integers read from cin.
 

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇1.4 Flow of Control (6) 下一篇1.5 Introducing Classes (2)

评论

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

·数据库:推荐几款 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)