Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Exercise1, its really ugly but it works and its 4AM so screw it im submitting it, you will have to make inData and outData txt manually, please use the format firstname lastname occupation /n salary bonus taxrate /n distance traveled distance time /n objects sold object price
- /*
- Example in inData.txt:
- Jake Winston Programming
- 6490 7 30
- 450 9
- 106 1.38
- */
- #include "stdafx.h"
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <iomanip>
- #include <sstream>
- #ifdef _WIN32
- #include <Windows.h> // this is bad practice but I don't care.
- #endif
- using namespace std;
- template <size_t N>
- void splitString(string(&arr)[N], string str)
- {
- int n = 0;
- istringstream iss(str);
- for (auto it = istream_iterator<string>(iss); it != istream_iterator<string>() && n < N; ++it, ++n)
- arr[n] = *it;
- }
- int main()
- {
- ifstream inFile("inData.txt");
- ofstream outFile("outData.txt");
- std::cout << std::fixed;
- std::cout << std::setprecision(2);
- std::string writeThis;
- char line1[100];
- char line2[100];
- char line3[100];
- char line4[100];
- std::string firstName;
- std::string lastName;
- std::string occupation;
- std::string array0[3];
- std::string array1[3];
- std::string array2[2];
- std::string array3[2];
- std::string::size_type sz;
- double salary, bonus, taxes, paycheck, travelDistance, travelTime, averageSpeed, coffeeSold, coffeeCost, salesAmount;
- std::stringstream ssSalary, ssBonus, ssTaxes, ssPaycheck, ssTravelDistance, ssTravelTime, ssAverageSpeed, ssCoffeeSold, ssCoffeeCost, ssSalesAmount;
- inFile.getline(line1, 99);
- inFile.getline(line2, 99);
- inFile.getline(line3, 99);
- inFile.getline(line4, 99);
- std::string newLine1 = line1;
- std::string newLine2 = line2;
- std::string newLine3 = line3;
- std::string newLine4 = line4;
- // convert all lines after 1st to integers using complicated arrays
- splitString(array0, newLine1);
- splitString(array1, newLine2);
- splitString(array2, newLine3);
- splitString(array3, newLine4);
- salary = std::stod(array1[0], &sz);
- bonus = std::stod(array1[1], &sz);
- taxes = std::stod(array1[2], &sz);
- travelDistance = std::stod(array2[0], &sz);
- travelTime = std::stod(array2[1], &sz);
- coffeeSold = std::stod(array3[0], &sz);
- coffeeCost = std::stod(array3[1], &sz);
- firstName = std::string(array0[0]);
- lastName = std::string(array0[1]);
- occupation = std::string(array0[2]);
- paycheck = salary * ((bonus / 100) + 1) * (1 - (taxes / 100));
- averageSpeed = travelDistance / travelTime;
- salesAmount = coffeeSold * coffeeCost;
- std::string strSalary = std::to_string(salary);
- outFile << setprecision(2) << fixed << "Name: " << firstName << " " << lastName << ", Department: " << occupation << "\nMonthly Gross Salary: $" << salary << ", Monthly Bonus: " << bonus << "%, Taxes: " << taxes << "%\nPaycheck: $"
- << paycheck << "\n\nDistance Traveled: " << travelDistance << " miles, Traveling Time: " << travelTime << " hours\nAverage Speed: " << averageSpeed << " miles per hour\n\nNumber of Coffee Cups Sold: " << setprecision(0) << coffeeSold
- << setprecision(2) << ", Cost: $" << coffeeCost << " per cup\nSales Amount = $" << coffeeSold;
- inFile.close();
- outFile.close();
- printf("Data parsed and written.\n");
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement