0% found this document useful (0 votes)
8 views

Lab_5_1_Using_the_while_Looping_Repetition_Structure

This document provides a detailed guide on using while loops in C++ programming, focusing on four types: counter-controlled, sentinel-controlled, flag-controlled, and EOF-controlled loops. It includes step-by-step instructions for designing and implementing programs that utilize these loops, along with memory tracking and expected outputs. The lab exercises aim to familiarize users with practical applications of while loops through various coding tasks.

Uploaded by

navid.panah1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Lab_5_1_Using_the_while_Looping_Repetition_Structure

This document provides a detailed guide on using while loops in C++ programming, focusing on four types: counter-controlled, sentinel-controlled, flag-controlled, and EOF-controlled loops. It includes step-by-step instructions for designing and implementing programs that utilize these loops, along with memory tracking and expected outputs. The lab exercises aim to familiarize users with practical applications of while loops through various coding tasks.

Uploaded by

navid.panah1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Lab 5.

1 Using the while Looping (Repetition) Structure

The three looping (repetition) structures, while, for, and do…while, are reserved
words in C++ and are used when you want a program to repeat a set of statements. The
three different types of looping structures offer flexibility in coding. This lab focuses on the
while loop.
The reserved word while acts on a Boolean expression and serves as a decision-maker.
Generally, the work of the loop is accomplished within the body of the loop, which can
include one or more statements, and is executed when the expression evaluates to true.
The expression is reevaluated after each iteration of the statements until the expression
evaluates to false. An exit condition must exist within a loop; otherwise, an infinite loop
will occur.
A looping expression contains the loop control variable (LCV). The LCV must be initialized
before it is evaluated and updated within the body of the loop to provide an exit condition.
All loops with LCVs require the following:
1. An initialization of the loop control variable.
2. Evaluation of the loop condition. If the loop condition is false, the body of the
loop never executes. If true, the body of the loop is executed.
3. An update of the loop control variable.
4. Repetition of Steps 2 and 3 until the loop condition evaluates to false.
There are four types of while loops: counter-controlled, sentinel-controlled, flag-
controlled, and end of file (EOF)-controlled.

Objectives
In this lab, you become acquainted with all four types of while loops.
After completing this lab, you will be able to:
• Write a counter-controlled while loop when you know exactly how many pieces of
data need to be read.
• Write a sentinel-controlled while loop that uses a special sentinel value to end the
loop.
• Write an EOF-controlled while loop that continues until the program reaches the
end of the file.
Estimated completion time: 60–75 minutes
Lab 5.1 Steps: Using the while Looping (Repetition) Structure

Design and write code for the following program that uses while loops.

1a. Design a program that prompts the user to enter the number of students registered in a
class. If the number is greater than 0, the program should use a counter-controlled
while loop to prompt the user that same number of times for the students’ names.
Write the students’ names to an output file named students.txt. Display a message to
the user when the program is complete.

Write your design in the following space. Your design should be a list of C++ comments
without any code.
1b. Write a C++ program based on the design you created in Exercise 1a and name it
roster1.cpp. Step through the code by hand.

Use the following memory table to show what occurs in memory when the C++ code is
executed. (Include line numbers as documentation only. Do not use line numbers when
entering your final program.) To fill out the memory table, use one or two lines for each
variable. On one line, enter declaration information. Write the name of the declared
variable, its data type, and the line number at declaration when initialized.

Variable Name Data Type Value in Line Number at Line Number


Memory Declaration when Initialized

In the following space, show what is displayed on the screen after executing the output
message.

1c. Enter, compile, link, and execute roster1.cpp. Then copy the output and save it in a
block comment at the end of your program. Save roster1.cpp in the Chap05 folder of
your Student Data Files.

The following is a copy of the screen results that might display after running your
program, depending on the data entered. The input entered by the user is shown in
bold.
How many students may register for this class? 9
Enter the student’s name: Yongrui Wuan
Enter the student’s name: Joy Garcia
Enter the student’s name: Tony Pederzani
Enter the student’s name: George Smith
Enter the student’s name: John Patrick
Enter the student’s name: Connor Bailey
Enter the student’s name: Ryan Dennis
Enter the student’s name: Susie Brown
Enter the student’s name: Jack Cunningham
The program is complete.
2a. Design a program that simulates an order room of a factory. To fill an order, workers
place items on a conveyer belt to be grouped, billed, and shipped. Each item has a UPC
code that designates the name of the item and its price. When an order is complete, a
worker places a bar on the conveyor belt to separate that order from the next order. The
bar has a UPC code of 999. In programming, a specific value to designate the end of a
loop is called a sentinel value. The UPC code of 999 is a sentinel value.

