More Loops
count-controlled loop must posses 3 elements:
1. It must initialize a counter variable to begin with 2. Test the initialization variable with a counter variable by comparing it to the maximum value, and terminate when reached. 3.Update the counter variable during each iteration, usually by increment it.
What three steps are taken when a file is used by a program
1. Open the file 2. Process the File 3. Close the File
Programs that calculate the total of a series of numbers typically use 2 elements:
1. A loop that reads each number in the series 2. A variable that accumulates the total of the numbers as they are read.
to read large files use a...
loop
read position
marks the location of the next byte that will be read from the file
To get the total number of iterations of a nested loop
multiply the number of iterations of all thee loops
num -= 1;
num = num - 1;
postfix mode
num++
An example of opening a file for output writing
ofstream outputFile; outputFile.open("Employees.txt");
header ofstream
output file stream. you create an object of this data type when you want to create a file and write data to it
Example and format of writing a string literal to a file
outputFile << "I love C++ programming\n";
Example and format of writing a string literal and the contens of a variable to a file
outputFile << "Price: " << Price << endl;
A common way to accidentally create an infinite while loop that makes the program look like it is lost in space
place a semicolon after the while () like: while ();
do-while loop
post test loop - its expression is tested after each iteration
programming read
provides the first value for the loop to test. it is entered just before the loop takes place
when using multiple statements in the initialization or update expression
separate values with a comma
Two general ways to access data stored in a file:
sequential and random access files
file-name extensions
short sequences of characters that appear at the end of the filename preceeded by a period
how to fave a user define the start and stop of the for loop
store their input in to a min and max variable and initialize num = min and go num <+ max
2 types of files
text and binary
a while loop will terminate when
the expression is found false
loop header
the first line of the for loop
postfix mode causes
the increment to happen after the value of the variable is used in the expression
prefix mode causes
the increment to happes after the value of the variable is used in the expression
input validation
the process of inspecting data given to a program by the user and determining if it is valid.
reading data
the process of retrieving data from the file
writing data
the process of saving data to a file
without a colse of the file
the program will have an error after a loop
If no braces are included in the loop, the while loop will only execute:
the statement immediately following it
two backslashes are needed
to represent one backslash in a string literal
Any non zero value is evaluated as
true
in order to use multiple statements in the test expression
use && or ||
A common mistake in while loops causing the test expression to assign a number to the variable instead of testing whether the variable i eqal to a number
using = instead of ==
a variable in the RAM
when a piece of data is read from a file, it is copied from the file to...
the first byte in the file
when an input file is opened its read position is initially set to ...
variables
when data is read from a file, the data flows from the file stream object that is associated with the file, into ...
file
when s program needs to store date for later use, it writes the data in a ....
format of while loop
while (expression) statement;
example using >> as a boolean expression in a while loop
while (inputFile >> number) If there are no more items to be read from the file the expression returns false
when the >> operator extracts data from the file it reads pieces of data separated by
whitespace characters (spaces, tabs or newlines).
random access file aka direct access file
works like a cd. you can jump directly to any piece of data in the file without reading the data that becomes before it.
sequential access file
you access data from the beginning of the file to the end of it. To get to one point of the file, you must pass through everything preceding it. Works like a cassette tape.
Process the file
Data is read from or written to the file
#include <fstream>
The header file in code for reading and writing files
Accumulator
The variable used to keep the running total
opening an input file
allows the program to read data from the fiel
user-controlled loop
allows the user to decide the number of iterations
test expression
an expression that controls the execution of the loop - as long as this expression is true the body of the loop will repeat
file stream object
an object that is associated wit a specific file and provides a way for a program to work with that file. - stream aka stream of data
Difference between do and do-while loop
because do-while is post-test loop and does not test the expression until it has completed an iteration, it will always do at least one iteration even if false, whereas the while is a pre-test loop
two types of loops
conditional loops and count controlled loops
text file
contains data that has been encoded as text using a scheme such as ASCII orUnicode
binary file
contains data that has not been converted to text.
infinite loop
continues to repeat until the program is interrupted.
Example of a while loop repeating and asking the user to enter a number between 1-100 until a number in the correct range is entered
cout << "enter #"; cin >> num; while (num<1 || num >100) { count << "enter a num in corect range"; cin >> num; }
Open the file
creates a connection between the file and the program
opening an output file
creates the file on the disk and allows the program to write data to it
decrement
decrease by one
control structures
direct the flow of a program
Close the file
disconnects the file from the program after the program is finsihed
format of do-while loop
do statement; while (expression);
iteratiion
each repetition of a loop
counter
a variable that is regularly incremented or decremented each time a loop iterates
Good times to use the for loop
Ideal in situations where the exact number of iterations is unknown.
prefix mode
++num
Good times to use a do-while loop
Ideal in situations where you always want the loop to iterate t least once. Good choice for repeating a menu.
2 things to happen before data can be written to or read from a file
1. a file stream object must be created 2. The file must be opened and linked to the file stream object
2 important parts of the while loop
1. an expression is tested for true and false, and 2. a statement or block that is repeated as long as the expression is true
c = a * b++ a=2, b=5
10
c = a * ++b a=2, b=5
11
Nested loop
A loop inside of another loop
Count-Controlled Loops
A loop that repeats a specific number of times - such as sales amounts for each month in a year must iterate 12 times
fstream
File stream. Objects of this data type can be used to open files for reading, writing, or both.
Good times to use a while loop
In situations where you do not want the loop to iterate if the condition is false from the beginning, such as, validating input that has been read and reading lists of data terminated by a sentinel value.
for loop is a pretest or posttest loop?
Pre-test - as long as the expression is true, the body of the loop will repeat
A loop keeps a running total because it
accumulates the total as it reads each number in the series
loop
a control structure that causes a statement or a group of statements to repeat
input file
a file that data is read from
output file
a file that data is written to
The for loop is ideal for performing
a known number of iterations
Sentinel
a special value that marks the end of a list of values. It cannot be mistaken as a member of the list and signals that there are no more values to be entered. When a user enters it the loop terminates.
A running total
a sum of numbers that accumulates with each iteration of a loop.
Conditional Loop
executes as long as a particular condition exists - such as a menu that executes as long as the input is valid
update expression
executes at the end of each iteration, usually increments the variable
filenames
files on a disk are identified by their...
Format of the For loop
for (initialization; test; update) statement;
what would make the for loop not terminate when you expect it to
if a statement in the body loop also modifies the counter variable
use a if statement to determine if the file opened correctly
if(inputFile)
how to define a file stream object and open a file in one statemetn
ifstream inputFile("customers.txt"); ofstream outputFile("Employees.txt");
Define an ifstream object and name it inputFile
ifstream inputFile;
code for creating a filestream object and linking the file to be open to the file stream object in order to open a file for input
ifstream inputFile; inputFile.open("Customers.txt")
when to use a for loop instead of a do while or while loop
in any situation that clearly requires an initialization, uses a false statement to stop the loop, and requires and update to occur at the end of the iteration.
file stream object in memory
in order for a program to work with a file on the computers disk, the program must create a ...
increment
increase by one
initialization expression
initialize a counter variable into its string value
header ifstream
input file stream. You create an object of this data type when you want to open an existing file and read data from it
example reading input from a file into the variable name
inputFile >> name;
the file stream object's close member function to call to close a file:
inputFile.close();
Call the inputFile object's member function and pass the string "Customers.txt" as an argument
inputFile.open("Customers.txt")
when a variable is defined in a for loop
it is not accesable outside the loop