Programming Language : C++ Use the C++ code below, to complete this. Create a .c
ID: 3864173 • Letter: P
Question
Programming Language : C++
Use the C++ code below, to complete this. Create a .csv file, paste this information below
Cross Shaina 12 Corrigan Ct Benicia CT 94510-2578 12.25 10.25
Nephyr Alysha 1748 Legend Cir Chelie CT 53248 10.25 14
Hellen Bernardo 1043 Amndo Avenue Ridgeside CT 44561 10.25 21
Colen Georgene 1435 Bayside Ct Mountain CT 44433-3147 13 21.75
Walters Ted 1115 Estes Ct York City CT 43522 10.5 16.5
Powers Denny 2530 Georgia Ct Newwater CT 44591-4934 15.5 21
Charlie Santos 307 Amberfall Circle Jersey CT 44532-1681 10.5 13.5
a.
Hint: The code you have been given reads all of the fields in each record as strings. You will need to convert wage and hours to float (or double) and perform the multiplication before writing the pay data to the output file.
#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
ifstream inFile;
string szSourcePath = // path of input file, change to your file location, if necessary
"test.csv";
ofstream outFile;
string szDestPath =
"out.csv"; // path of output file, change to your file location, if necessary
string szLastName;
string szFirstName;
string szAddress;
string szCity;
string szState;
string szZip;
string szWage;
string szHours;
string szPay;
inFile.open(szSourcePath);
outFile.open(szDestPath);
// this code reads the first line of the input file only
// insert loop to read entire file here
getline( inFile, szLastName, ',');
getline( inFile, szFirstName, ',');
getline( inFile, szAddress, ',');
getline( inFile, szCity, ',');
getline( inFile, szState, ',');
getline( inFile, szZip, ',');
getline( inFile, szWage, ',');
getline( inFile, szHours, ' ');
// calculate pay here
// insert write to output file here
inFile.close();
outFile.close();
system("Pause");
return 0;
}
Explanation / Answer
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main () {
string line;
string wage;
string hours;
float pay;
string delim = ",";
ifstream myfile ("example.csv");
ofstream myoutfile ("Outexample.csv");
if (myfile.is_open())
{
while ( getline (myfile,line) )
{
auto start = 0U;
auto end = line.find(delim);
int count = 0;
while (end != std::string::npos)
{
string temp = line.substr(start, end - start);
if (count == 6){
wage = temp;
}
else if (count == 6){
hours = temp;
}
start = end + delim.length();
end = s.find(delim, start);
count = count +1;
}
pay = stof(wage) * stof(hours);
myoutfile << line <<","<<to_string(pay)<<' ';
}
myfile.close();
myoutfile.close();
}
else {
cout << "Unable to open file";
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.