8.5 Answers to Chapter Questions

I l @ ve RuBoard

Answer 8-1: The problem lies with the semicolon (;) at the end of the for statement. The body of the for statement is between the closing parentheses and the semicolon. In this case it is nothing. Even though the std::cout statement is indented, it is not part of the for statement. The indentation is misleading. The C++ compiler does not look at indentation. The program does nothing until the expression celsius <= 100 becomes false (celsius == 101) . Then the std::cout is executed.

Answer 8-2: The problem is that we read the number into data[1] through data[5] . In C++ the range of legal array indices is 0 to <array size >-1, or in this case 0 to 4. data[5] is illegal. When we use it strange things happen; in this case the variable three_count is changed. The solution is to use only data[0] through data[4] .

I l @ ve RuBoard


Practical C++ Programming
Practical C Programming, 3rd Edition
ISBN: 1565923065
EAN: 2147483647
Year: 2003
Pages: 364

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net