Sams Teach Yourself C++ in 21 Days

(singke) #1
Answers 823

D Answers


Exercises ........................................................................................................


  1. The following is one possible answer:
    1: #include
    2: using namespace std;
    3: int main()
    4: {
    5: cout << “I love C++\n”;
    6: return 0;
    7: }

  2. The following program contains a main()function that does nothing. This is, how-
    ever, a complete program that can be compiled, linked, and run. When run, it
    appears that nothing happens because the program does nothing!
    int main(){}

  3. Line 4 is missing an opening quote for the string.

  4. The following is the corrected program:
    1: #include
    2: main()
    3: {
    4: std::cout << “Is there a bug here?”;
    5: }
    This listing prints the following to the screen:
    Is there a bug here?

  5. The following is one possible solution:
    1: #include
    2: int Add (int first, int second)
    3: {
    4: std::cout << “In Add(), received “ << first << “ and “ << second
    ➥<< “\n”;
    5: return (first + second);
    6: }
    7:
    8: int Subtract (int first, int second)
    9: {
    10: std::cout << “In Subtract(), received “ << first << “ and “
    ➥<< second << “\n”;
    11: return (first - second);
    12: }
    13:
    14: int main()
    15: {
    16: using std::cout;
    17: using std::cin;
    18:
    19: cout << “I’m in main()!\n”;


32 0672327112_app_d.qxd 11/19/04 12:30 PM Page 823

Free download pdf