A text file has an employee database in the following format. First Name Last Na
ID: 3821627 • Letter: A
Question
A text file has an employee database in the following format. First Name Last Name Salary The salary value has special characters such as "S Write a program to find the average salary of the employees. You HAVE to read all three parameters as strings. Standard C++ does not support converting string to doubles. So, you MUST first convert this string to aC-string. Then, call the function that converts C-strings to floating point numbers. Use "HW8Probl dat file for your program. HINT 1. Use the string library functions to do this problem. Declare three strings variables, read the file one line at a time and assign the read values to correct variables. a 2. For the salary parameter, use substr0 function to grab the part of string without the S sign.Explanation / Answer
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;
int main()
{
ifstream infile("HW8Prob1.dat");
string firstName, lastName, tempSalary;
double salary;
while(infile >> firstName >> lastName >> tempSalary)
{
salary = atof(tempSalary.substr(1).c_str());
cout << "Firstname: " << firstName << " Lastname: " << lastName << " Salary: " << salary << endl;
}
return 0;
}
Sample output
Firstname: John Lastname: Harris Salary: 50000
Firstname: Lisa Lastname: Smith Salary: 75000.8
Firstname: Adam Lastname: Johnson Salary: 68500.5
Firstname: Sheila Lastname: Smith Salary: 150000
Firstname: Tristen Lastname: Major Salary: 75800.8
Firstname: Yannic Lastname: Lennart Salary: 58000.6
Firstname: Lorena Lastname: Emil Salary: 43000
Firstname: Tereza Lastname: Santeri Salary: 48000
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.