aboutsummaryrefslogtreecommitdiff
path: root/CST116F2021-Lab8
diff options
context:
space:
mode:
authorJames Lawrance <[email protected]>2021-11-23 21:29:11 -0800
committerJames Lawrance <[email protected]>2021-11-23 21:29:11 -0800
commitea93e6d7dbe2ef6e8bfc58b6feeb07ba3809ec3f (patch)
tree7eba8abd196d77a6387556e7054967298c6e0d09 /CST116F2021-Lab8
parentAdd online IDE url (diff)
downloadcst115-lab8-jemersonlawrance-ea93e6d7dbe2ef6e8bfc58b6feeb07ba3809ec3f.tar.xz
cst115-lab8-jemersonlawrance-ea93e6d7dbe2ef6e8bfc58b6feeb07ba3809ec3f.zip
Lab 8 complete 11/23/21HEADmaster
Diffstat (limited to 'CST116F2021-Lab8')
-rw-r--r--CST116F2021-Lab8/CST116F2021-Lab8.cpp213
-rw-r--r--CST116F2021-Lab8/CST116F2021-Lab8.vcxproj7
-rw-r--r--CST116F2021-Lab8/CST116F2021-Lab8.vcxproj.filters13
-rw-r--r--CST116F2021-Lab8/Functions.cpp156
-rw-r--r--CST116F2021-Lab8/Header.h19
-rw-r--r--CST116F2021-Lab8/Output and Written Work.txt131
-rw-r--r--CST116F2021-Lab8/employee data.txt11
7 files changed, 538 insertions, 12 deletions
diff --git a/CST116F2021-Lab8/CST116F2021-Lab8.cpp b/CST116F2021-Lab8/CST116F2021-Lab8.cpp
index d4a77b8..a68b654 100644
--- a/CST116F2021-Lab8/CST116F2021-Lab8.cpp
+++ b/CST116F2021-Lab8/CST116F2021-Lab8.cpp
@@ -1,20 +1,209 @@
-// CST116F2021-Lab8.cpp : This file contains the 'main' function. Program execution begins and ends there.
-//
+#include "Header.h"
+//13b
+//int main()
+//{
+ //initial message
+// cout << setw(70) << "--Median finder program--" << endl;
+
+ //initializations
+// int valuesList[20];
+// int VALUE = -1;
+// int valuesTotal = 0;
+
+// int reorderList[20];
+// int reorderTotal = 0;
+
+ //get input
+// getInput(valuesList, VALUE);
+
+ //calculate median
+
+ //calculate length of list
+// calculateLength(valuesList, VALUE, valuesTotal);
+
+ //reorder list from lowest to highest value
+// lowestToHighest(valuesList, VALUE, valuesTotal, reorderList, reorderTotal);
+
+ //calculation based on even or odd
+// calculateMedian(valuesList, VALUE, valuesTotal);
+//}
+
+//13c
+/********************************************************************
+* File: Chapter 11 Debug.cpp
+*
+* General Instructions: Complete each step before proceeding to the
+* next.
+*
+* Debugging Exercise 1
+*
+* 1) Make your own data file like Troy 12, with the next person on the
+* next line and save it to a directory you create on your drive.
+* 2) Under the Project menu, select Add Existing Item and
+* add the input file you just placed on your drive to your
+* current project. Make sure your Solution Explorer window
+* is visible. If not, you can display it by selecting Solution
+* Explorer (or Ctrl+Alt+L).
+* 3) Open the input file by simply double clicking the name of the
+* file in your Solution Explorer.
+* 4) Build and execute the program.
+* 5) Update the file paths below to correctly represent the path you
+* created.
+* 6) Rebuild and execute the program.
+* 7) Examine the code and the output and notice the use of
+* parallel arrays.
+* 8) Add the output file created via the execution of
+* your program to your Project. Execute your program again
+* and notice how Visual Studio has rewritten your output file
+* and asks if you would like to reload the file (select Yes).
+* 9) Examine the contents of both the input and the output file.
+* 10) Fix all the problems in your code that exist in relation to
+* the output. Verify that your output is appropriate for your
+ input file.
+* 11) Build and execute your code until you have all errors
+* removed and all the output is correct.
+*
+* Debugging Exercise 2
+*
+* 1) Replace the double slashes (\\) in the input file open statement
+* with only a single slash
+* (i.e., inFile.open("C:\TEMP\Chap_11_data.txt").
+* 2) Build your code, noticing the impact of the invalid path you
+* created in the previous step.
+* 3) Replace the backslashes as they were.
+* 4) Change both the input and output filenames so they are
+* invalid.
+* 5) Update the file related error messages within the code
+* to also provide the specific name of the file that is having a
+* problem.
+* 6) Rebuild and execute your program to verify that your messages
+* are correct.
+* 7) Correct the path names.
+* 8) Build and execute your code and carefully check your
+* output on both the console and in the output file.
+*
+********************************************************************/
#include <iostream>
+#include <fstream> // For the files!!!!
+#include <iomanip> // For manipulators & formatting options
+using std::cin;
+using std::cout;
+using std::endl;
+using std::setw;
+using std::ios;
+
+using std::ifstream;
+using std::ofstream;
+
+const int EMPLOYEES = 20;
+const int MAX = 21;
+
+int ReadData(ifstream& inFile, ofstream& outFile, char name[][MAX], int age[]);
+void WriteOutputFile(ofstream& outFile, char name[][MAX], int age[],
+ int counter);
+void PrintTotalsAndSummary(ofstream& out, int totalRecords);
int main()
{
- std::cout << "Hello World!\n";
+ char name[EMPLOYEES][MAX];
+ int age[EMPLOYEES];
+ int record_counter(0);
+
+ ifstream inFile;
+
+ // Notice how this automatically opens the file
+ ofstream outFile("C:\\TEMP\\Chap_11_Report.txt");
+
+ inFile.open("C:\\TEMP\\Chap_11_data.txt");
+
+ if (inFile.is_open())
+ {
+ record_counter = ReadData(inFile, outFile, name, age);
+ inFile.close();
+
+ if (outFile.is_open())
+ {
+ WriteOutputFile(outFile, name, age, record_counter);
+ PrintTotalsAndSummary(outFile, record_counter);
+ outFile.close();
+ }
+ else
+ {
+ cout << "Trouble Opening File";
+ cout << "\n\n\t\t ** About to EXIT NOW! ** ";
+ }
+ }
+ else
+ {
+ cout << "Trouble Opening File";
+ cout << "\n\n\t\t ** About to EXIT NOW! ** ";
+ }
+ return 0;
}
+int ReadData(ifstream& inFile, ofstream& outFile, char name[][MAX], int age[])
+{
+ int counter = 0;
+ inFile >> name[counter] >> age[counter]; // Priming Read
+
+ while (!inFile.eof())
+ {
+ cout << setiosflags(ios::left) << setw(25)
+ << name[counter] << resetiosflags(ios::left)
+ << setw(4) << age[counter] << endl;
+ counter++;
+ inFile >> name[counter] >> age[counter];
+ }
+
+ return counter;
+}
+void WriteOutputFile(ofstream& outFile, char name[][MAX], int age[], int counter)
+{
+ outFile << " Here is the Output File" << endl;
+ for (int r = 0; r <= counter; r++)
+ {
+ outFile << setiosflags(ios::left) << setw(25)
+ << name[r] << setw(4)
+ << resetiosflags(ios::left) << age[r]
+ << endl;
+ }
+}
+void PrintTotalsAndSummary(ofstream& outFile, int totalRecords)
+{
+ // To screen
+ cout << "\n\n\t** Total Records: " << totalRecords << " **\n"
+ << "\t\t The End \n";
+
+ // To file
+ outFile << "\n\n\t** Total Records: " << totalRecords << " **\n"
+ << "\t\t The End \n";
+}
+
+
+//11.14
+int main()
+{
+ //initializations
+ string name;
+ int check;
+
+ //prompt
+ cout << setw(70) << "--Employee Data Organizer--\n"
+ << endl;
+
+ cout << "This program formats each employee's name, SS number, wage, hours worked, straight time pay, overtime pay, whether they work part-time or full-time, and their net pay.\n"
+ << endl;
+
+ check = checkFile();
-// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
-// Debug program: F5 or Debug > Start Debugging menu
+ if (check == 0)
+ {
+ createFile();
+ }
+ else
+ {
+ formatData();
+ }
-// Tips for Getting Started:
-// 1. Use the Solution Explorer window to add/manage files
-// 2. Use the Team Explorer window to connect to source control
-// 3. Use the Output window to see build output and other messages
-// 4. Use the Error List window to view errors
-// 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
-// 6. In the future, to open this project again, go to File > Open > Project and select the .sln file
+ return 0;
+} \ No newline at end of file
diff --git a/CST116F2021-Lab8/CST116F2021-Lab8.vcxproj b/CST116F2021-Lab8/CST116F2021-Lab8.vcxproj
index 00a34aa..5e89959 100644
--- a/CST116F2021-Lab8/CST116F2021-Lab8.vcxproj
+++ b/CST116F2021-Lab8/CST116F2021-Lab8.vcxproj
@@ -140,6 +140,13 @@
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="CST116F2021-Lab8.cpp" />
+ <ClCompile Include="Functions.cpp" />
+ </ItemGroup>
+ <ItemGroup>
+ <ClInclude Include="Header.h" />
+ </ItemGroup>
+ <ItemGroup>
+ <Text Include="Output and Written Work.txt" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
diff --git a/CST116F2021-Lab8/CST116F2021-Lab8.vcxproj.filters b/CST116F2021-Lab8/CST116F2021-Lab8.vcxproj.filters
index b0158d2..c4fc0d0 100644
--- a/CST116F2021-Lab8/CST116F2021-Lab8.vcxproj.filters
+++ b/CST116F2021-Lab8/CST116F2021-Lab8.vcxproj.filters
@@ -18,5 +18,18 @@
<ClCompile Include="CST116F2021-Lab8.cpp">
<Filter>Source Files</Filter>
</ClCompile>
+ <ClCompile Include="Functions.cpp">
+ <Filter>Source Files</Filter>
+ </ClCompile>
+ </ItemGroup>
+ <ItemGroup>
+ <ClInclude Include="Header.h">
+ <Filter>Header Files</Filter>
+ </ClInclude>
+ </ItemGroup>
+ <ItemGroup>
+ <Text Include="Output and Written Work.txt">
+ <Filter>Source Files</Filter>
+ </Text>
</ItemGroup>
</Project> \ No newline at end of file
diff --git a/CST116F2021-Lab8/Functions.cpp b/CST116F2021-Lab8/Functions.cpp
new file mode 100644
index 0000000..0070aef
--- /dev/null
+++ b/CST116F2021-Lab8/Functions.cpp
@@ -0,0 +1,156 @@
+#include "Header.h"
+
+//13b
+
+void getInput(int valuesList[], int& VALUE)
+{
+ while (valuesList[VALUE] != '\0')
+ {
+ VALUE++;
+
+ cout << "\nEnter value #" << VALUE + 1 << ". Enter 0 to stop input and display median.\n";
+ cin >> valuesList[VALUE];
+ if (valuesList[VALUE] == 0)
+ {
+ valuesList[VALUE] = '\0';
+ }
+ }
+
+ VALUE = 0;
+}
+
+void calculateLength(int valuesList[], int& VALUE, int& valuesTotal)
+{
+ while (valuesList[VALUE] != '\0')
+ {
+ valuesTotal++;
+ VALUE++;
+ }
+
+ VALUE = 0;
+}
+
+void lowestToHighest(int valuesList[], int& VALUE, int& valuesTotal, int reorderList[], int& reorderTotal)
+{
+ while (reorderTotal < valuesTotal)
+ {
+ int mostBig = 0;
+ int mostBigPos = 0;
+ VALUE = 0;
+
+ while (VALUE < valuesTotal)
+ {
+ if (valuesList[VALUE] > mostBig)
+ {
+ mostBig = valuesList[VALUE];
+ mostBigPos = VALUE;
+ }
+ VALUE++;
+ }
+
+ reorderList[((valuesTotal)-reorderTotal) - 1] = mostBig;
+ valuesList[mostBigPos] = 0;
+
+ reorderTotal++;
+ }
+
+ VALUE = 0;
+
+ while (VALUE < valuesTotal)
+ {
+ valuesList[VALUE] = reorderList[VALUE];
+
+ VALUE++;
+ }
+
+ VALUE = 0;
+}
+
+void calculateMedian(int valuesList[], int& VALUE, int& valuesTotal)
+{
+ if (valuesTotal % 2 == 0)
+ {
+ int mid1 = 0;
+ int mid2 = 0;
+
+ while (VALUE <= (valuesTotal / 2))
+ {
+
+ if (VALUE == ((valuesTotal / 2) - 1))
+ {
+ mid1 = valuesList[VALUE];
+ }
+
+ if (VALUE == ((valuesTotal / 2)))
+ {
+ mid2 = valuesList[VALUE];
+ }
+
+ VALUE++;
+ }
+
+ cout << "\nThe median of your input is: " << (mid1 + mid2) / 2
+ << endl;
+
+ }
+ else
+ {
+ int mid = 0;
+
+ VALUE = ((valuesTotal / 2));
+ mid = valuesList[VALUE];
+
+ cout << "\nThe median of your input is: " << mid
+ << endl;
+ }
+}
+
+//13c
+
+int checkFile()
+{
+ ifstream inFile;
+ inFile.open("employee data.txt");
+
+ if (!inFile)
+ {
+ return 0;
+ }
+ else
+ {
+ return 1;
+ }
+}
+
+void createFile()
+{
+ ofstream newFile;
+ newFile.open("employee data.txt");
+ newFile.close();
+
+ cout << "No file detected. "
+ << "A new file has been created. "
+ << "Please input data into the file and restart the program.\n";
+}
+
+void formatData()
+{
+ int wage[100] = {};
+ string text;
+
+ //if the file is detected
+ cout << "File detected. Formatting data...\n"
+ << endl;
+
+ ifstream readFile;
+ readFile.open("employee data.txt");
+
+ while (readFile)
+ {
+ getline(readFile, text);
+ cout << text << endl;
+ }
+
+ cout << "\nAll data has been read. The program will now end."
+ << endl;
+} \ No newline at end of file
diff --git a/CST116F2021-Lab8/Header.h b/CST116F2021-Lab8/Header.h
new file mode 100644
index 0000000..eb54d03
--- /dev/null
+++ b/CST116F2021-Lab8/Header.h
@@ -0,0 +1,19 @@
+#pragma once
+#include <iostream>
+#include <iomanip>
+#include <fstream>
+#include <cstring>
+#include <string>
+
+using namespace std;
+
+//13b
+void getInput(int valuesList[], int& VALUE);
+void calculateLength(int valuesList[], int& VALUE, int& valuesTotal);
+void lowestToHighest(int valuesList[], int& VALUE, int& valuesTotal, int reorderList[], int& reorderTotal);
+void calculateMedian(int valuesList[], int& VALUE, int& valuesTotal);
+
+//13c
+int checkFile();
+void createFile();
+void formatData();
diff --git a/CST116F2021-Lab8/Output and Written Work.txt b/CST116F2021-Lab8/Output and Written Work.txt
new file mode 100644
index 0000000..c3ef8ac
--- /dev/null
+++ b/CST116F2021-Lab8/Output and Written Work.txt
@@ -0,0 +1,131 @@
+13a -
+
+#5
+ a.
+ if (inFile.is_open)
+ {
+ ...
+ }
+ b.
+ string nameRecords[100];
+ int intRecords[2][100];
+
+ string lname;
+ int id;
+ int age;
+
+ for (int i = 0; nameRecords[i] != '\0' && intRecords[i] != '\0'; i++)
+ {
+ getline(inFile, lname, ',');
+ getline(inFile, id, ',');
+ getline(inFile, age, ',')
+
+ nameRecords[i] = lname;
+
+ intRecords[1][i] = id;
+ intRecords[2][i] = age;
+ }
+ c. inFile.close();
+
+ #6
+ a.
+ False
+ b.
+ True
+ c.
+ False
+ d.
+ True
+ e.
+ True
+
+13b output -
+
+output 1 -
+ --Median finder program--
+
+Enter value #1. Enter 0 to stop input and display median.
+6
+
+Enter value #2. Enter 0 to stop input and display median.
+7
+
+Enter value #3. Enter 0 to stop input and display median.
+7
+
+Enter value #4. Enter 0 to stop input and display median.
+1
+
+Enter value #5. Enter 0 to stop input and display median.
+2
+
+Enter value #6. Enter 0 to stop input and display median.
+9
+
+Enter value #7. Enter 0 to stop input and display median.
+3
+
+Enter value #8. Enter 0 to stop input and display median.
+7
+
+Enter value #9. Enter 0 to stop input and display median.
+6
+
+Enter value #10. Enter 0 to stop input and display median.
+0
+
+The median of your input is: 6
+
+output 2 -
+
+ --Median finder program--
+
+Enter value #1. Enter 0 to stop input and display median.
+100
+
+Enter value #2. Enter 0 to stop input and display median.
+200
+
+Enter value #3. Enter 0 to stop input and display median.
+300
+
+Enter value #4. Enter 0 to stop input and display median.
+400
+
+Enter value #5. Enter 0 to stop input and display median.
+0
+
+The median of your input is: 250
+
+13c output -
+
+output 1 -
+
+ --Employee Data Organizer--
+
+This program formats each employee's name, SS number, wage, hours worked, straight time pay, overtime pay, whether they work part-time or full-time, and their net pay.
+
+No file detected. A new file has been created. Please input data into the file and restart the program.
+
+output 2 -
+
+ --Employee Data Organizer--
+
+This program formats each employee's name, SS number, wage, hours worked, straight time pay, overtime pay, whether they work part-time or full-time, and their net pay.
+
+File detected. Formatting data...
+
+John Smith 123-09-8765 9.00 46 F
+Molly Brown 432-89-7654 9.50 40 F
+Tim Wheeler 239-34-3458 11.25 83 F
+Keil Wader 762-84-6543 6.50 35 P
+Trish Dish 798-65-9844 7.52 40 P
+Anthony Lei 934-43-9843 4.50 30 F
+Kevin Ashes 765-94-7343 4.50 30 P
+Cheryl Prince 983-54-9000 4.65 45 F
+Kim Cares 343-11-2222 10.00 52 F
+Dave Cockroach 356-98-1236 5.75 48 F
+Will Kusick 232-45-2332 15.00 45 P
+
+
+All data has been read. The program will now end. \ No newline at end of file
diff --git a/CST116F2021-Lab8/employee data.txt b/CST116F2021-Lab8/employee data.txt
new file mode 100644
index 0000000..52dbcee
--- /dev/null
+++ b/CST116F2021-Lab8/employee data.txt
@@ -0,0 +1,11 @@
+John Smith 123-09-8765 9.00 46 F
+Molly Brown 432-89-7654 9.50 40 F
+Tim Wheeler 239-34-3458 11.25 83 F
+Keil Wader 762-84-6543 6.50 35 P
+Trish Dish 798-65-9844 7.52 40 P
+Anthony Lei 934-43-9843 4.50 30 F
+Kevin Ashes 765-94-7343 4.50 30 P
+Cheryl Prince 983-54-9000 4.65 45 F
+Kim Cares 343-11-2222 10.00 52 F
+Dave Cockroach 356-98-1236 5.75 48 F
+Will Kusick 232-45-2332 15.00 45 P