Problem

In this exercise, you experiment with the concepts of scope and lifetime. a. Follow th...

In this exercise, you experiment with the concepts of scope and lifetime.

a. Follow the instructions for starting C++ and opening the Interme-diatel9.cpp file. The file is contained in either the Cpp6\Chap091 Intermediatel9 Project folder or the Cpp61Chap09 folder. Run the program. If you are asked whether you want to run the last successful build, click the No button. The C++ compiler displays an error message indicating that the getDoubleNumber function does not recognize the number variable. The error occurs because the number variable is local to the main function. To fix this error, you can either pass the number variable's value to the getDoubleNumber function or create a global variable named number. Passing the variable's value is the preferred way for the main function to communicate with the getDoubleNumber function. However, to give you an opportunity to see how global variables work in a program, you will fix the program's error by creating a global variable named number.

b. Change the int number = 0; statement in the main function to .a comment. Recall that global variables are declared out-side of any function in the program. In the blank line below the //declare global variable comment, type int number = 0;. Because the number variable is now a global variable, both the main and getDoubleNumber functions have access to it. You will run the program to verify that fact.

c. Save and then run the program. When prompted for a number, type 5 and press Enter. The Command Prompt window shows that doubling the number 5 results in the number 10, which is correct.

d. Now change the statement that declares the global number variable to a comment. Also remove the two forward slashes from the // int number = 0; line in the main function. Fix the program's error by passing the value contained in the number variable to the getDoubleNumber function.

e. Save and then run the program. When prompted for a number, type S and press Enter. The Command Prompt window shows that doubling the number 5 results in the number 10, which is correct.

Step-by-Step Solution

Request Professional Solution

Request Solution!

We need at least 10 more requests to produce the solution.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the solution will be notified once they are available.
Add your Solution
Textbook Solutions and Answers Search