Вы находитесь на странице: 1из 8

Rochester Institute of Technology Golisano College of Computing and Information Sciences Department of Information Sciences & Technologies

Name: ____________________________ 2:00 4:00

Section:
(Circle one)

10:00

4002-208 Lab 9: myInput functions


Exercise 1 Creating a project (4 points)
Must be completed during the lab period. 1. You are to create the default project using Code::Blocks which prints the phrase "Hello,world". The directions can be found in the CodeBlock module on myCourses. Select the only item labeled Project Instructions which will download a document named codeblockinstuctions.pdf. Your instructor used the project name myInput. The contents of the Management pane should appear as below. Click the "plus symbol in a square" to the left of Sources to see the list of source files, here only main.cpp. If you do not see the Management pane, select View Manager.

Run the project in the normal manner and you will see the result below.

4002-208

Page 1 of 8

Fall 2012

Lab 9

4002-208

Page 2 of 8

Fall 2012

Lab 9 2. After you can successfully run the default project, download the file 208_Lab9.7z from the Lab 9 module in myCourses. When you unzip the file, you will find the four files below. You will use only the first three in the remainder of Exercise 1. File testMyInput.cpp myInput.cpp myInput.h readIntPos.cpp Description Contains the main function to test the code in the input function in myInput.cpp. Contains the code for the input function readInt. Contains the prototype for the input function readInt Used later

3. Copy the first three files above into your project folder. Your instructor used the folder named myInput which contains two folders bin and obj after the project was executed. 4. Now, remove the file main.cpp from the project and add the first three above to the project. To remove a file, use Project Remove file. To add the three files, use Project Add files. The Management pane should appear as below after the deletions and additions.

5. Compile the project. You should receive an error as shown below.

The file testMyInput.cpp did not include the prototype for the function readInt. This prototype is in the file myInput.h. You need to include this file

4002-208

Page 3 of 8

Fall 2012

Lab 9 in the file testMyInput.cpp. Use the following include statement: #include "myInput.h" The file is enclosed in double quotes as it is a local include file. The symbols <..> surrounding a file name indicate a system include file. Code::Blocks knows the location of the system include files. Execute the project. See sample execution on the next page.

4002-208

Page 4 of 8

Fall 2012

Lab 9 Sample Execution

Signature: _________________________ Date: __________________________ Have your instructor or TA sign here when Exercise 1 works correctly.

4002-208

Page 5 of 8

Fall 2012

Lab 9

Exercise 2 Adding another function to myInput (3 points)


Must be completed during the lab period. 6. The file readIntPos.cpp contains the code for the function readIntPos which was used in an assignment earlier in the quarter. This function reads a positive integer number. Copy and paste the entire function including the documentation for the function into the file myInput.cpp. 7. Several changes need to be made to the function. i. The function reads the input with the following statements: cout << prompt; cin >> num; Replace the above two statements with a call to the function readInt which reads an integer and checks for improperly formatted integer input. There are two places where the changes need to be made. i. At the end of the function readIntPos, the input buffer is flushed. This is not needed as the function readInt already flushes the input buffer. Note: if you forget to remove this code, you will need to enter data to be ignored. 8. Add code to the main function to test the new function by calling function readIntPos and printing the result. Sample Execution

Signature: _________________________ Date: __________________________ Have your instructor or TA sign here when Exercise 2 works correctly.

4002-208

Page 6 of 8

Fall 2012

Lab 9

Exercise 3 Adding another function to myInput (3 points)


If you do not complete this exercise during the lab period, you need to complete the work outside of the lab period and bring the completed work to the lab next week for testing and signoff. 9. You are to add another myInput function. The name of the function is readIntBetween. The purpose of this function is to read a valid integer between a minimum and maximum value. 10. The function has 3 parameters. The first is a prompt for the user of type string. The second is the minimum acceptable integer value. The third is the maximum acceptable value. For example, the following call would prompt the user to enter an integer option between 1 and 3, inclusively: intNum = readIntBetween("Enter option: ", 1, 3); 11. Since the code for this new function is similar to the code for the function readIntPos, you can copy the code for readIntPos to start the new function and then make the needed modifications. Again this new function will not directly read input with the cin statement, but rather use the function readInt to do the input. 12. Add the prototype for this new function to the header file myInput.h.

13. Add code to call the new function from the main function as shown in the sample output on the next page.

4002-208

Page 7 of 8

Fall 2012

Lab 9 Sample Execution

Prompt for 2 calls of readIntBetween

Signature: _________________________ Date: __________________________ Have your instructor or TA sign here when Exercise 3 works correctly.

4002-208

Page 8 of 8

Fall 2012

Вам также может понравиться