210_lab09

Upload: 07031981

Post on 02-Apr-2018

217 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/27/2019 210_Lab09

    1/8

    Rochester Institute of TechnologyGolisano College of Computing and Information

    SciencesDepartment of Information Sciences &

    Technologies

    Name: ____________________________ Section: 10:002:00 4:00

    (Circle one)

    4002-208Lab 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 thephrase "Hello,world". The directions can be found in the CodeBlockmodule onmyCourses. Select the only item labeledProject Instructions which will download a document named codeblock-instuctions.pdf.

    Your instructor used the project name myInput.

    The contents of the Management pane should appear as below. Click the "plussymbol in a square" to the left ofSources to see the list of source files, hereonly 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

  • 7/27/2019 210_Lab09

    2/8

    Lab 9

    4002-208 Page 2 of 8 Fall 2012

  • 7/27/2019 210_Lab09

    3/8

    Lab 9

    2. After you can successfully run the default project, download the file208_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 theremainder of Exercise 1.

    File DescriptiontestMyInput.cpp Contains the main function to test the code in

    the input function in myInput.cpp.myInput.cpp Contains the code for the input function

    readInt.myInput.h Contains the prototype for the input function

    readIntreadIntPos.cpp Used later

    3. Copy the first three files above into your project folder. Your instructor used thefolder 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 aboveto the project.To remove a file, use Project Remove file. To add the three files, useProject Add files.The Management pane should appear as below after the deletions andadditions.

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

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

    4002-208 Page 3 of 8 Fall 2012

  • 7/27/2019 210_Lab09

    4/8

    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::Blocksknows 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

  • 7/27/2019 210_Lab09

    5/8

    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

  • 7/27/2019 210_Lab09

    6/8

    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 positiveinteger 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 > num;

    Replace the above two statements with a call to the function readInt whichreads an integer and checks for improperly formatted integer input. Thereare 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: ifyou 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 functionreadIntPos 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

  • 7/27/2019 210_Lab09

    7/8

    Lab 9

    Exercise 3 Adding another function to myInput (3 points)If you do not complete this exercise during the lab period, you need tocomplete the work outside of the lab period and bring the completedwork to the lab next week for testing and signoff.

    9. You are to add another myInput function. The name of the function isreadIntBetween. The purpose of this function is to read a valid integerbetween a minimum and maximum value.

    10. The function has 3 parameters. The first is a prompt for the user of typestring. The second is the minimum acceptable integer value. The third is themaximum acceptable value. For example, the following call would prompt theuser 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 functionand then make the needed modifications. Again this new function will notdirectly read input with the cin statement, but rather use the function readIntto 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 thesample output on the next page.

    4002-208 Page 7 of 8 Fall 2012

  • 7/27/2019 210_Lab09

    8/8

    Lab 9

    Sample Execution

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

    4002-208 Page 8 of 8 Fall 2012

    Prompt for 2calls ofreadIntBetween