$24
C1A8 General Information, continued
2
What is a “Working Directory”?
Where Does a Program Look for Files When Attempting to Open Them?
6
Where Does a Program Create New Files?
7
Where Should You Put Instructor-Supplied Data Files?
8
9
What is a “Working Directory”?
A program’s “Working Directory” is the directory it uses for any files it opens or creates if their names are
specified without a path, and you must place any instructor-supplied data file(s) (.txt or .bin extensions)
your program needs in that directory. Its default location differs between IDEs and operating systems
and it is important to know where it is and how to change it. For further information please refer to the
Determining/Changing the “Working Directory” topic in the version of the course document titled “Using
the Compiler’s IDE…” that is applicable to the IDE you are using.
17
18
19
20
Opening Files – Testing for Failure/Success
21
Always check the success/failure status of opening a file before using it or opening another file.
26
Supplying Information to a Program via its “Command Line”
27
It is often more appropriate to supply information to a program via “command line arguments” than by
user prompts. Such arguments can be provided regardless of how a program is being run, whether it be
from within an IDE, a system command window, a GUI icon, or a batch file. For this course I strongly
recommend using an IDE for running all programs.
If you are not familiar with using command line arguments first review note 8.3 for information on how to
process them within any program, then review the appropriate version of the course document titled
“Using the Compiler’s IDE…”, which illustrates implementing an arbitrary command line in several ways
including implementing command arguments containing spaces.
It is important to note that command line redirection information (note 4.2), if any, is only visible to the
operating system and will not be among the command line arguments available to the program being
run.
Personalized C1A8 requirements exclusively for Jose Medrano (U09845800)
C/C++ Programming I (Section 174273)
C1A8E0 (6 points total - 1 point per question – No program required)
Assume language standards compliance and any necessary standard library support unless stated otherwise. These are not trick questions and there is only one correct answer. Basing an answer on actual testing is risky. Place your answers in a plain text “quiz file” named C1A8E0_Quiz.txt formatted as:
a “Non-Code” Title Block, an empty line, then the answers:
A
C etc.
Personalized C1A8 requirements exclusively for Jose Medrano (U09845800)
C/C++ Programming I (Section 174273)
C1A8E1 (6 points – C++ Program)
Exclude any existing source code files that may already be in your IDE project and add three new ones,
naming them C1A8E1_SavingsAccount.h, C1A8E1_SavingsAccount.cpp, and C1A8E1_main.cpp. Do not
use #include to include either of the two .cpp files in each other or in any other file. However, you may
use it to include any appropriate header file(s) you need. You must include C1A8E1_SavingsAccount.h
in any file that needs its contents.
7
This exercise is not an example of rigorous banking practices but is only to familiarize you with simple C++
class members. Assume the class below represents just the data members of a savings account:
10
class SavingsAccount // Do not change the access level (private) of the class’s data members.
11
12
{
// Do not add any additional data members.
int type_;
string ownerName_;
long IDnbr_;
double balance_, closurePenaltyPercent_;
};
18
19
Arrays and loops are neither necessary nor allowed anywhere in this exercise.
There must not be a SavingsAccount variable anywhere other than in function main.
21
Do not add any member functions other than those described below.
22
File C1A8E1_SavingsAccount.h must contain the following:
1. The class SavingsAccount definition from above with the following added to its contents:
25
a. The prototype only for member function GetInitialValues.
26
b. The prototype only for member function DisplayValues.
27
c. The entire definition of member function CalculatePenalty.
2. The entire definition of function DisplayValues – placed outside class SavingsAccount.
File C1A8E1_SavingsAccount.cpp must contain:
1. The entire definition of member function GetInitialValues.
File C1A8E1_main.cpp must contain function main, which must:
1. Declare a variable of type SavingsAccount (do not allocate it dynamically).
2. Call functions GetInitialValues, DisplayValues, and CalculatePenalty in that order. You
may respond to any prompts with whatever values you deem sufficient to test your program.
3. Display the value returned from CalculatePenalty using the following format, where the
question mark represents the actual value:
39
Account closure penalty: ?
40
GetInitialValues: a non-inline member function, which must:
1. Have a void parameter list and return void.
2. For each of the five data members in class SavingsAccount on this page and in the order
shown there, prompt the user for a value and initialize the member with it. An owner’s name
containing spaces must be supported.
46
DisplayValues: a const inline member function, which must:
1. Have a void parameter list and return void.
2. Not prompt for anything.
3. Use one cout to display the values of all five data members of its SavingsAccount object in the
order and format shown below, replacing the question marks with the members’ values:
52
Account type: ?
53
Owner name: ?
54
ID number: ?
55
Account balance: ?
……………………Continued
56
Account closure penalty percent: ?
1992-2023 Ray Mitchell
Page 1 of 2 of C1A8E1
Personalized C1A8 requirements exclusively for Jose Medrano (U09845800)
C/C++ Programming I (Section 174273)
CalculatePenalty: a const inline member function, which must:
1. Have a void parameter list and return an appropriate type.
2. Calculate the account closure penalty and return it to the caller. It will be the percent specified
by closurePenaltyPercent_ of the account balance specified by balance_. Do not use
division.
3. Not prompt or display anything.
63
64
Manually re-run your program several times, testing with different values.
Submitting your solution
`Send an empty-body email to the assignment checker with the subject line C1A8E1_174273_U09845800
and with all three source code files attached.
See the course document titled “How to Prepare and Submit Assignments” for additional exercise
formatting, submission, and assignment checker requirements.
73
74
Hints:
1. If you attempt to read the account owner’s name from user input using the >> operator, your
program will fail if the name contains whitespace. This is because that operator stops reading a
string when the first non-leading whitespace is encountered. The correct approach is to use the
getline function (note 7.7).
80
2. See note 4.1. Some operations that read newline-terminated input leave the newline character
in the input buffer, which then acts as leading whitespace for a subsequent read. If that
character is still there when the getline function is called, only it will be read and the line will be
considered empty. Use either cin >> ws or cin.ignore() before calling getline to remove
and discard the newline character. ws is not a variable but is instead what is known as a
“manipulator”. It is in the std namespace and is available when you include standard C++
header file iostream. It is not necessary to include standard C++ header file iomanip to use the
ws manipulator.
89
3. 1.3% of 0.2 is not 0.26
4. Be sure to use “include guards” (note D.2) in header file C1A8E1_SavingsAccount.h. Use
#include to include this file in files C1A8E1_SavingsAccount.cpp and C1A8E1_main.cpp.
5. If you get a “multiply defined” linker error regarding function DisplayValues, it is because it has
96 not been declared as “inline”.
1992-2023 Ray Mitchell Page 2 of 2 of C1A8E1
Personalized C1A8 requirements exclusively for Jose Medrano (U09845800)
C/C++ Programming I (Section 174273)
C1A8E2 (8 points – C++ Program)
Exclude any existing source code files that may already be in your IDE project and add a new one,
naming it C1A8E2_main.cpp. Write a program in that file to perform a “Search and Replace All”
4 operation. This operation consists of performing a case-sensitive search for all occurrences of
an arbitrary sequence of characters within a file and substituting another arbitrary sequence in place of
them.
7
One of the biggest problems some students have with this exercise occurs simply because they do not
read the command line information provided in the appropriate version of the “Using the … IDE” course
document. Aside from this, I recommend (but do not require) that you implement the algorithm
provided in the hints.
12
Your program:
1. May assume, for simplicity:
15
a. It will only be used with text files.
16
b. The input and output file names will be different.
17
c. No character sequence will be split across multiple lines.
2. Must support multiple occurrences of the “search” character sequence on a single line.
3. Must support cases where the length of the “search” character sequence is different from the
length of the “replacement” character sequence.
4. Must get the following information from the command line in the order listed. Spaces must be
allowed in all items:
23
a. The name of the file to search (the input file).
24
b. The name of the file to store the results in (the output file).
25
c. The sequence of characters to search for.
26
d. The sequence of characters to use as replacements.
5. Must verify that exactly the correct number of command arguments exist before accessing them
and must exit with an error message and error code if they do not.
6. Must read one line at time from the input file into a character buffer using the getline function
and process it completely before reading the next line.
7. Must not copy anything from the character buffer to any destination other than the output file.
8. Must not use data types list, queue, stack, string, or vector.
9. Must not use functions peek, seekp, seekg, tellp, tellg, fseek, ftell, fsetpos, or fgetpos.
10. Must not call the strlen function in any part of any loop.
11. Must not declare more than one array or use dynamic memory allocation.
12. Must not test for an empty input file or one that does not contain the search sequence.
13. Must not write any function other than main.
38
Manually re-run your program several times, testing at least the following 2 cases with instructor-supplied
data file TestFile1.txt, which must be placed in the program’s “working directory”. Choose any name
you wish for the output file as long as it is different from the input file name:
42
43
Test#
Input File
Output File
Search For
Replace Each With
44
1
TestFile1.txt
your choice
the
John Galt?
45
2
TestFile1.txt
your choice
string literal
TESTING
46
For example (Test #1), if the input file contained,
These are the answers to neither of their questions!
the output file would contain,
These are John Galt? answers to neiJohn Galt?r of John Galt?ir questions!
Note that there were three replacements on one line and that “the” was part of another word in
two of those cases.
53
Submitting your solution
`Send an empty-body email to the assignment checker with the subject line C1A8E2_174273_U09845800
1992-2023 Ray Mitchell Page 1 of 2 of C1A8E2
Personalized C1A8 requirements exclusively for Jose Medrano (U09845800)
C/C++ Programming I (Section 174273)
and with your source code file attached.
See the course document titled “How to Prepare and Submit Assignments” for additional exercise
formatting, submission, and assignment checker requirements.
59
Hints:
1. The value of EOF cannot be correctly stored or detected using data type char or unsigned char;
use type int instead (Notes 4.3A & 4.3B). Since the end of file condition only occurs when a read
(or write) is attempted, testing for it before such an attempt is meaningless and inappropriate.
Only test after such an attempt before the value obtained from reading is used (Note 4.3B).
2. Not testing files for a successful opening is bad programming (Notes 10.3 & 10.4B).
3. It is not necessary to determine the length of the replacement string.
68
69 You may use any algorithm you wish to complete this exercise as long as none of the
requirements/restrictions are violated. The following describes the algorithm I used and recommend.
Drawing a diagram of memory as the algorithm progresses always helps:
72
General Algorithm Description:
For each line read into a character buffer from the input file
{
Set a pointer to the first character in the line.
For each “search” sequence found in the line using the strstr library function:
{
Copy characters into the output file from the pointer to where the “search” sequence starts.
Write the replacement string into the output file.
Move the pointer to the next character in the input line after the “search” sequence ends.
}
Copy the remainder of the line into the output file.
}
85
Algorithm Step-by-Step Implementation Details:
1. Do all the standard things, including declaring needed variables and opening the input and
output files.
2. Implement a loop statement that contains the following 3 statements (a, b, and c) in order:
90
a. a statement that gets the next line from the input file and stores it into a character buffer.
91
(The newline character must be discarded.) If the end of file condition occurs, terminate
92
the loop. Otherwise, proceed to step b below.
93
b. a “for” statement that does everything in steps i and ii below (in order):
94
i. does the following in its “control” section. The “control” section is the portion of
95
the “for” statement that is in parentheses just after the keyword for:
96
1)
The “initial expression” (Note 3.5) initializes a character pointer (I’ll call it
97
cp1) to point to the beginning of the character buffer you are reading
98
each line into.
99
2)
The “controlling expression” (Note 3.5) assigns the return value of the
100
strstr function to a different character pointer (I’ll call it cp2). The first
101
argument of strstr will be cp1 and the second argument will be a
102
pointer to the first character of the string you are looking for in the file.
103
3)
The “loop expression” (Note 3.5) is empty.
104
ii. does the following in the loop “body”:
105
1)
Uses the write function to write to the output file. The first argument of
106
write will be cp1 and the second will be cp2-cp1.
107
2)
Uses the << operator to write the replacement string to the output file.
108
3)
Updates cp1 by assigning to it the sum of cp2 and the length of the string
109
you are searching for in the file.
110
c. a statement that writes the string in cp1 and a newline character to the output file.
1992-2023 Ray Mitchell
Page 2 of 2 of C1A8E2
Page 7 (7/11/2023)