aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLTB-Pravda <[email protected]>2021-11-23 23:47:51 -0800
committerLTB-Pravda <[email protected]>2021-11-23 23:47:51 -0800
commit7d565e1a8e1463945fba6e5ecec77232edcb7783 (patch)
tree9232c73cd94061c5f8e9ee5396a417b59caa9d56
parentAdd online IDE url (diff)
downloadcst115-lab8-ltb-pravda-7d565e1a8e1463945fba6e5ecec77232edcb7783.tar.xz
cst115-lab8-ltb-pravda-7d565e1a8e1463945fba6e5ecec77232edcb7783.zip
Finalizing updates to Lab 8HEADmaster
-rw-r--r--CST116F2021-Lab8/CST116F2021-Lab8.cpp400
-rw-r--r--CST116F2021-Lab8/CST116F2021-Lab8.vcxproj6
-rw-r--r--CST116F2021-Lab8/CST116F2021-Lab8.vcxproj.filters14
3 files changed, 408 insertions, 12 deletions
diff --git a/CST116F2021-Lab8/CST116F2021-Lab8.cpp b/CST116F2021-Lab8/CST116F2021-Lab8.cpp
index d4a77b8..10dfa75 100644
--- a/CST116F2021-Lab8/CST116F2021-Lab8.cpp
+++ b/CST116F2021-Lab8/CST116F2021-Lab8.cpp
@@ -1,20 +1,396 @@
-// CST116F2021-Lab8.cpp : This file contains the 'main' function. Program execution begins and ends there.
+#include <iostream>
+#include <fstream>
+#include <string>
+#include <cctype> // For 13b
+#include <cmath> // For 13d
+#include <iomanip> // For 13c and 13d
+#include <sstream> // For 13d
+using namespace std;
+
+// 13a - 11.7 Exercises
+/*
+// #5
+
+a: if (inFile.is_open())
+
+b: for (int i = 0; i < RECORDS; i++)
+ {
+ used = i;
+ for (int j = 0; j < DATA; j++)
+ {
+ getline(inFile, records[i][j], ' ');
+ cout << records[i][j] << ' ';
+ }
+ if (inFile.eof())
+ break;
+ }
+// Note: RECORDS is a constant that marks the maximum amount of records
+ DATA is a constant that marks the number of entries per record
+
+c: inFile.close();
+
+// #6
+
+a: false
+
+b: true
+
+c: true
+
+d: true
+
+e: true
+
+*/
+
+// 13b - 11.9 Learn by Doing Exercises
+/*
+const int NUMS = 100;
+const int DIGIT = 12;
+
+void enter_nums(int[], int&);
+void sort_nums(int[], int);
+void median(int[], int);
+int main()
+{
+ int data[NUMS];
+ int nums = 0;
+ enter_nums(data, nums);
+ sort_nums(data, nums);
+ median(data, nums);
+}
+void enter_nums(int data[], int& nums)
+{
+ char input[DIGIT];
+ char exit[DIGIT] = "exit";
+ int temp;
+ cout << "Input numbers of at most 10 digits separated by a newline.\n"
+ << "Enter \"Exit\" to exit\n\n";
+ for (int i = 0; i < NUMS; i++)
+ {
+ nums = i;
+ cin.getline(input, DIGIT - 1, '\n');
+ _strlwr_s(input);
+ if (strcmp(input, exit) == 0)
+ {
+ cout << "Exiting input\n\n";
+ return;
+ }
+ temp = atoi(input);
+ data[i] = temp;
+ }
+ cout << "Max digits reached.\n"
+ << "Exiting input\n\n";
+ return;
+}
+void sort_nums(int data[], int nums)
+{
+ bool swapped;
+ int temp;
+ for (int i = 0; i < nums - 1; i++)
+ {
+ swapped = false;
+ for (int j = 0; j < nums - 1; j++)
+ {
+ if (data[j] > data[j + 1])
+ {
+ temp = data[j + 1];
+ data[j + 1] = data[j];
+ data[j] = temp;
+ swapped = true;
+ }
+ }
+ if (swapped == false)
+ break;
+ }
+}
+void median(int data[], int nums)
+{
+ float med;
+ int temp = nums;
+ if (temp % 2 == 1)
+ {
+ temp--;
+ temp /= 2;
+ med = data[temp];
+ }
+ else
+ {
+ temp /= 2;
+ med = data[temp];
+ med += data[temp + 1];
+ med /= 2;
+ }
+ cout << "The median is " << med << endl;
+}
+*/
+
+// 13c - 11.13 Debugging Exercises
+/*
+
+// 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 // Just used the same paths as they followed what I was doing
+// 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>
+// Removed #includes and put them at top of file
+using namespace std; // consolodated all the std:: statements
+
+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 Chap_11_Report.txt"; // Changed added file name
+ cout << "\n\n\t\t ** About to EXIT NOW! ** ";
+ }
+ }
+ else
+ {
+ cout << "Trouble Opening File Chap_11_data.txt"; // Changed added file name
+ 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
-// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
-// Debug program: F5 or Debug > Start Debugging menu
+ 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];
+ }
-// 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 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++) // Removed "=" so writing stops correctly
+ {
+ 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";
+}
+*/
+
+// 13d - 11.14 Programming Exercises #1
+/*
+const int RECORDS = 20;
+const int DATA = 6;
+const int FORMAT = 9;
+void read_data(string[][DATA], int&);
+void format_data(string[][DATA], string[][FORMAT], int);
+void print_format(string[][FORMAT], int);
+int main()
+{
+ string data[RECORDS][DATA]; // Input data
+ string format[RECORDS][FORMAT]; // Output formatted
+ int used = 0; // Number of entries
+ read_data(data, used);
+ format_data(data, format, used);
+ print_format(format, used);
+ return 0;
+}
+void read_data(string records[][DATA], int& used)
+{
+ ifstream inFile;
+ inFile.open("C:\\Temp\\Lab_8_data.txt");
+
+ if (inFile.is_open())
+ {
+ for (int i = 0; i < RECORDS; i++)
+ {
+ used = i;
+ for (int j = 0; j < DATA; j++)
+ {
+ getline(inFile, records[i][j], ' ');
+ cout << records[i][j] << ' ';
+ }
+ if (inFile.eof())
+ break;
+ }
+ }
+
+ inFile.close();
+}
+void format_data(string data[][DATA], string format[][FORMAT], int used)
+{
+ float wage, // Hourly wage from data
+ t1, // Temp number for calculations
+ t2; // Temp number for calculations
+ int hrs; // Hours worked from data
+ int hrs2; // Overtime worked
+ double total; // Calcuated total pay
+ for (int i = 0; i < used; i++)
+ {
+ stringstream temp0, // All temp strings to format numbers
+ temp1,
+ temp2,
+ temp3;
+ wage = 0; // Resetting each variable each loop and making sure the program doesn't forget it exists (That was a weird problem)
+ t1 = 0;
+ t2 = 0;
+ hrs = 0;
+ hrs2 = 0;
+ total = 0;
+ for (int j = 0; j < 5; j++) // For the first 4 columns
+ format[i][j] = data[i][j]; // Copy from data
+ wage = stof(data[i][3]); // Get wage from data
+ hrs = stoi(data[i][4]); // Get hours from data
+ temp1 << '$' << fixed << setprecision(2) << wage; // Format wage
+ temp1 >> format[i][3]; // Return to format array
+ if (hrs > 40) // If overtime worked
+ {
+ hrs2 = hrs - 40; // Calculate overtime
+ hrs = 40; // Set regular hours to 40
+ }
+ t1 = hrs * wage; // Temp1 = regular hours * wage
+ temp2 << '$' << fixed << setprecision(2) << t1; // Format temp1
+ temp2 >> format[i][5]; // Return to format array
+ t2 = hrs2 * wage * 1.5; // Temp2 = overtime hours * wage * time and a half pay
+ temp3 << '$' << fixed << setprecision(2) << t2; // Format temp2
+ temp3 >> format[i][6]; // Return to format array
+ if (data[i][DATA - 1] == "F") // If marked as full time
+ {
+ format[i][7] = "Full Time"; // Return to format array
+ total -= 5; // Subtract union fees
+ }
+ else
+ format[i][7] = "Part Time"; // Return to format array
+ total += t1; // Add temp1 to total
+ total += t2; // Add temp2 to total
+ temp0 << '$' << fixed << setprecision(2) << total; // Format total
+ temp0 >> format[i][8]; // Return to format array
+ }
+}
+void print_format(string format[][FORMAT], int used)
+{
+ ofstream outFile;
+ outFile.open("C:\\Temp\\Lab_8_out.txt");
+ if (outFile.is_open())
+ {
+ cout << "\n\n"; // Formatting
+ // For some reason, the second row of data onwards takes up 1 less character than the first or titles, so I had to increase the setw for them
+ cout << setw(12) << left << "First Name" << setw(12) << left << "Last Name" << setw(15) << left << "SSN"
+ << setw(10) << left << "Wage" << setw(10) << left << "Hours" << setw(10) << left << "Reg Pay" << setw(10) << left << "Overtime"
+ << setw(12) << left << "Employment" << "Total" << endl << endl;;
+ cout << setw(12) << left << format[0][0] << setw(12) << left << format[0][1] << setw(15) << left << format[0][2]
+ << setw(10) << left << format[0][3] << setw(10) << left << format[0][4] << setw(10) << left << format[0][5]
+ << setw(10) << left << format[0][6] << setw(12) << left << format[0][7] << format[0][8] << endl;
+ // Same thing but writing to the file
+ outFile << setw(12) << left << "First Name" << setw(12) << left << "Last Name" << setw(15) << left << "SSN"
+ << setw(10) << left << "Wage" << setw(10) << left << "Hours" << setw(10) << left << "Reg Pay" << setw(10) << left << "Overtime"
+ << setw(12) << left << "Employment" << "Total" << endl << endl;;
+ outFile << setw(12) << left << format[0][0] << setw(12) << left << format[0][1] << setw(15) << left << format[0][2]
+ << setw(10) << left << format[0][3] << setw(10) << left << format[0][4] << setw(10) << left << format[0][5]
+ << setw(10) << left << format[0][6] << setw(12) << left << format[0][7] << format[0][8] << endl;
+ for (int i = 1; i < used; i++)
+ {
+ cout << setw(13) << left << format[i][0] << setw(12) << left << format[i][1] << setw(15) << left << format[i][2]
+ << setw(10) << left << format[i][3] << setw(10) << left << format[i][4] << setw(10) << left << format[i][5]
+ << setw(10) << left << format[i][6] << setw(12) << left << format[i][7] << format[i][8] << endl;
+ // Same thing but writing to the file
+ outFile << setw(13) << left << format[i][0] << setw(12) << left << format[i][1] << setw(15) << left << format[i][2]
+ << setw(10) << left << format[i][3] << setw(10) << left << format[i][4] << setw(10) << left << format[i][5]
+ << setw(10) << left << format[i][6] << setw(12) << left << format[i][7] << format[i][8] << endl;
+ }
+
+ }
+
+ outFile.close();
+}
+*/ \ No newline at end of file
diff --git a/CST116F2021-Lab8/CST116F2021-Lab8.vcxproj b/CST116F2021-Lab8/CST116F2021-Lab8.vcxproj
index 00a34aa..39e79ea 100644
--- a/CST116F2021-Lab8/CST116F2021-Lab8.vcxproj
+++ b/CST116F2021-Lab8/CST116F2021-Lab8.vcxproj
@@ -141,6 +141,12 @@
<ItemGroup>
<ClCompile Include="CST116F2021-Lab8.cpp" />
</ItemGroup>
+ <ItemGroup>
+ <Text Include="..\..\..\..\..\..\Temp\Chap_11_Data.txt" />
+ <Text Include="..\..\..\..\..\..\Temp\Chap_11_Report.txt" />
+ <Text Include="..\..\..\..\..\..\Temp\Lab_8_data.txt" />
+ <Text Include="..\..\..\..\..\..\Temp\Lab_8_out.txt" />
+ </ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
diff --git a/CST116F2021-Lab8/CST116F2021-Lab8.vcxproj.filters b/CST116F2021-Lab8/CST116F2021-Lab8.vcxproj.filters
index b0158d2..4ec3496 100644
--- a/CST116F2021-Lab8/CST116F2021-Lab8.vcxproj.filters
+++ b/CST116F2021-Lab8/CST116F2021-Lab8.vcxproj.filters
@@ -19,4 +19,18 @@
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
+ <ItemGroup>
+ <Text Include="..\..\..\..\..\..\Temp\Chap_11_Data.txt">
+ <Filter>Resource Files</Filter>
+ </Text>
+ <Text Include="..\..\..\..\..\..\Temp\Chap_11_Report.txt">
+ <Filter>Resource Files</Filter>
+ </Text>
+ <Text Include="..\..\..\..\..\..\Temp\Lab_8_data.txt">
+ <Filter>Resource Files</Filter>
+ </Text>
+ <Text Include="..\..\..\..\..\..\Temp\Lab_8_out.txt">
+ <Filter>Resource Files</Filter>
+ </Text>
+ </ItemGroup>
</Project> \ No newline at end of file