- Write C++ programs
- Compile C++ programs
- Implement programs that read and write to files
Instructions
Answer the programming problems sequentially (i.e., answer prob01 before prob02). If you have questions let your instructor or the lab assistant know. You can also consult your classmates.
When you answer two programming problems correctly, let your instructor know and wait for further instruction.
Lab exercise guide
Here’s a link to the Lab exercise guide in case you need to review the lab exercise objectives, grading scheme, or evaluation process.
Phonebook
This program is used to create a phonebook for storing contact information into a text file.
The program begins by asking the user to provide a name for the file that will contain their phone book information. It will then ask the user to provide the names and numbers of their contacts. It should keep asking the user for contact information until they type in Done
(case-sensitive).
After the user types Done
, store all contact information into the file name they provided at the beginning of the program. If the user does not add any contacts, then the file should be empty.
Please see the sample output below to guide the design of your program.
Sample Output:
Please provide the file name for your phone book: contacts.txtContact 1:Please provide the name: Elsie SimonPlease provide their number: (202)555-0115Contact 2:Please provide the name: Kaisha CopePlease provide their number: 5455689016Contact 3:Please provide the name: Sultan VargasPlease provide their number: 371-998-4166Contact 4:Please provide the name: DoneSaving phonebook into contacts.txtDone!
Submission checklist
- Compiled and ran the driver (
main
). - Manually checked for compilation and logical errors.
- Ensured no errors on the unit test (
make test
). - Followed advice from the stylechecker (
make stylecheck
). - Followed advice from the formatchecker to improve code readbility (
make formatcheck
).
Code evaluation
Open the terminal and navigate to the folder that contains this exercise. Assuming you have pulled the code inside of /home/student/labex02-tuffy
and you are currently in /home/student
you can issue the following commands
cd labex02-tuffy
You also need to navigate into the problem you want to answer. To access the files needed to answer problem 1, for example, you need to issue the following command.
cd prob01
When you want to answer another problem, you need to go back up to the parent folder and navigate into the next problem. Assuming you are currently in prob01
, you can issue the following commands to go to the parent folder then go into another problem you want to answer; prob02
for example.
cd ..cd prob02
Use the clang++
command to compile your code and the ./
command to run it. The sample code below shows how you would compile code save in main.cpp
and into the executable file main
. Make sure you use the correct filenames required in this problem. Take note that if you make any changes to your code, you will need to compile it first before you see changes when running it.
clang++ -std=c++17 main.cpp -o main./main
You can run one, two, or all the commands below to test
your code, stylecheck
your code’s design, or formatcheck
your work. Kindly make sure that you have compiled and executed your code before issuing any of the commands below to avoid errors.
make testmake stylecheckmake formatcheck
A faster way of running all these tests uses the all
parameter.
make all
Phonebook display
This program loads contact information stored in a text file and displays it on the screen.
The program asks the user to provide the filename of a phonebook file. It will then load all of the names and numbers stored in the file and display them on the screen.
We assume that the file is stored using the correct format wherein for each contact, there is always a name followed by their phone number. Below is an example of a possible phonebook file.
Sample phonebook file (phonebook.txt):
Shanay Wickens7205972770Harlee Collins3328206140Addison Ryan7917471622
Please see the sample output below assuming it loaded the phonebook file shown above to guide the design of your program. In case there are no contacts, inform the user that there were no contacts stored in the file. If the file does not exist, inform the user that the phonebook file is missing.
Sample Output:
Please provide the file name for your phone book: phonebook.txtContact 1:Name: Shanay WickensNumber: 7205972770Contact 2:Name: Harlee CollinsNumber: 3328206140Contact 3:Name: Addison RyanNumber: 7917471622
Empty phonebook sample Output:
Please provide the file name for your phone book: phonebook.txtPhone book does not contain any contacts!
Missing phonebook file sample Output:
Please provide the file name for your phone book: phonebookszz.txtInvalid phonebook file!
Submission checklist
- Compiled and ran the driver (
main
). - Manually checked for compilation and logical errors.
- Ensured no errors on the unit test (
make test
). - Followed advice from the stylechecker (
make stylecheck
). - Followed advice from the formatchecker to improve code readbility (
make formatcheck
).
Code evaluation
Open the terminal and navigate to the folder that contains this exercise. Assuming you have pulled the code inside of /home/student/labex02-tuffy
and you are currently in /home/student
you can issue the following commands
cd labex02-tuffy
You also need to navigate into the problem you want to answer. To access the files needed to answer problem 1, for example, you need to issue the following command.
cd prob01
When you want to answer another problem, you need to go back up to the parent folder and navigate into the next problem. Assuming you are currently in prob01
, you can issue the following commands to go to the parent folder then go into another problem you want to answer; prob02
for example.
cd ..cd prob02
Use the clang++
command to compile your code and the ./
command to run it. The sample code below shows how you would compile code save in main.cpp
and into the executable file main
. Make sure you use the correct filenames required in this problem. Take note that if you make any changes to your code, you will need to compile it first before you see changes when running it.
clang++ -std=c++17 main.cpp -o main./main
You can run one, two, or all the commands below to test
your code, stylecheck
your code’s design, or formatcheck
your work. Kindly make sure that you have compiled and executed your code before issuing any of the commands below to avoid errors.
make testmake stylecheckmake formatcheck
A faster way of running all these tests uses the all
parameter.
make all
Donations
This program is used to store donor contributions into a text file.
The program begins by asking the user to provide the number of donors participating in their cause and then the name of the file to store donation information. It will then ask the user to provide the donation amounts for each of the donors. The number of donors and the donation amounts should be stored into the text file with the file name provided by the user.
The program should only store the file if the user indicates that there is at least one donor. Otherwise, the program will not ask for a filename and display an error.
Please see the sample output below to guide the design of your program.
Sample Output:
Please provide the number of donors: 3Please provide the name of the donations file: donations.txtDonor 1: $35.00Donor 2: $500.00Donor 3: $235.10Thank you for donating!
Sample Output file:
335500235.10
Sample invalid input:
Please provide the number of donors: 0You need to have at least one donor for your cause to save donation information.
Submission checklist
- Compiled and ran the driver (
main
). - Manually checked for compilation and logical errors.
- Ensured no errors on the unit test (
make test
). - Followed advice from the stylechecker (
make stylecheck
). - Followed advice from the formatchecker to improve code readbility (
make formatcheck
).
Code evaluation
Open the terminal and navigate to the folder that contains this exercise. Assuming you have pulled the code inside of /home/student/labex02-tuffy
and you are currently in /home/student
you can issue the following commands
cd labex02-tuffy
You also need to navigate into the problem you want to answer. To access the files needed to answer problem 1, for example, you need to issue the following command.
cd prob01
When you want to answer another problem, you need to go back up to the parent folder and navigate into the next problem. Assuming you are currently in prob01
, you can issue the following commands to go to the parent folder then go into another problem you want to answer; prob02
for example.
cd ..cd prob02
Use the clang++
command to compile your code and the ./
command to run it. The sample code below shows how you would compile code save in main.cpp
and into the executable file main
. Make sure you use the correct filenames required in this problem. Take note that if you make any changes to your code, you will need to compile it first before you see changes when running it.
clang++ -std=c++17 main.cpp -o main./main
You can run one, two, or all the commands below to test
your code, stylecheck
your code’s design, or formatcheck
your work. Kindly make sure that you have compiled and executed your code before issuing any of the commands below to avoid errors.
make testmake stylecheckmake formatcheck
A faster way of running all these tests uses the all
parameter.
make all
Donations display
This program loads donation information stored in a text file, displays each one on the screen, and displays the total donations.
The program asks the user to provide the filename of a donation file. It will then load all of the donations in the file and display them on the screen. Finally it will display the total of all donations.
A donation file’s first line describes the number of donations listed in the file followed by the donations. Below is an example of a possible donation file.
Sample phonebook file (donations.txt):
4200143.23501.37396.88
Please see the sample output below assuming it loaded the donation file shown above to guide the design of your program. If the file does not exist, inform the user that the phonebook file is missing.
Sample Output:
Please provide the file name for your donation file: donations.txtDonation 1: $200.00Donation 2: $143.23Donation 3: $501.37Donation 4: $396.88Total donation: $1241.48
Missing donation file sample Output:
Please provide the file name for your donation file: donationszz.txtInvalid donation file!
Submission checklist
- Compiled and ran the driver (
main
). - Manually checked for compilation and logical errors.
- Ensured no errors on the unit test (
make test
). - Followed advice from the stylechecker (
make stylecheck
). - Followed advice from the formatchecker to improve code readbility (
make formatcheck
).
Code evaluation
Open the terminal and navigate to the folder that contains this exercise. Assuming you have pulled the code inside of /home/student/labex02-tuffy
and you are currently in /home/student
you can issue the following commands
cd labex02-tuffy
You also need to navigate into the problem you want to answer. To access the files needed to answer problem 1, for example, you need to issue the following command.
cd prob01
When you want to answer another problem, you need to go back up to the parent folder and navigate into the next problem. Assuming you are currently in prob01
, you can issue the following commands to go to the parent folder then go into another problem you want to answer; prob02
for example.
cd ..cd prob02
Use the clang++
command to compile your code and the ./
command to run it. The sample code below shows how you would compile code save in main.cpp
and into the executable file main
. Make sure you use the correct filenames required in this problem. Take note that if you make any changes to your code, you will need to compile it first before you see changes when running it.
clang++ -std=c++17 main.cpp -o main./main
You can run one, two, or all the commands below to test
your code, stylecheck
your code’s design, or formatcheck
your work. Kindly make sure that you have compiled and executed your code before issuing any of the commands below to avoid errors.
make testmake stylecheckmake formatcheck
A faster way of running all these tests uses the all
parameter.
make all
Consolidated report
This program loads a monthly report from a report file and displays the consolidated sales for a given range of months.
First, the program will ask the user to provide the filename of the monthly report file. Then the user will provide the number of months that will be consolidated and displayed. For example, if the user selects 1
then the total sales for each month will be shown. If the user selects 2
then every two months’ total sales will be added and shown. If the user selects 3
then the every three months’ total sales is shown and so on.
The program should only accept values equal to one or greater. Otherwise the program should display an error. If the number of months provided by the user does not perfectly divide the total number of months on the report, then it will just show the ranges that contain enough months. For example, if the user selects 2
and there are only 5 monthly reports, then it will display the total sales for months 1 and 2, and 3 and 4, only.
Below is an example of a possible monthly report file.
Sample monthly report file (report.txt):
999.99705.67803.23400.54
Please see the sample output below assuming it loaded the report file shown above to guide the design of your program. If the file does not exist, inform the user that the report file is missing.
Sample Output – 1 consolidated month:
Please provide the file name for your report file: report.txtPlease provide the number of months to consolidate: 1Month 1 sales: $999.99Month 2 sales: $705.67Month 3 sales: $803.23Month 4 sales: $400.54
Sample Output – 2 consolidated months:
Please provide the file name for your report file: report.txtPlease provide the number of months to consolidate: 2Month 1 2 sales: $1705.66Month 3 4 sales: $1203.77
Sample Output – 3 consolidated months:
Please provide the file name for your report file: report.txtPlease provide the number of months to be consolidated: 3Month 1 2 3 sales: $2508.89
Missing report file sample Output:
Please provide the file name for your report file: reportszz.txtInvalid report file!
Submission checklist
- Compiled and ran the driver (
main
). - Manually checked for compilation and logical errors.
- Ensured no errors on the unit test (
make test
). - Followed advice from the stylechecker (
make stylecheck
). - Followed advice from the formatchecker to improve code readbility (
make formatcheck
).
Code evaluation
Open the terminal and navigate to the folder that contains this exercise. Assuming you have pulled the code inside of /home/student/labex02-tuffy
and you are currently in /home/student
you can issue the following commands
cd labex02-tuffy
You also need to navigate into the problem you want to answer. To access the files needed to answer problem 1, for example, you need to issue the following command.
cd prob01
When you want to answer another problem, you need to go back up to the parent folder and navigate into the next problem. Assuming you are currently in prob01
, you can issue the following commands to go to the parent folder then go into another problem you want to answer; prob02
for example.
cd ..cd prob02
Use the clang++
command to compile your code and the ./
command to run it. The sample code below shows how you would compile code save in main.cpp
and into the executable file main
. Make sure you use the correct filenames required in this problem. Take note that if you make any changes to your code, you will need to compile it first before you see changes when running it.
clang++ -std=c++17 main.cpp -o main./main
You can run one, two, or all the commands below to test
your code, stylecheck
your code’s design, or formatcheck
your work. Kindly make sure that you have compiled and executed your code before issuing any of the commands below to avoid errors.
make testmake stylecheckmake formatcheck
A faster way of running all these tests uses the all
parameter.
make all
Reviews
There are no reviews yet.