After the computer system scans the UPC code of each item, it records the name and
price in a file named invoice1.txt. This output file contains the product name and price
on one line, separated by a pound sign (#). For example, if a hammer is purchased for
the price of 9.95, the program writes this line as hammer#9.95. The program does not
write the sentinel value to the output file. In your program, use a while repetition
construct.

Because this is a program simulation, prompt the user to enter the name and price of
each item. On a real order-scanning machine, no prompt would be used. When the
system reads the value 999, display a thank-you message, the name of the output file,
and each item and price added to the output file.

Write your design in the following space. Your design should be a list of C++ comments
without any code.
2b. Write a C++ program based on the design you created in Exercise 2a and name it
scan1.cpp. Step through the code by hand.

Use the following memory table to show what occurs in memory when the C++ code is
executed. (Include line numbers as documentation only. Do not use line numbers when
entering your final program.) To fill out the memory table, use one or two lines for each
variable. On one line, enter declaration information. Write the name of the declared
variable, its data type, and the line number at declaration.

Variable Name Data Type Value in Line Number at Line Number


Memory Declaration when Initialized

In the following space, show what is displayed on the screen after executing the output
message.

2c. Enter, compile, link, and execute scan1.cpp. Then copy the output and save it in a block
comment at the end of your program. Save scan1.cpp in the Chap05 folder of your
Student Data Files.

The following is a copy of the screen results that might display after running your
program, depending on the data entered. The input entered by the user is shown in
bold.

Please scan the name of the first item: hammer


Please scan the price of the hammer: 9.95

Please scan the name of the next item: saw


Please scan the price of the saw: 20.15

Please scan the name of the next item: shovel


Please scan the price of the shovel: 35.40

Please scan the name of the next item: 999

Thank you for your order.


invoice1.txt
hammer#9.95
saw#20.15
shovel#35.40
3a. Design a program that prompts the user to enter the name of an item, searches the file
invoice1.txt that was created in Exercise 2 for the item, and then displays the name and
the price associated with that item. When you get to the point that a search item is not
in the list, you have reached the end of file. Use a compound flag-controlled while loop
to search the list until either the item is found or the end of the file is reached. Display a
message if the item is not found.
3b. Write a C++ program based on the design you created in Exercise 3a and name it
scan2.cpp. Step through the code by hand.

Use the following memory table to show what occurs in memory when the C++ code is
executed. (Include line numbers as documentation only. Do not use line numbers when
entering your final program.) To fill out the memory table, use one or two lines for each
variable. On one line, enter declaration information. Write the name of the declared
variable, its data type, and the line number at declaration.

Variable Name Data Type Value in Line Number at Line Number


Memory Declaration when Initialized

In the following space, show what is displayed on the screen after executing the output
message, depending on the data entered.
3c. Enter, compile, link, and execute scan2.cpp. Then copy the output and save it in a block
comment at the end of your program. Save scan2.cpp in the Chap05 folder of your
Student Data Files.

The following is a copy of the screen results that might display after running your
program, depending on the data entered. The input entered by the user is shown in
bold.

Enter name of item for search: shovel


The item shovel costs 35.40

Enter name of item for search: hoe


The item hoe was not found.
4a. Design a program that reads names and prices and then counts and accumulates all
prices in the file invoice1.txt. Display each name and price, total count, and total of all
prices in currency format.

Write your design in the following space. Your design should be a list of C++ comments
without any code.
4b. Write a C++ program based on the design you created in Exercise 4a and name it
scan3.cpp. Step through the code by hand.

Use the following memory table to show what occurs in memory when the C++ code is
executed. (Include line numbers as documentation only. Do not use line numbers when
entering your final program.) To fill out the memory table, use one or two lines for each
variable. On one line, enter declaration information. Write the name of the declared
variable, its data type, and the line number at declaration.

Variable Name Data Type Value in Line Number at Line Number


Memory Declaration when
Initialized

In the following space, show what is displayed on the screen after executing the output
message.
4c. Enter, compile, link, and execute scan3.cpp. Then copy the output and save it in a block
comment at the end of your program. Save scan3.cpp in the Chap05 folder of your
Student Data Files.

The following is a copy of the screen results that might display after running your
program, depending on the data entered.

Invoice
hammer 9.95
saw 20.15
shovel 35.40

Total 3 items $ 65.50

You might also like